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

The following examples show how to use org.apache.bcel.Const#IRETURN . 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: ReturnInstruction.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
public Type getType() {
    final short _opcode = super.getOpcode();
    switch (_opcode) {
        case Const.IRETURN:
            return Type.INT;
        case Const.LRETURN:
            return Type.LONG;
        case Const.FRETURN:
            return Type.FLOAT;
        case Const.DRETURN:
            return Type.DOUBLE;
        case Const.ARETURN:
            return Type.OBJECT;
        case Const.RETURN:
            return Type.VOID;
        default: // Never reached
            throw new ClassGenException("Unknown type " + _opcode);
    }
}
 
Example 2
Source File: DismantleBytecode.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public boolean isReturn(int opcode) {
    switch (opcode) {
    case Const.IRETURN:
    case Const.ARETURN:
    case Const.LRETURN:
    case Const.DRETURN:
    case Const.FRETURN:
    case Const.RETURN:
        return true;
    default:
        return false;
    }
}
 
Example 3
Source File: FindComparatorProblems.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (getStack().getStackDepth() == 0) {
        this.lastEmptyStackPC = getPC();
    }
    if ((seen == Const.DCMPG || seen == Const.DCMPL || seen == Const.FCMPL || seen == Const.FCMPG) && getStack().getStackDepth() == 2) {
        int[] startEnd = new int[] { this.lastEmptyStackPC, getPC() };
        for (Iterator<int[]> iterator = twoDoublesInStack.iterator(); iterator.hasNext();) {
            int[] oldStartEnd = iterator.next();
            if (codeEquals(oldStartEnd, startEnd)) {
                Item item1 = getStack().getStackItem(0);
                Item item2 = getStack().getStackItem(1);
                accumulator.accumulateBug(
                        new BugInstance("CO_COMPARETO_INCORRECT_FLOATING", NORMAL_PRIORITY).addClassAndMethod(this)
                                .addType(item1.getSignature())
                                .addMethod(item1.getSignature().equals("D") ? DOUBLE_DESCRIPTOR : FLOAT_DESCRIPTOR).describe(
                                        MethodAnnotation.SHOULD_CALL)
                                .addValueSource(item1, this)
                                .addValueSource(item2, this), this);
                iterator.remove();
                return;
            }
        }
        twoDoublesInStack.add(startEnd);
    }
    if (seen == Const.IRETURN) {
        OpcodeStack.Item top = stack.getStackItem(0);
        Object o = top.getConstant();
        if (o instanceof Integer && ((Integer) o).intValue() == Integer.MIN_VALUE) {
            accumulator.accumulateBug(
                    new BugInstance(this, "CO_COMPARETO_RESULTS_MIN_VALUE", NORMAL_PRIORITY).addClassAndMethod(this), this);
        }
    }
}
 
Example 4
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 5
Source File: UselessSubclassMethod.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    switch (state) {
    case SEEN_NOTHING:
        if (seen == Const.ALOAD_0) {
            argTypes = Type.getArgumentTypes(this.getMethodSig());
            curParm = 0;
            curParmOffset = 1;
            if (argTypes.length > 0) {
                state = State.SEEN_PARM;
            } else {
                state = State.SEEN_LAST_PARM;
            }
        } else {
            state = State.SEEN_INVALID;
        }
        break;

    case SEEN_PARM:
        if (curParm >= argTypes.length) {
            state = State.SEEN_INVALID;
        } else {
            String signature = argTypes[curParm++].getSignature();
            char typeChar0 = signature.charAt(0);
            if ((typeChar0 == 'L') || (typeChar0 == '[')) {
                checkParm(seen, Const.ALOAD_0, Const.ALOAD, 1);
            } else if (typeChar0 == 'D') {
                checkParm(seen, Const.DLOAD_0, Const.DLOAD, 2);
            } else if (typeChar0 == 'F') {
                checkParm(seen, Const.FLOAD_0, Const.FLOAD, 1);
            } else if (typeChar0 == 'I' || typeChar0 == 'Z' || typeChar0 == 'S' || typeChar0 == 'C') {
                checkParm(seen, Const.ILOAD_0, Const.ILOAD, 1);
            } else if (typeChar0 == 'J') {
                checkParm(seen, Const.LLOAD_0, Const.LLOAD, 2);
            } else {
                state = State.SEEN_INVALID;
            }

            if ((state != State.SEEN_INVALID) && (curParm >= argTypes.length)) {
                state = State.SEEN_LAST_PARM;
            }

        }
        break;

    case SEEN_LAST_PARM:
        if ((seen == Const.INVOKENONVIRTUAL) && getMethodName().equals(getNameConstantOperand())
                && getMethodSig().equals(getSigConstantOperand())) {
            invokePC = getPC();
            state = State.SEEN_INVOKE;
        } else {
            state = State.SEEN_INVALID;
        }
        break;

    case SEEN_INVOKE:
        Type returnType = getMethod().getReturnType();
        char retSigChar0 = returnType.getSignature().charAt(0);
        if ((retSigChar0 == 'V') && (seen == Const.RETURN)) {
            state = State.SEEN_RETURN;
        } else if (((retSigChar0 == 'L') || (retSigChar0 == '[')) && (seen == Const.ARETURN)) {
            state = State.SEEN_RETURN;
        } else if ((retSigChar0 == 'D') && (seen == Const.DRETURN)) {
            state = State.SEEN_RETURN;
        } else if ((retSigChar0 == 'F') && (seen == Const.FRETURN)) {
            state = State.SEEN_RETURN;
        } else if ((retSigChar0 == 'I' || retSigChar0 == 'S' || retSigChar0 == 'C' || retSigChar0 == 'B' || retSigChar0 == 'Z')
                && (seen == Const.IRETURN)) {
            state = State.SEEN_RETURN;
        } else if ((retSigChar0 == 'J') && (seen == Const.LRETURN)) {
            state = State.SEEN_RETURN;
        } else {
            state = State.SEEN_INVALID;
        }
        break;

    case SEEN_RETURN:
        state = State.SEEN_INVALID;
        break;
    default:
        break;
    }
}
 
