Java Code Examples for org.apache.bcel.Const#IFNULL

The following examples show how to use org.apache.bcel.Const#IFNULL . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WaitInLoop.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void sawOpcode(int seen) {

    if ((seen == Const.INVOKEVIRTUAL || seen == Const.INVOKEINTERFACE) && "notify".equals(getNameConstantOperand())
            && "()V".equals(getSigConstantOperand())) {
        sawNotify = true;
        notifyPC = getPC();
    }
    if (!(sawWait || sawAwait) && (seen == Const.INVOKEVIRTUAL || seen == Const.INVOKEINTERFACE)
            && (isMonitorWait() || isConditionAwait())) {

        if ("wait".equals(getNameConstantOperand())) {
            sawWait = true;
        } else {
            sawAwait = true;
        }
        waitHasTimeout = !"()V".equals(getSigConstantOperand());
        waitAt = getPC();
        earliestJump = getPC() + 1;
        return;
    }
    if (seen >= Const.IFEQ && seen <= Const.GOTO || seen >= Const.IFNULL && seen <= Const.GOTO_W) {
        earliestJump = Math.min(earliestJump, getBranchTarget());
    }
}
 
Example 2
Source File: ObligationAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
private Type nullCheck(short opcode, Edge edge, InstructionHandle last, BasicBlock sourceBlock)
        throws DataflowAnalysisException {
    if (DEBUG_NULL_CHECK) {
        System.out.println("checking for nullcheck on edge " + edge);
    }
    Type type = null;
    if ((opcode == Const.IFNULL && edge.getType() == EdgeTypes.IFCMP_EDGE)
            || (opcode == Const.IFNONNULL && edge.getType() == EdgeTypes.FALL_THROUGH_EDGE)) {
        Location location = new Location(last, sourceBlock);
        TypeFrame typeFrame = typeDataflow.getFactAtLocation(location);
        if (typeFrame.isValid()) {
            type = typeFrame.getTopValue();
            if (DEBUG_NULL_CHECK) {
                System.out.println("ifnull comparison of " + type + " to null at " + last);
            }
        }
    }
    return type;
}
 
Example 3
Source File: DismantleBytecode.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void printOpCode(int seen) {
    System.out.print("  " + this.getClass().getSimpleName() + ": [" + formatter.format(getPC()) + "]  " + Const.getOpcodeName(seen));
    if ((seen == Const.INVOKEVIRTUAL) || (seen == Const.INVOKESPECIAL) || (seen == Const.INVOKEINTERFACE) || (seen == Const.INVOKESTATIC)) {
        System.out.print("   " + getClassConstantOperand() + "." + getNameConstantOperand() + " " + getSigConstantOperand());
    } else if (seen == Const.LDC || seen == Const.LDC_W || seen == Const.LDC2_W) {
        Constant c = getConstantRefOperand();
        if (c instanceof ConstantString) {
            System.out.print("   \"" + getStringConstantOperand() + "\"");
        } else if (c instanceof ConstantClass) {
            System.out.print("   " + getClassConstantOperand());
        } else {
            System.out.print("   " + c);
        }
    } else if ((seen == Const.ALOAD) || (seen == Const.ASTORE)) {
        System.out.print("   " + getRegisterOperand());
    } else if ((seen == Const.GOTO) || (seen == Const.GOTO_W) || (seen == Const.IF_ACMPEQ) || (seen == Const.IF_ACMPNE)
            || (seen == Const.IF_ICMPEQ)
            || (seen == Const.IF_ICMPGE) || (seen == Const.IF_ICMPGT) || (seen == Const.IF_ICMPLE) || (seen == Const.IF_ICMPLT)
            || (seen == Const.IF_ICMPNE) || (seen == Const.IFEQ) || (seen == Const.IFGE) || (seen == Const.IFGT) || (seen == Const.IFLE)
            || (seen == Const.IFLT)
            || (seen == Const.IFNE) || (seen == Const.IFNONNULL) || (seen == Const.IFNULL)) {
        System.out.print("   " + getBranchTarget());
    } else if ((seen == Const.NEW) || (seen == Const.INSTANCEOF)) {
        System.out.print("   " + getClassConstantOperand());
    } else if ((seen == Const.TABLESWITCH) || (seen == Const.LOOKUPSWITCH)) {
        System.out.print("    [");
        int switchPC = getPC();
        int[] offsets = getSwitchOffsets();
        for (int offset : offsets) {
            System.out.print((switchPC + offset) + ",");
        }
        System.out.print((switchPC + getDefaultSwitchOffset()) + "]");
    }

    System.out.println();
}
 
