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

The following examples show how to use org.apache.bcel.Const#IF_ICMPNE . 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: CheckAnalysisContextContainedAnnotation.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    switch (seen) {
    default:
        break;
    case Const.IF_ICMPEQ:
    case Const.IF_ICMPNE:
        OpcodeStack.Item left = stack.getStackItem(1);
        OpcodeStack.Item right = stack.getStackItem(0);
        if (bad(left, right) || bad(right, left)) {
            accumulator.accumulateBug(new BugInstance(this, "TESTING", NORMAL_PRIORITY).addClassAndMethod(this)
                    .addValueSource(left, this).addValueSource(right, this)
                    .addString("Just check the sign of the result of compare or compareTo, not specific values such as 1 or -1"), this);
        }
        break;
    }

}
 
Example 2
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 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: FindBadForLoop.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.ISTORE || seen == Const.ISTORE_0 || seen == Const.ISTORE_1 || seen == Const.ISTORE_2 || seen == Const.ISTORE_3) {
        lastRegStore = getRegisterOperand();
    }
    if (lineNumbers != null
            && stack.getStackDepth() >= 2
            && (seen == Const.IF_ICMPGE || seen == Const.IF_ICMPGT || seen == Const.IF_ICMPLT || seen == Const.IF_ICMPLE
                    || seen == Const.IF_ICMPNE || seen == Const.IF_ICMPEQ)) {
        OpcodeStack.Item item0 = stack.getStackItem(0);
        OpcodeStack.Item item1 = stack.getStackItem(1);
        int r0 = item0.getRegisterNumber();
        int r1 = item1.getRegisterNumber();
        int rMin = Math.min(r0, r1);
        int rMax = Math.max(r0, r1);
        int branchTarget = getBranchTarget();
        if (rMin == -1 && rMax > 0 && rMax == lastRegStore && branchTarget - 6 > getPC()) {
            int beforeTarget = getCodeByte(branchTarget - 3);
            int beforeGoto = getCodeByte(branchTarget - 6);
            if (beforeTarget == Const.GOTO && beforeGoto == Const.IINC) {
                int offset1 = (byte) getCodeByte(branchTarget - 2);
                int offset2 = getCodeByte(branchTarget - 1);
                int offset = offset1 << 8 | offset2;
                int backTarget = branchTarget - 3 + offset;
                int reg = getCodeByte(branchTarget - 5);
                int testLineNumber = lineNumbers.getSourceLine(getPC());
                int incLineNumber = lineNumbers.getSourceLine(branchTarget - 6);
                int beforeIncLineNumber = lineNumbers.getSourceLine(branchTarget - 7);
                if (backTarget < getPC() && getPC() - 8 < backTarget && reg != rMax && incLineNumber < testLineNumber + 3
                        && beforeIncLineNumber > incLineNumber) {

                    bugReporter.reportBug(new BugInstance(this, "QF_QUESTIONABLE_FOR_LOOP", NORMAL_PRIORITY)
                            .addClassAndMethod(this).addSourceLine(this));
                }
            }

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