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

The following examples show how to use org.apache.bcel.Const#GOTO . 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: 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: 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 5
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 6
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 7
Source File: SynchronizingOnContentsOfFieldToProtectField.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(state + " " + getPC() + " " + Const.getOpcodeName(seen));
    if (countDown == 2 && seen == Const.GOTO) {
        CodeException tryBlock = getSurroundingTryBlock(getPC());
        if (tryBlock != null && tryBlock.getEndPC() == getPC()) {
            pendingBug.lowerPriority();
        }
    }
    if (countDown > 0) {
        countDown--;
        if (countDown == 0) {
            if (seen == Const.MONITOREXIT) {
                pendingBug.lowerPriority();
            }

            bugReporter.reportBug(pendingBug);
            pendingBug = null;
        }
    }
    if (seen == Const.PUTFIELD) {

        if (syncField != null && getPrevOpcode(1) != Const.ALOAD_0 && syncField.equals(getXFieldOperand())) {
            OpcodeStack.Item value = stack.getStackItem(0);
            int priority = Priorities.HIGH_PRIORITY;
            if (value.isNull()) {
                priority = Priorities.NORMAL_PRIORITY;
            }
            pendingBug = new BugInstance(this, "ML_SYNC_ON_FIELD_TO_GUARD_CHANGING_THAT_FIELD", priority)
                    .addClassAndMethod(this).addField(syncField).addSourceLine(this);
            countDown = 2;

        }

    }
    if (seen == Const.MONITOREXIT) {
        pendingBug = null;
        countDown = 0;
    }

    if (seen == Const.MONITORENTER) {
        syncField = null;
    }

    switch (state) {
    case 0:
        if (seen == Const.ALOAD_0) {
            state = 1;
        }
        break;
    case 1:
        if (seen == Const.GETFIELD) {
            state = 2;
            field = getXFieldOperand();
        } else {
            state = 0;
        }
        break;
    case 2:
        if (seen == Const.DUP) {
            state = 3;
        } else {
            state = 0;
        }
        break;
    case 3:
        if (isRegisterStore()) {
            state = 4;
        } else {
            state = 0;
        }
        break;
    case 4:
        if (seen == Const.MONITORENTER) {
            state = 0;
            syncField = field;
        } else {
            state = 0;
        }
        break;
    default:
        break;
    }

}
 
Example 8
Source File: FindLocalSelfAssignment2.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) {
        previousGotoTarget = getBranchTarget();
        gotoCount++;
        if (previousGotoTarget < getPC()) {
            previousLoadOf = -1;
        }
    } else {
        if (isRegisterLoad()) {
            previousLoadOf = getRegisterOperand();
        } else {
            if (isRegisterStore()) {
                if (previousLoadOf == getRegisterOperand() && gotoCount < 2 && getPC() != previousGotoTarget) {
                    int priority = NORMAL_PRIORITY;
                    String methodName = getMethodName();
                    if (Const.CONSTRUCTOR_NAME.equals(methodName) || methodName.startsWith("set") && getCode().getCode().length <= 5
                            || !previousStores.get(getRegisterOperand())) {
                        priority = HIGH_PRIORITY;
                    }
                    previousStores.set(getRegisterOperand());
                    XClass c = getXClass();
                    LocalVariableAnnotation local = LocalVariableAnnotation.getLocalVariableAnnotation(getMethod(),
                            getRegisterOperand(), getPC(), getPC());
                    if ("?".equals(local.getName())) {
                        priority++;
                    } else {
                        for (XField f : c.getXFields()) {
                            if (f.getName().equals(local.getName()) && (f.isStatic() || !getMethod().isStatic())) {
                                bugReporter.reportBug(new BugInstance(this, "SA_LOCAL_SELF_ASSIGNMENT_INSTEAD_OF_FIELD",
                                        priority).addClassAndMethod(this).add(local).addField(f)
                                                .describe(FieldAnnotation.DID_YOU_MEAN_ROLE).addSourceLine(this));
                                return;

                            }
                        }
                    }

                    bugReporter.reportBug(new BugInstance(this, "SA_LOCAL_SELF_ASSIGNMENT", priority).addClassAndMethod(this)
                            .add(local).addSourceLine(this));
                } else {
                    previousStores.set(getRegisterOperand());
                }
            }

            previousLoadOf = -1;
            gotoCount = 0;
        }
    }
}
 
Example 9
Source File: VarArgsProblems.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("State:" + state);
    if (seen == Const.GOTO && getBranchOffset() == 4) {
        state = SEEN_GOTO;
    } else {
        switch (state) {
        case SEEN_NOTHING:
            if ((seen == Const.ICONST_1)) {
                state = SEEN_ICONST_1;
            }
            break;

        case SEEN_ICONST_1:
            if (seen == Const.ANEWARRAY && primitiveArray.matcher(getClassConstantOperand()).matches()) {
                // System.out.println("Allocation of array of type " +
                // getClassConstantOperand());
                primitiveArraySig = getClassConstantOperand();
                state = SEEN_ANEWARRAY;
            } else {
                state = SEEN_NOTHING;
            }
            break;

        case SEEN_ANEWARRAY:
            if (seen == Const.DUP) {
                state = SEEN_DUP;
            } else {
                state = SEEN_NOTHING;
            }
            break;
        case SEEN_DUP:
            if (seen == Const.ICONST_0) {
                state = SEEN_ICONST_0;
            } else {
                state = SEEN_NOTHING;
            }
            break;
        case SEEN_ICONST_0:
            if (((seen >= Const.ALOAD_0) && (seen < Const.ALOAD_3)) || (seen == Const.ALOAD)) {
                state = SEEN_ALOAD;
            } else {
                state = SEEN_NOTHING;
            }
            break;

        case SEEN_ALOAD:
            if (seen == Const.AASTORE) {
                state = SEEN_AASTORE;
            } else {
                state = SEEN_NOTHING;
            }
            break;

        case SEEN_AASTORE:
            if (seen == Const.INVOKESTATIC || seen == Const.INVOKEINTERFACE || seen == Const.INVOKESPECIAL || seen == Const.INVOKEVIRTUAL) {
                // System.out.println(getClassConstantOperand());
                // System.out.println(getNameConstantOperand());
                // System.out.println(getSigConstantOperand());
                if (getSigConstantOperand().indexOf("Ljava/lang/Object;)") == -1) {
                    break;
                }
                int priority = NORMAL_PRIORITY;
                if ("asList".equals(getNameConstantOperand()) && "java/util/Arrays".equals(getClassConstantOperand())) {
                    priority = HIGH_PRIORITY;
                }
                bugReporter.reportBug(new BugInstance(this, "VA_PRIMITIVE_ARRAY_PASSED_TO_OBJECT_VARARG", priority)
                        .addClassAndMethod(this).addType(primitiveArraySig).describe(TypeAnnotation.FOUND_ROLE)
                        .addCalledMethod(this).addSourceLine(this));
            }
            state = SEEN_NOTHING;
            break;

        case SEEN_GOTO:
            state = SEEN_NOTHING;
            break;
        default:
            throw new IllegalStateException("State " + state + " not expected");

        }
    }
}
 
Example 10
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 11
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);
    }
}
 
Example 12
Source File: FindNullDeref.java    From spotbugs with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Determine whether or not given instruction is a goto.
 *
 * @param instruction
 *            the instruction
 * @return true if the instruction is a goto, false otherwise
 */
private boolean isGoto(Instruction instruction) {
    return instruction.getOpcode() == Const.GOTO || instruction.getOpcode() == Const.GOTO_W;
}