Example 4
Source File: DismantleBytecode.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static boolean areOppositeBranches(int opcode1, int opcode2) {
    if (!isBranch(opcode1)) {
        throw new IllegalArgumentException(Const.getOpcodeName(opcode1) + " isn't a branch");
    }
    if (!isBranch(opcode2)) {
        throw new IllegalArgumentException(Const.getOpcodeName(opcode2) + " isn't a branch");
    }
    switch (opcode1) {
    case Const.IF_ACMPEQ:
    case Const.IF_ACMPNE:
    case Const.IF_ICMPEQ:
    case Const.IF_ICMPNE:
    case Const.IF_ICMPLT:
    case Const.IF_ICMPLE:
    case Const.IF_ICMPGT:
    case Const.IF_ICMPGE:
    case Const.IFNE:
    case Const.IFEQ:
    case Const.IFLT:
    case Const.IFLE:
    case Const.IFGT:
    case Const.IFGE:
        return ((opcode1 + 1) ^ 1) == opcode2 + 1;
    case Const.IFNONNULL:
        return opcode2 == Const.IFNULL;
    case Const.IFNULL:
        return opcode2 == Const.IFNONNULL;
    default:
        return false;

    }
}
 
Example 5
Source File: BadUseOfReturnValue.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (seen == Const.INVOKEVIRTUAL && "indexOf".equals(getNameConstantOperand())
            && "java/lang/String".equals(getClassConstantOperand())
            && "(Ljava/lang/String;)I".equals(getSigConstantOperand())) {
        stringIndexOfOnTOS = true;
    } else if (stringIndexOfOnTOS) {
        if (seen == Const.IFLE || seen == Const.IFGT) {
            bugAccumulator.accumulateBug(
                    new BugInstance(this, "RV_CHECK_FOR_POSITIVE_INDEXOF", LOW_PRIORITY).addClassAndMethod(this), this);
        }
        stringIndexOfOnTOS = false;
    }

    if (seen == Const.INVOKEVIRTUAL && "readLine".equals(getNameConstantOperand())
            && "()Ljava/lang/String;".equals(getSigConstantOperand()) && getClassConstantOperand().startsWith("java/io")
            && !"java/io/LineNumberReader".equals(getClassConstantOperand())) {
        readLineOnTOS = true;
    } else if (readLineOnTOS) {
        if (seen == Const.IFNULL || seen == Const.IFNONNULL) {
            bugAccumulator.accumulateBug(
                    new BugInstance(this, "RV_DONT_JUST_NULL_CHECK_READLINE", NORMAL_PRIORITY).addClassAndMethod(this), this);
        }

        readLineOnTOS = false;
    }
}
 
Example 6
Source File: ObligationAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Obligation comparesObligationTypeToNull(Edge edge) throws DataflowAnalysisException {
    BasicBlock sourceBlock = edge.getSource();
    InstructionHandle last = sourceBlock.getLastInstruction();
    if (last == null) {
        if (DEBUG_NULL_CHECK) {
            System.out.println("no last instruction in source block of " + edge + " ???");
        }
        return null;
    }
    Type type = null;

    short opcode = last.getInstruction().getOpcode();
    switch (opcode) {
    case Const.IFNULL:
    case Const.IFNONNULL:
        type = nullCheck(opcode, edge, last, sourceBlock);
        break;

    case Const.IF_ACMPEQ:
    case Const.IF_ACMPNE:
        type = acmpNullCheck(opcode, edge, last, sourceBlock);
        break;
    default:
        break;
    }

    if (!(type instanceof ObjectType)) {
        return null;
    }

    try {
        // See if the type of value compared to null is an obligation type.
        return database.getFactory().getObligationByType((ObjectType) type);
    } catch (ClassNotFoundException e) {
        errorLogger.reportMissingClass(e);
        throw new MissingClassException(e);
    }

}
 
Example 7
Source File: NullDerefAndRedundantComparisonFinder.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Examine basic blocks for null checks and potentially-redundant null
 * comparisons.
 *
 * @throws DataflowAnalysisException
 * @throws CFGBuilderException
 */
