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

The following examples show how to use org.apache.bcel.Const#IFNE . 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: 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 2
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 3
Source File: FindNullDerefsInvolvingNonShortCircuitEvaluation.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.IAND || seen == Const.IOR) {

        int nextOpcode = getCodeByte(getPC() + 1);
        if (nextOpcode == Const.IFEQ || nextOpcode == Const.IFNE) {
            OpcodeStack.Item left = stack.getStackItem(1);
            OpcodeStack.Item right = stack.getStackItem(0);
            checkForNullForcingABranch(seen, nextOpcode, left);
            checkForNullForcingABranch(seen, nextOpcode, right);
        }

    }
}
 
Example 4
Source File: QuestionableBooleanAssignment.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (seen == Const.GOTO && getBranchOffset() == 4) {
        state = SEEN_GOTO;
    } else {
        switch (state) {
        case SEEN_NOTHING:
            if ((seen == Const.ICONST_1) || (seen == Const.ICONST_0)) {
                state = SEEN_ICONST_0_OR_1;
            }
            break;

        case SEEN_ICONST_0_OR_1:
            if (seen == Const.DUP) {
                state = SEEN_DUP;
            } else {
                state = SEEN_NOTHING;
            }
            break;

        case SEEN_DUP:
            if (((seen >= Const.ISTORE_0) && (seen <= Const.ISTORE_3)) || (seen == Const.ISTORE)) {
                state = SEEN_ISTORE;
            } else {
                state = SEEN_NOTHING;
            }
            break;

        case SEEN_ISTORE:
            if (seen == Const.IFEQ || seen == Const.IFNE) {
                bug = new BugInstance(this, "QBA_QUESTIONABLE_BOOLEAN_ASSIGNMENT", HIGH_PRIORITY).addClassAndMethod(this)
                        .addSourceLine(this);
                state = SEEN_IF;
            } else {
                state = SEEN_NOTHING;
            }
            break;

        case SEEN_IF:
            state = SEEN_NOTHING;
            if (seen == Const.NEW) {
                String cName = getClassConstantOperand();
                if ("java/lang/AssertionError".equals(cName)) {
                    break;
                }
            }
            bugReporter.reportBug(bug);
            break;
        case SEEN_GOTO:
            state = SEEN_NOTHING;
            break;
        default:
            break;
        }
    }
}
 
Example 5
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 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: FindFloatEquality.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    switch (seen) {
    case Const.FCMPG:
    case Const.FCMPL:
    case Const.DCMPG:
    case Const.DCMPL:
        if (stack.getStackDepth() >= 2) {
            OpcodeStack.Item first = stack.getStackItem(0);
            OpcodeStack.Item second = stack.getStackItem(1);

            if (first.getRegisterNumber() == second.getRegisterNumber() && first.getRegisterNumber() != -1) {
                break;
            }
            if (first.isInitialParameter() && second.isInitialParameter()) {
                break;
            }
            if (sameField(first, second)) {
                break;
            }

            Number n1 = (Number) first.getConstant();
            Number n2 = (Number) second.getConstant();
            if (n1 != null && Double.isNaN(n1.doubleValue()) || n2 != null && Double.isNaN(n2.doubleValue())) {
                BugInstance bug = new BugInstance(this, "FE_TEST_IF_EQUAL_TO_NOT_A_NUMBER", HIGH_PRIORITY)
                        .addClassAndMethod(this);
                bugAccumulator.accumulateBug(bug, this);
                state = SAW_NOTHING;
                break;
            }
            if (first.getSpecialKind() == OpcodeStack.Item.NASTY_FLOAT_MATH && !isZero(n2)
                    || second.getSpecialKind() == OpcodeStack.Item.NASTY_FLOAT_MATH && !isZero(n1)
                    || first.getSpecialKind() == OpcodeStack.Item.FLOAT_MATH && !okValueToCompareAgainst(n2)
                    || second.getSpecialKind() == OpcodeStack.Item.FLOAT_MATH && !okValueToCompareAgainst(n1)) {
                if (priority != HIGH_PRIORITY) {
                    found.clear();
                }
                priority = HIGH_PRIORITY;
                state = SAW_COMP;
                break;
            }
            if (priority == HIGH_PRIORITY) {
                break;
            }
            // if (first.isInitialParameter() && n2 != null) break;
            // if (second.isInitialParameter() && n1 != null) break;
            if (n1 != null && n2 != null) {
                break;
            }

            if (okValueToCompareAgainst(n1) || okValueToCompareAgainst(n2)) {
                break;
            }
            if (n1 != null && !second.isInitialParameter() || n2 != null && !first.isInitialParameter()) {
                if (priority == LOW_PRIORITY) {
                    found.clear();
                }
                priority = NORMAL_PRIORITY;

            } else if (priority == NORMAL_PRIORITY) {
                break;
            }
            state = SAW_COMP;
        }
        break;

    case Const.IFEQ:
    case Const.IFNE:
        if (state == SAW_COMP) {
            SourceLineAnnotation sourceLineAnnotation = SourceLineAnnotation.fromVisitedInstruction(getClassContext(), this,
                    getPC());
            if (sourceLineAnnotation != null) {
                found.add(sourceLineAnnotation);
            }
        }
        state = SAW_NOTHING;
        break;

    default:
        state = SAW_NOTHING;
        break;
    }
}
 
Example 8
Source File: FindNonShortCircuit.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void scanForShortCircuit(int seen) {
    switch (seen) {
    case Const.IAND:
    case Const.IOR:

        // System.out.println("Saw IOR or IAND at distance " + distance);
        OpcodeStack.Item item0 = stack.getStackItem(0);
        OpcodeStack.Item item1 = stack.getStackItem(1);
        if (item0.getConstant() == null && item1.getConstant() == null && distance < 4) {
            //                if (item0.getRegisterNumber() >= 0 && item1.getRegisterNumber() >= 0) {
            //                    if (false) {
            //                        clearAll();
            //                    }
            //                }
            operator = seen;
            stage2 = 1;
        } else {
            stage2 = 0;
        }
        break;
    case Const.IFEQ:
    case Const.IFNE:
        if (stage2 == 1) {
            // System.out.println("Found nsc");
            reportBug();
        }
        stage2 = 0;
        break;
    case Const.PUTFIELD:
    case Const.PUTSTATIC:
    case Const.IRETURN:
        if (operator == Const.IAND && stage2 == 1) {
            reportBug();
        }
        stage2 = 0;
        break;
    default:
        stage2 = 0;
        break;
    }
}
 
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);
    }
}