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

The following examples show how to use org.apache.bcel.Const#IF_ACMPNE . 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: ClassContext.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
static boolean checkForBranchExit(byte[] codeBytes, int pos) {
    if (pos < 0 || pos + 2 >= codeBytes.length) {
        return false;
    }
    switch (0xff & codeBytes[pos]) {
    case Const.IF_ACMPEQ:
    case Const.IF_ACMPNE:
    case Const.IF_ICMPEQ:
    case Const.IF_ICMPGE:
    case Const.IF_ICMPGT:
    case Const.IF_ICMPLE:
    case Const.IF_ICMPLT:
    case Const.IF_ICMPNE:
        break;
    default:
        return false;
    }
    int branchTarget = pos + getBranchOffset(codeBytes, pos + 1);
    if (branchTarget - 3 < pos
            || branchTarget >= codeBytes.length
            || (codeBytes[branchTarget - 3] & 0xff) != Const.GOTO) {
        return false;
    }
    int backBranchTarget = branchTarget + getBranchOffset(codeBytes, branchTarget - 2);
    return (backBranchTarget <= pos && backBranchTarget + 12 >= pos);
}
 
Example 2
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 3
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 4
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 5
Source File: ObligationAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Type acmpNullCheck(short opcode, Edge edge, InstructionHandle last, BasicBlock sourceBlock)
        throws DataflowAnalysisException {
    Type type = null;
    //
    // Make sure that IF a value has been compared to null,
    // this edge is the edge on which the
    // compared value is definitely null.
    //
    if ((opcode == Const.IF_ACMPEQ && edge.getType() == EdgeTypes.IFCMP_EDGE)
            || (opcode == Const.IF_ACMPNE && edge.getType() == EdgeTypes.FALL_THROUGH_EDGE)) {
        //
        // Check nullness and type of the top two stack values.
        //
        Location location = new Location(last, sourceBlock);
        IsNullValueFrame invFrame = invDataflow.getFactAtLocation(location);
        TypeFrame typeFrame = typeDataflow.getFactAtLocation(location);
        if (invFrame.isValid() && typeFrame.isValid()) {
            //
            // See if exactly one of the top two stack values is definitely
            // null
            //
            boolean leftIsNull = invFrame.getStackValue(1).isDefinitelyNull();
            boolean rightIsNull = invFrame.getStackValue(0).isDefinitelyNull();

            if ((leftIsNull || rightIsNull) && !(leftIsNull && rightIsNull)) {
                //
                // Now we can determine what type was compared to null.
                //
                type = typeFrame.getStackValue(leftIsNull ? 0 : 1);
                if (DEBUG_NULL_CHECK) {
                    System.out.println("acmp comparison of " + type + " to null at " + last);
                }
            }
        }
    }
    return type;
}
 
Example 6
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 7
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 8
Source File: FindRefComparison.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void inspectLocation(JavaClass jclass, ConstantPoolGen cpg, Method method, MethodGen methodGen,
        LinkedList<WarningWithProperties> refComparisonList, LinkedList<WarningWithProperties> stringComparisonList,
        RefComparisonTypeFrameModelingVisitor visitor, TypeDataflow typeDataflow, Location location)
        throws DataflowAnalysisException {
    Instruction ins = location.getHandle().getInstruction();
    short opcode = ins.getOpcode();
    if (opcode == Const.IF_ACMPEQ || opcode == Const.IF_ACMPNE) {
        checkRefComparison(location, jclass, method, methodGen, visitor, typeDataflow, stringComparisonList,
                refComparisonList);
    } else if (ins instanceof InvokeInstruction) {
        InvokeInstruction inv = (InvokeInstruction) ins;
        boolean isStatic = inv instanceof INVOKESTATIC;
        @DottedClassName
        String className = inv.getClassName(cpg);
        String methodName = inv.getMethodName(cpg);
        String methodSig = inv.getSignature(cpg);
        if ("assertSame".equals(methodName) && "(Ljava/lang/Object;Ljava/lang/Object;)V".equals(methodSig)) {
            checkRefComparison(location, jclass, method, methodGen, visitor, typeDataflow, stringComparisonList,
                    refComparisonList);
        } else if ("assertFalse".equals(methodName) && "(Z)V".equals(methodSig)) {
            SourceLineAnnotation lastLocation = bugAccumulator.getLastBugLocation();
            InstructionHandle prevHandle = location.getHandle().getPrev();
            if (lastLocation != null && prevHandle != null && lastLocation.getEndBytecode() == prevHandle.getPosition()) {
                bugAccumulator.forgetLastBug();
                if (DEBUG) {
                    System.out.println("Forgetting last bug due to call to " + className + "." + methodName);
                }
            }

        } else {
            boolean equalsMethod = !isStatic && "equals".equals(methodName) && "(Ljava/lang/Object;)Z".equals(methodSig)
                    || isStatic && "assertEquals".equals(methodName)
                            && "(Ljava/lang/Object;Ljava/lang/Object;)V".equals(methodSig)
                    || isStatic && "equal".equals(methodName) && "(Ljava/lang/Object;Ljava/lang/Object;)Z".equals(methodSig)
                            && "com.google.common.base.Objects".equals(className)
                    || isStatic && "equals".equals(methodName) && "(Ljava/lang/Object;Ljava/lang/Object;)Z".equals(methodSig)
                            && "java.util.Objects".equals(className);

            if (equalsMethod) {
                checkEqualsComparison(location, jclass, method, methodGen, cpg, typeDataflow);
            }
        }
    }

}
 
Example 9
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);
    }
}