private void examineBasicBlocks() throws DataflowAnalysisException, CFGBuilderException {
    // Look for null check blocks where the reference being checked
    // is definitely null, or null on some path
    Iterator<BasicBlock> bbIter = invDataflow.getCFG().blockIterator();
    while (bbIter.hasNext()) {
        BasicBlock basicBlock = bbIter.next();

        if (basicBlock.isNullCheck()) {
            analyzeNullCheck(invDataflow, basicBlock);
        } else if (!basicBlock.isEmpty()) {
            // Look for all reference comparisons where
            // - both values compared are definitely null, or
            // - one value is definitely null and one is definitely not null
            // These cases are not null dereferences,
            // but they are quite likely to indicate an error, so while
            // we've got
            // information about null values, we may as well report them.
            InstructionHandle lastHandle = basicBlock.getLastInstruction();
            Instruction last = lastHandle.getInstruction();
            switch (last.getOpcode()) {
            case Const.IF_ACMPEQ:
            case Const.IF_ACMPNE:
                analyzeRefComparisonBranch(basicBlock, lastHandle);
                break;
            case Const.IFNULL:
            case Const.IFNONNULL:
                analyzeIfNullBranch(basicBlock, lastHandle);
                break;
            default:
                break;
            }
        }
    }
}
 
Example 8
Source File: FindSpinLoop.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {

    // System.out.println("PC: " + PC + ", stage: " + stage1);
    switch (seen) {
    case Const.ALOAD_0:
    case Const.ALOAD_1:
    case Const.ALOAD_2:
    case Const.ALOAD_3:
    case Const.ALOAD:
        if (DEBUG) {
            System.out.println("   ALOAD at PC " + getPC());
        }
        start = getPC();
        stage = 1;
        break;
    case Const.GETSTATIC:
        if (DEBUG) {
            System.out.println("   getfield in stage " + stage);
        }
        lastFieldSeen = FieldAnnotation.fromReferencedField(this);
        start = getPC();
        stage = 2;
        break;
    case Const.GETFIELD:
        if (DEBUG) {
            System.out.println("   getfield in stage " + stage);
        }
        lastFieldSeen = FieldAnnotation.fromReferencedField(this);
        if (stage == 1 || stage == 2) {
            stage = 2;
        } else {
            stage = 0;
        }
        break;
    case Const.GOTO:
    case Const.IFNE:
    case Const.IFEQ:
    case Const.IFNULL:
    case Const.IFNONNULL:
        if (DEBUG) {
            System.out.println("   conditional branch in stage " + stage + " to " + getBranchTarget());
        }
        if (stage == 2 && getBranchTarget() == start) {
            bugReporter.reportBug(new BugInstance(this, "SP_SPIN_ON_FIELD", NORMAL_PRIORITY).addClassAndMethod(this)
                    .addReferencedField(lastFieldSeen).addSourceLine(this, start));
            stage = 0;
        } else if (getBranchTarget() < getPC()) {
            stage = 0;
        }
        break;
    default:
        stage = 0;
        break;
    }

}
 
Example 9
Source File: Noise.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    int priority;
    switch (seen) {
    case Const.INVOKEINTERFACE:
    case Const.INVOKEVIRTUAL:
    case Const.INVOKESPECIAL:
    case Const.INVOKESTATIC:
        hq.pushHash(getClassConstantOperand());
        if (getNameConstantOperand().indexOf('$') == -1) {
            hq.pushHash(getNameConstantOperand());
        }
        hq.pushHash(getSigConstantOperand());

        priority = hq.getPriority();
        if (priority <= Priorities.LOW_PRIORITY) {
            accumulator.accumulateBug(new BugInstance(this, "NOISE_METHOD_CALL", priority).addClassAndMethod(this)
                    .addCalledMethod(this), this);
        }
        break;
    case Const.GETFIELD:
    case Const.PUTFIELD:
    case Const.GETSTATIC:
    case Const.PUTSTATIC:
        hq.pushHash(getClassConstantOperand());
        if (getNameConstantOperand().indexOf('$') == -1) {
            hq.pushHash(getNameConstantOperand());
        }
        hq.pushHash(getSigConstantOperand());
        priority = hq.getPriority();
        if (priority <= Priorities.LOW_PRIORITY) {
            accumulator.accumulateBug(new BugInstance(this, "NOISE_FIELD_REFERENCE", priority).addClassAndMethod(this)
                    .addReferencedField(this), this);
        }
        break;
    case Const.CHECKCAST:
    case Const.INSTANCEOF:
    case Const.NEW:
        hq.pushHash(getClassConstantOperand());
        break;
    case Const.IFEQ:
    case Const.IFNE:
    case Const.IFNONNULL:
    case Const.IFNULL:
    case Const.IF_ICMPEQ:
    case Const.IF_ICMPNE:
    case Const.IF_ICMPLE:
    case Const.IF_ICMPGE:
    case Const.IF_ICMPGT:
    case Const.IF_ICMPLT:
    case Const.IF_ACMPEQ:
    case Const.IF_ACMPNE:
    case Const.RETURN:
    case Const.ARETURN:
    case Const.IRETURN:
    case Const.MONITORENTER:
    case Const.MONITOREXIT:
    case Const.IINC:
    case Const.NEWARRAY:
    case Const.TABLESWITCH:
    case Const.LOOKUPSWITCH:
    case Const.LCMP:
    case Const.INEG:
    case Const.IADD:
    case Const.IMUL:
    case Const.ISUB:
    case Const.IDIV:
    case Const.IREM:
    case Const.IXOR:
    case Const.ISHL:
    case Const.ISHR:
    case Const.IUSHR:
    case Const.IAND:
    case Const.IOR:
    case Const.LAND:
    case Const.LOR:
    case Const.LADD:
    case Const.LMUL:
    case Const.LSUB:
    case Const.LDIV:
    case Const.LSHL:
    case Const.LSHR:
    case Const.LUSHR:
    case Const.AALOAD:
    case Const.AASTORE:
    case Const.IALOAD:
    case Const.IASTORE:
    case Const.BALOAD:
    case Const.BASTORE:
        hq.push(seen);
        priority = hq.getPriority();
        if (priority <= Priorities.LOW_PRIORITY) {
            accumulator.accumulateBug(
                    new BugInstance(this, "NOISE_OPERATION", priority).addClassAndMethod(this).addString(Const.getOpcodeName(seen)),
                    this);
        }
        break;
    default:
        break;
    }
}
 
