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

The following examples show how to use org.apache.bcel.Const#LDC . 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: FindRoughConstants.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.LDC || seen == Const.LDC_W || seen == Const.LDC2_W) {
        Constant c = getConstantRefOperand();
        if (c instanceof ConstantFloat) {
            checkConst(((ConstantFloat) c).getBytes());
        } else if (c instanceof ConstantDouble) {
            checkConst(((ConstantDouble) c).getBytes());
        }
        return;
    }
    // Lower priority if the constant is put into array immediately or after the boxing:
    // this is likely to be just similar number in some predefined dataset (like lookup table)
    if (seen == Const.INVOKESTATIC && lastBug != null) {
        if (getNextOpcode() == Const.AASTORE
                && getNameConstantOperand().equals("valueOf")
                && (getClassConstantOperand().equals("java/lang/Double") || getClassConstantOperand().equals(
                        "java/lang/Float"))) {
            lastBug = ((BugInstance) lastBug.clone());
            lastBug.setPriority(lastPriority + 1);
            bugAccumulator.forgetLastBug();
            bugAccumulator.accumulateBug(lastBug, this);
        }
    }
    lastBug = null;
}
 
Example 2
Source File: UnconditionalValueDerefAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
private static boolean check(InstructionHandle h, int[] opcodes) {
    for (int opcode : opcodes) {
        if (h == null) {
            return false;
        }
        short opcode2 = h.getInstruction().getOpcode();
        if (opcode == Const.LDC) {
            switch (opcode2) {
            case Const.LDC:
            case Const.ALOAD:
            case Const.ALOAD_0:
            case Const.ALOAD_1:
            case Const.ALOAD_2:
            case Const.ALOAD_3:
                break;
            default:
                return false;
            }
        } else if (opcode2 != opcode) {
            return false;
        }
        h = h.getNext();
    }
    return true;
}
 
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: ReflectiveClasses.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void sawClass() {
    int opcode = getOpcode();
    if ((opcode == Const.LDC) || (opcode == Const.LDC_W)) {
        process(getClassConstantOperand());
    }
}
 
Example 5
Source File: InheritanceUnsafeGetResource.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (reportedForThisClass) {
        return;
    }

    switch (seen) {
    case Const.LDC:
        Constant constantValue = getConstantRefOperand();
        if (constantValue instanceof ConstantClass) {
            sawGetClass = -100;
        } else if (constantValue instanceof ConstantString) {
            stringConstant = ((ConstantString) constantValue).getBytes(getConstantPool());
        }
        break;

    case Const.ALOAD_0:
        state = 1;
        break;
    case Const.INVOKEVIRTUAL:
        if ("java/lang/Class".equals(getClassConstantOperand())
                && ("getResource".equals(getNameConstantOperand()) || "getResourceAsStream".equals(getNameConstantOperand()))
                && sawGetClass + 10 >= getPC()) {
            int priority = NORMAL_PRIORITY;
            if (prevOpcode == Const.LDC && stringConstant != null && stringConstant.length() > 0 && stringConstant.charAt(0) == '/') {
                priority = LOW_PRIORITY;
            } else {
                priority = adjustPriority(priority);
            }
            bugReporter.reportBug(new BugInstance(this, "UI_INHERITANCE_UNSAFE_GETRESOURCE", priority)
                    .addClassAndMethod(this).addSourceLine(this));
            reportedForThisClass = true;

        } else if (state == 1 && !methodIsStatic && !classIsFinal && classIsVisibleToOtherPackages
                && "getClass".equals(getNameConstantOperand()) && "()Ljava/lang/Class;".equals(getSigConstantOperand())) {
            sawGetClass = getPC();
        }
        state = 0;
        break;
    default:
        state = 0;
        break;
    }
    if (seen != Const.LDC) {
        stringConstant = null;
    }
    prevOpcode = seen;

}