Example 6
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 7
Source File: MethodGen.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * Computes stack usage of an instruction list by performing control flow analysis.
 *
 * @return maximum stack depth used by method
 */
public static int getMaxStack( final ConstantPoolGen cp, final InstructionList il, final CodeExceptionGen[] et ) {
    final BranchStack branchTargets = new BranchStack();
    /* Initially, populate the branch stack with the exception
     * handlers, because these aren't (necessarily) branched to
     * explicitly. in each case, the stack will have depth 1,
     * containing the exception object.
     */
    for (final CodeExceptionGen element : et) {
        final InstructionHandle handler_pc = element.getHandlerPC();
        if (handler_pc != null) {
            branchTargets.push(handler_pc, 1);
        }
    }
    int stackDepth = 0;
    int maxStackDepth = 0;
    InstructionHandle ih = il.getStart();
    while (ih != null) {
        final Instruction instruction = ih.getInstruction();
        final short opcode = instruction.getOpcode();
        final int delta = instruction.produceStack(cp) - instruction.consumeStack(cp);
        stackDepth += delta;
        if (stackDepth > maxStackDepth) {
            maxStackDepth = stackDepth;
        }
        // choose the next instruction based on whether current is a branch.
        if (instruction instanceof BranchInstruction) {
            final BranchInstruction branch = (BranchInstruction) instruction;
            if (instruction instanceof Select) {
                // explore all of the select's targets. the default target is handled below.
                final Select select = (Select) branch;
                final InstructionHandle[] targets = select.getTargets();
                for (final InstructionHandle target : targets) {
                    branchTargets.push(target, stackDepth);
                }
                // nothing to fall through to.
                ih = null;
            } else if (!(branch instanceof IfInstruction)) {
                // if an instruction that comes back to following PC,
                // push next instruction, with stack depth reduced by 1.
                if (opcode == Const.JSR || opcode == Const.JSR_W) {
                    branchTargets.push(ih.getNext(), stackDepth - 1);
                }
                ih = null;
            }
            // for all branches, the target of the branch is pushed on the branch stack.
            // conditional branches have a fall through case, selects don't, and
            // jsr/jsr_w return to the next instruction.
            branchTargets.push(branch.getTarget(), stackDepth);
        } else {
            // check for instructions that terminate the method.
            if (opcode == Const.ATHROW || opcode == Const.RET
                    || (opcode >= Const.IRETURN && opcode <= Const.RETURN)) {
                ih = null;
            }
        }
        // normal case, go to the next instruction.
        if (ih != null) {
            ih = ih.getNext();
        }
        // if we have no more instructions, see if there are any deferred branches to explore.
        if (ih == null) {
            final BranchTarget bt = branchTargets.pop();
            if (bt != null) {
                ih = bt.target;
                stackDepth = bt.stackDepth;
            }
        }
    }
    return maxStackDepth;
}