Example 10
Source File: SynchronizeAndNullCheckField.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    // System.out.println(getPC() + " " + Const.getOpcodeName(seen) + " " +
    // currState);
    switch (currState) {
    case 0:
        if (seen == Const.GETFIELD || seen == Const.GETSTATIC) {
            syncField = FieldAnnotation.fromReferencedField(this);
            currState = 1;
        }
        break;
    case 1:
        if (seen == Const.DUP) {
            currState = 2;
        } else {
            currState = 0;
        }
        break;
    case 2:
        if (seen == Const.ASTORE || seen == Const.ASTORE_0 || seen == Const.ASTORE_1 || seen == Const.ASTORE_2 || seen == Const.ASTORE_3) {
            currState = 3;
        } else {
            currState = 0;
        }
        break;
    case 3:
        if (seen == Const.MONITORENTER) {
            currState = 4;
        } else {
            currState = 0;
        }
        break;
    case 4:
        if (seen == Const.GETFIELD || seen == Const.GETSTATIC) {
            gottenField = FieldAnnotation.fromReferencedField(this);
            currState = 5;
        } else {
            currState = 0;
        }
        break;
    case 5:
        if ((seen == Const.IFNONNULL || seen == Const.IFNULL) && gottenField.equals(syncField)) {
            BugInstance bug = new BugInstance(this, "NP_SYNC_AND_NULL_CHECK_FIELD", NORMAL_PRIORITY).addClass(this)
                    .addMethod(this).addField(syncField).addSourceLine(this);
            bugReporter.reportBug(bug);
        } else {
            currState = 0;
        }
        break;
    default:
        currState = 0;
    }
}
 
Example 11
Source File: FindNonShortCircuit.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void scanForBooleanValue(int seen) {
    switch (seen) {

    case Const.IAND:
    case Const.IOR:
        switch (prevOpcode) {
        case Const.ILOAD:
        case Const.ILOAD_0:
        case Const.ILOAD_1:
        case Const.ILOAD_2:
        case Const.ILOAD_3:
            clearAll();
            break;
        default:
            break;
        }
        break;
    case Const.ICONST_1:
        stage1 = 1;
        switch (prevOpcode) {
        case Const.IFNONNULL:
        case Const.IFNULL:
            sawNullTest = true;
            break;
        case Const.IF_ICMPGT:
        case Const.IF_ICMPGE:
        case Const.IF_ICMPLT:
        case Const.IF_ICMPLE:
            sawNumericTest = true;
            break;
        }

        break;
    case Const.GOTO:
        if (stage1 == 1) {
            stage1 = 2;
        } else {
            stage1 = 0;
            clearAll();
        }
        break;
    case Const.ICONST_0:
        if (stage1 == 2) {
            sawBooleanValue();
        }
        stage1 = 0;
        break;
    case Const.INVOKEINTERFACE:
    case Const.INVOKEVIRTUAL:
    case Const.INVOKESPECIAL:
    case Const.INVOKESTATIC:
        String sig = getSigConstantOperand();
        if (sig.endsWith(")Z")) {
            sawBooleanValue();
        }
        stage1 = 0;
        break;
    default:
        stage1 = 0;
    }
}
 
