Search in sources :

Example 6 with ConstantPool

use of org.apache.bcel.classfile.ConstantPool in project fb-contrib by mebigfatguy.

the class SillynessPotPourri method checkNotEqualsStringBuilderLength.

private void checkNotEqualsStringBuilderLength() {
    byte[] bytes = getCode().getCode();
    if ((lastPCs[2] != -1) && (CodeByteUtils.getbyte(bytes, lastPCs[3]) == Const.INVOKEVIRTUAL) && (CodeByteUtils.getbyte(bytes, lastPCs[2]) == Const.INVOKEVIRTUAL)) {
        ConstantPool pool = getConstantPool();
        int toStringIndex = CodeByteUtils.getshort(bytes, lastPCs[2] + 1);
        ConstantMethodref toStringMR = (ConstantMethodref) pool.getConstant(toStringIndex);
        String toStringCls = toStringMR.getClass(pool);
        if (toStringCls.startsWith("java.lang.StringBu")) {
            int nandtIndex = toStringMR.getNameAndTypeIndex();
            ConstantNameAndType cnt = (ConstantNameAndType) pool.getConstant(nandtIndex);
            if (Values.TOSTRING.equals(cnt.getName(pool))) {
                int lengthIndex = CodeByteUtils.getshort(bytes, lastPCs[3] + 1);
                ConstantMethodref lengthMR = (ConstantMethodref) pool.getConstant(lengthIndex);
                nandtIndex = lengthMR.getNameAndTypeIndex();
                cnt = (ConstantNameAndType) pool.getConstant(nandtIndex);
                if ("length".equals(cnt.getName(pool))) {
                    bugReporter.reportBug(new BugInstance(this, BugType.SPP_USE_STRINGBUILDER_LENGTH.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                }
            }
        }
    }
}
Also used : ConstantMethodref(org.apache.bcel.classfile.ConstantMethodref) ConstantPool(org.apache.bcel.classfile.ConstantPool) BugInstance(edu.umd.cs.findbugs.BugInstance) ToString(com.mebigfatguy.fbcontrib.utils.ToString) ConstantString(org.apache.bcel.classfile.ConstantString) ConstantNameAndType(org.apache.bcel.classfile.ConstantNameAndType)

Example 7 with ConstantPool

use of org.apache.bcel.classfile.ConstantPool in project fb-contrib by mebigfatguy.

the class SillynessPotPourri method checkForEmptyStringAndNullChecks.

private void checkForEmptyStringAndNullChecks(int seen) {
    if (lastLoadWasString && (lastPCs[0] != -1)) {
        byte[] bytes = getCode().getCode();
        int loadIns = CodeByteUtils.getbyte(bytes, lastPCs[2]);
        if ((((loadIns >= Const.ALOAD_0) && (loadIns <= Const.ALOAD_3)) || (loadIns == Const.ALOAD)) && (CodeByteUtils.getbyte(bytes, lastPCs[3]) == Const.INVOKEVIRTUAL) && (CodeByteUtils.getbyte(bytes, lastPCs[2]) == loadIns) && (CodeByteUtils.getbyte(bytes, lastPCs[1]) == Const.IFNULL) && (CodeByteUtils.getbyte(bytes, lastPCs[0]) == loadIns) && ((loadIns != Const.ALOAD) || (CodeByteUtils.getbyte(bytes, lastPCs[2] + 1) == CodeByteUtils.getbyte(bytes, lastPCs[0] + 1)))) {
            int brOffset = (loadIns == Const.ALOAD) ? 11 : 10;
            if ((seen == Const.IFNE) ? CodeByteUtils.getshort(bytes, lastPCs[1] + 1) > brOffset : CodeByteUtils.getshort(bytes, lastPCs[1] + 1) == brOffset) {
                int nextOp = CodeByteUtils.getbyte(bytes, getNextPC());
                if ((nextOp != Const.GOTO) && (nextOp != Const.GOTO_W)) {
                    ConstantPool pool = getConstantPool();
                    int mpoolIndex = CodeByteUtils.getshort(bytes, lastPCs[3] + 1);
                    ConstantMethodref cmr = (ConstantMethodref) pool.getConstant(mpoolIndex);
                    int nandtIndex = cmr.getNameAndTypeIndex();
                    ConstantNameAndType cnt = (ConstantNameAndType) pool.getConstant(nandtIndex);
                    if ("length".equals(cnt.getName(pool))) {
                        bugReporter.reportBug(new BugInstance(this, BugType.SPP_SUSPECT_STRING_TEST.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                    }
                }
            }
        }
    }
}
Also used : ConstantMethodref(org.apache.bcel.classfile.ConstantMethodref) ConstantPool(org.apache.bcel.classfile.ConstantPool) BugInstance(edu.umd.cs.findbugs.BugInstance) ConstantNameAndType(org.apache.bcel.classfile.ConstantNameAndType)

Example 8 with ConstantPool

use of org.apache.bcel.classfile.ConstantPool in project fb-contrib by mebigfatguy.

the class RegisterUtilsTest method shouldGetParameterRegisters.

@Test(dataProvider = "parameterRegisters")
public void shouldGetParameterRegisters(int accessFlags, String signature, int[] expected) {
    Method method = new Method();
    method.setAccessFlags(accessFlags);
    Constant[] cnst = new Constant[] { new ConstantUtf8(signature) };
    ConstantPool cp = new ConstantPool(cnst) {

        @Override
        protected Object clone() throws CloneNotSupportedException {
            throw new CloneNotSupportedException();
        }
    };
    method.setConstantPool(cp);
    method.setSignatureIndex(0);
    int[] regs = RegisterUtils.getParameterRegisters(method);
    assertEquals(regs, expected);
}
Also used : Constant(org.apache.bcel.classfile.Constant) ConstantPool(org.apache.bcel.classfile.ConstantPool) Method(org.apache.bcel.classfile.Method) BeforeMethod(org.testng.annotations.BeforeMethod) ConstantUtf8(org.apache.bcel.classfile.ConstantUtf8) Test(org.testng.annotations.Test)

Example 9 with ConstantPool

use of org.apache.bcel.classfile.ConstantPool in project fb-contrib by mebigfatguy.

the class OptionalIssues method sawOpcode.

/**
 * implements the visitor to look for reference compares of Optional, Optional use when more specific Optionals should be used, and use of orElse when
 * orElseGet would be more appropriate
 *
 * @param seen
 *            the opcode of the currently parsed instruction
 */
@Override
public void sawOpcode(int seen) {
    FQMethod curCalledMethod = null;
    Boolean sawPlainOptional = null;
    try {
        switch(seen) {
            case Const.IFNULL:
            case Const.IFNONNULL:
                if (stack.getStackDepth() > 0) {
                    OpcodeStack.Item itm = stack.getStackItem(0);
                    if ("Ljava/util/Optional;".equals(itm.getSignature())) {
                        bugReporter.reportBug(new BugInstance(this, BugType.OI_OPTIONAL_ISSUES_CHECKING_REFERENCE.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                    }
                }
                break;
            case Const.INVOKEDYNAMIC:
                // smells like a hack. Not sure how to do this better
                ConstantInvokeDynamic id = (ConstantInvokeDynamic) getConstantRefOperand();
                ConstantPool cp = getConstantPool();
                ConstantNameAndType nameAndType = (ConstantNameAndType) cp.getConstant(id.getNameAndTypeIndex());
                ConstantUtf8 typeConstant = (ConstantUtf8) cp.getConstant(nameAndType.getSignatureIndex());
                curCalledMethod = new FQMethod(getClassName(), "lambda$" + id.getBootstrapMethodAttrIndex(), typeConstant.getBytes());
                break;
            case Const.INVOKESTATIC:
            case Const.INVOKEINTERFACE:
            case Const.INVOKESPECIAL:
                String clsName = getClassConstantOperand();
                String methodName = getNameConstantOperand();
                curCalledMethod = new FQMethod(clsName, methodName, getSigConstantOperand());
                if ("java/util/Optional".equals(clsName) && "of".equals(methodName)) {
                    if (stack.getStackDepth() > 0) {
                        OpcodeStack.Item itm = stack.getStackItem(0);
                        String itmSig = itm.getSignature();
                        if (BOXED_OPTIONAL_TYPES.contains(itmSig)) {
                            bugReporter.reportBug(new BugInstance(this, BugType.OI_OPTIONAL_ISSUES_PRIMITIVE_VARIANT_PREFERRED.name(), LOW_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                        }
                    }
                }
                break;
            case Const.INVOKEVIRTUAL:
                curCalledMethod = new FQMethod(getClassConstantOperand(), getNameConstantOperand(), getSigConstantOperand());
                if (OR_ELSE_METHODS.contains(curCalledMethod)) {
                    if (stack.getStackDepth() > 0) {
                        OpcodeStack.Item itm = stack.getStackItem(0);
                        if ((itm.getRegisterNumber() < 0) && (itm.getReturnValueOf() != null) && !isTrivialStackOps()) {
                            bugReporter.reportBug(new BugInstance(this, BugType.OI_OPTIONAL_ISSUES_USES_IMMEDIATE_EXECUTION.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                        }
                    }
                    if (OPTIONAL_OR_ELSE_METHOD.equals(curCalledMethod)) {
                        sawPlainOptional = Boolean.TRUE;
                    }
                } else if (OR_ELSE_GET_METHODS.contains(curCalledMethod)) {
                    if (!activeStackOps.isEmpty()) {
                        ActiveStackOp op = activeStackOps.getLast();
                        FQMethod method = op.getMethod();
                        if (method == null) {
                            bugReporter.reportBug(new BugInstance(this, BugType.OI_OPTIONAL_ISSUES_USES_ORELSEGET_WITH_NULL.name(), LOW_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                        } else {
                            Method getMethod = getLambdaMethod(method.getMethodName());
                            if (getMethod != null) {
                                byte[] byteCode = getMethod.getCode().getCode();
                                if (byteCode.length <= 4) {
                                    // we are looking for ALOAD, GETFIELD, or LDC followed by ARETURN, that should fit in 4 bytes
                                    if (!hasInvoke(byteCode)) {
                                        bugReporter.reportBug(new BugInstance(this, BugType.OI_OPTIONAL_ISSUES_USES_DELAYED_EXECUTION.name(), LOW_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                                    }
                                }
                            }
                        }
                    }
                    if (OPTIONAL_OR_ELSE_GET_METHOD.equals(curCalledMethod)) {
                        sawPlainOptional = Boolean.TRUE;
                    }
                } else if (OPTIONAL_GET_METHOD.equals(curCalledMethod)) {
                    sawPlainOptional = Boolean.TRUE;
                }
                break;
        }
    } finally {
        stack.sawOpcode(this, seen);
        int stackDepth = stack.getStackDepth();
        if (stackDepth == 0) {
            activeStackOps.clear();
        } else {
            activeStackOps.addLast(new ActiveStackOp(seen, curCalledMethod));
            while (activeStackOps.size() > stackDepth) {
                activeStackOps.removeFirst();
            }
            if (sawPlainOptional != null) {
                OpcodeStack.Item itm = stack.getStackItem(0);
                itm.setUserValue(sawPlainOptional);
            }
        }
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) BugInstance(edu.umd.cs.findbugs.BugInstance) ToString(com.mebigfatguy.fbcontrib.utils.ToString) Method(org.apache.bcel.classfile.Method) FQMethod(com.mebigfatguy.fbcontrib.utils.FQMethod) ConstantUtf8(org.apache.bcel.classfile.ConstantUtf8) ConstantNameAndType(org.apache.bcel.classfile.ConstantNameAndType) ConstantPool(org.apache.bcel.classfile.ConstantPool) FQMethod(com.mebigfatguy.fbcontrib.utils.FQMethod) ConstantInvokeDynamic(org.apache.bcel.classfile.ConstantInvokeDynamic)

Example 10 with ConstantPool

use of org.apache.bcel.classfile.ConstantPool in project fb-contrib by mebigfatguy.

the class IncorrectInternalClassUse method visitClassContext.

/**
 * implements the visitor to look for classes that reference com.sun.xxx, or org.apache.xerces.xxx classes by looking for class Const in the constant pool
 *
 * @param context
 *            the context object of the currently parsed class
 */
@Override
public void visitClassContext(ClassContext context) {
    JavaClass cls = context.getJavaClass();
    if (!isInternal(cls.getClassName())) {
        ConstantPool pool = cls.getConstantPool();
        int numItems = pool.getLength();
        for (int i = 0; i < numItems; i++) {
            Constant c = pool.getConstant(i);
            if (c instanceof ConstantClass) {
                String clsName = ((ConstantClass) c).getBytes(pool);
                if (isInternal(clsName)) {
                    bugReporter.reportBug(new BugInstance(this, BugType.IICU_INCORRECT_INTERNAL_CLASS_USE.name(), NORMAL_PRIORITY).addClass(cls).addString(clsName));
                }
            }
        }
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) ConstantPool(org.apache.bcel.classfile.ConstantPool) Constant(org.apache.bcel.classfile.Constant) BugInstance(edu.umd.cs.findbugs.BugInstance) ToString(com.mebigfatguy.fbcontrib.utils.ToString) ConstantClass(org.apache.bcel.classfile.ConstantClass)

Aggregations

ConstantPool (org.apache.bcel.classfile.ConstantPool)17 ToString (com.mebigfatguy.fbcontrib.utils.ToString)7 BugInstance (edu.umd.cs.findbugs.BugInstance)7 Constant (org.apache.bcel.classfile.Constant)7 ConstantClass (org.apache.bcel.classfile.ConstantClass)6 ConstantNameAndType (org.apache.bcel.classfile.ConstantNameAndType)6 JavaClass (org.apache.bcel.classfile.JavaClass)5 ConstantMethodref (org.apache.bcel.classfile.ConstantMethodref)4 ConstantString (org.apache.bcel.classfile.ConstantString)4 ConstantUtf8 (org.apache.bcel.classfile.ConstantUtf8)4 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)3 ArrayList (java.util.ArrayList)3 ExceptionTable (org.apache.bcel.classfile.ExceptionTable)3 Method (org.apache.bcel.classfile.Method)3 BitSet (java.util.BitSet)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Attribute (org.apache.bcel.classfile.Attribute)2 Code (org.apache.bcel.classfile.Code)2 ConstantInvokeDynamic (org.apache.bcel.classfile.ConstantInvokeDynamic)2