Example 12
Source File: NullDerefAndRedundantComparisonFinder.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
/** This is called for both IFNULL and IFNONNULL instructions. */
private void analyzeIfNullBranch(BasicBlock basicBlock, InstructionHandle lastHandle) throws DataflowAnalysisException {

    Location location = new Location(lastHandle, basicBlock);

    IsNullValueFrame frame = invDataflow.getFactAtLocation(location);

    if (!frame.isValid()) {
        // This is probably dead code due to an infeasible exception edge.
        return;
    }

    IsNullValue top = frame.getTopValue();

    // Find the line number.
    int lineNumber = getLineNumber(method, lastHandle);
    if (lineNumber < 0) {
        return;
    }

    if (!(top.isDefinitelyNull() || top.isDefinitelyNotNull())) {
        if (DEBUG) {
            System.out.println("Line " + lineNumber + " undetermined");
        }
        undeterminedBranchSet.set(lineNumber);
        return;
    }

    // Figure out if the branch is always taken
    // or always not taken.
    short opcode = lastHandle.getInstruction().getOpcode();
    boolean definitelySame = top.isDefinitelyNull();
    if (opcode != Const.IFNULL) {
        definitelySame = !definitelySame;
    }

    if (definitelySame) {
        if (DEBUG) {
            System.out.println("Line " + lineNumber + " always same");
        }
        definitelySameBranchSet.set(lineNumber);
    } else {
        if (DEBUG) {
            System.out.println("Line " + lineNumber + " always different");
        }
        definitelyDifferentBranchSet.set(lineNumber);
    }

    RedundantBranch redundantBranch = new RedundantBranch(location, lineNumber, top);

    // Determine which control edge is made infeasible by the redundant
    // comparison
    boolean wantNull = (opcode == Const.IFNULL);
    int infeasibleEdgeType = (wantNull == top.isDefinitelyNull()) ? EdgeTypes.FALL_THROUGH_EDGE : EdgeTypes.IFCMP_EDGE;
    Edge infeasibleEdge = invDataflow.getCFG().getOutgoingEdgeWithType(basicBlock, infeasibleEdgeType);
    redundantBranch.setInfeasibleEdge(infeasibleEdge);

    if (DEBUG) {
        System.out.println("Adding redundant branch: " + redundantBranch);
    }
    redundantBranchList.add(redundantBranch);
}
 
Example 13
Source File: InstructionFactory.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/** Create branch instruction by given opcode, except LOOKUPSWITCH and TABLESWITCH.
 * For those you should use the SWITCH compound instruction.
 */
public static BranchInstruction createBranchInstruction( final short opcode, final InstructionHandle target ) {
    switch (opcode) {
        case Const.IFEQ:
            return new IFEQ(target);
        case Const.IFNE:
            return new IFNE(target);
        case Const.IFLT:
            return new IFLT(target);
        case Const.IFGE:
            return new IFGE(target);
        case Const.IFGT:
            return new IFGT(target);
        case Const.IFLE:
            return new IFLE(target);
        case Const.IF_ICMPEQ:
            return new IF_ICMPEQ(target);
        case Const.IF_ICMPNE:
            return new IF_ICMPNE(target);
        case Const.IF_ICMPLT:
            return new IF_ICMPLT(target);
        case Const.IF_ICMPGE:
            return new IF_ICMPGE(target);
        case Const.IF_ICMPGT:
            return new IF_ICMPGT(target);
        case Const.IF_ICMPLE:
            return new IF_ICMPLE(target);
        case Const.IF_ACMPEQ:
            return new IF_ACMPEQ(target);
        case Const.IF_ACMPNE:
            return new IF_ACMPNE(target);
        case Const.GOTO:
            return new GOTO(target);
        case Const.JSR:
            return new JSR(target);
        case Const.IFNULL:
            return new IFNULL(target);
        case Const.IFNONNULL:
            return new IFNONNULL(target);
        case Const.GOTO_W:
            return new GOTO_W(target);
        case Const.JSR_W:
            return new JSR_W(target);
        default:
            throw new IllegalArgumentException("Invalid opcode: " + opcode);
    }
}