Search in sources :

Example 41 with JavaClass

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

the class PartiallyConstructedObjectAccess method visitClassContext.

/**
 * implements the visitor to set up the stack and methodToCalledmethods map reports calls to public non final methods from methods called from constructors.
 *
 * @param classContext
 *            the context object of the currently parsed class
 */
@Override
public void visitClassContext(final ClassContext classContext) {
    try {
        JavaClass cls = classContext.getJavaClass();
        if (!cls.isFinal()) {
            stack = new OpcodeStack();
            methodToCalledMethods = new HashMap<>();
            super.visitClassContext(classContext);
            if (!methodToCalledMethods.isEmpty()) {
                reportChainedMethods();
            }
        }
    } finally {
        stack = null;
        methodToCalledMethods = null;
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) OpcodeStack(edu.umd.cs.findbugs.OpcodeStack)

Example 42 with JavaClass

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

the class PoorMansEnum method visitClassContext.

@Override
public void visitClassContext(ClassContext classContext) {
    try {
        JavaClass cls = classContext.getJavaClass();
        if (cls.getMajor() >= Const.MAJOR_1_5) {
            fieldValues = new HashMap<>();
            nameToField = new HashMap<>();
            for (Field f : cls.getFields()) {
                if (f.isPrivate() && !f.isSynthetic()) {
                    String fieldName = f.getName();
                    // preallocating a set per field is just a waste, so just insert the empty set as a place holder
                    fieldValues.put(fieldName, Collections.emptySet());
                    nameToField.put(fieldName, f);
                }
            }
            if (!fieldValues.isEmpty()) {
                stack = new OpcodeStack();
                firstFieldUse = new HashMap<>();
                try {
                    super.visitClassContext(classContext);
                    for (Map.Entry<String, Set<Object>> fieldInfo : fieldValues.entrySet()) {
                        Set<Object> values = fieldInfo.getValue();
                        if (values.size() >= 3) {
                            String fieldName = fieldInfo.getKey();
                            bugReporter.reportBug(new BugInstance(this, BugType.PME_POOR_MANS_ENUM.name(), NORMAL_PRIORITY).addClass(this).addField(XFactory.createXField(cls, nameToField.get(fieldName))).addSourceLine(firstFieldUse.get(fieldName)));
                        }
                    }
                } catch (StopOpcodeParsingException e) {
                // no fields left
                }
            }
        }
    } finally {
        fieldValues = null;
        nameToField = null;
        firstFieldUse = null;
        stack = null;
    }
}
Also used : Field(org.apache.bcel.classfile.Field) Set(java.util.Set) HashSet(java.util.HashSet) JavaClass(org.apache.bcel.classfile.JavaClass) OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException) BugInstance(edu.umd.cs.findbugs.BugInstance) HashMap(java.util.HashMap) Map(java.util.Map)

Example 43 with JavaClass

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

the class LoggerOddities method hasExceptionOnStack.

/**
 * returns whether an exception object is on the stack slf4j will find this, and not include it in the parm list so i we find one, just don't report
 *
 * @return whether or not an exception i present
 */
@SuppressWarnings("unchecked")
private boolean hasExceptionOnStack() {
    try {
        for (int i = 0; i < (stack.getStackDepth() - 1); i++) {
            OpcodeStack.Item item = stack.getStackItem(i);
            String sig = item.getSignature();
            if (sig.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX)) {
                String name = SignatureUtils.stripSignature(sig);
                JavaClass cls = Repository.lookupClass(name);
                if (cls.instanceOf(throwableClass)) {
                    return true;
                }
            } else if (sig.startsWith(Values.SIG_ARRAY_PREFIX)) {
                LOUserValue<Integer> uv = (LOUserValue<Integer>) item.getUserValue();
                if ((uv != null) && (uv.getType() == LOUserValue.LOType.ARRAY_SIZE)) {
                    Integer sz = uv.getValue();
                    if ((sz != null) && (sz.intValue() < 0)) {
                        return true;
                    }
                }
            }
        }
        return false;
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
        return true;
    }
}
Also used : Item(edu.umd.cs.findbugs.OpcodeStack.Item) OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) JavaClass(org.apache.bcel.classfile.JavaClass) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

Example 44 with JavaClass

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

the class LoggerOddities method isNonPrivateLogField.

/**
 * looks to see if this field is a logger, and declared non privately
 *
 * @param fieldClsName
 *            the owning class type of the field
 * @param fieldName
 *            the name of the field
 * @param fieldSig
 *            the signature of the field
 * @return if the field is a logger and not private
 */
private boolean isNonPrivateLogField(String fieldClsName, String fieldName, String fieldSig) {
    String fieldType = SignatureUtils.trimSignature(fieldSig);
    if (!SLF4J_LOGGER.equals(fieldType) && !COMMONS_LOGGER.equals(fieldType) && !LOG4J_LOGGER.equals(fieldType) && !LOG4J2_LOGGER.equals(fieldType)) {
        return false;
    }
    JavaClass cls = getClassContext().getJavaClass();
    if (!cls.getClassName().equals(fieldClsName.replace('/', '.'))) {
        return false;
    }
    for (Field f : getClassContext().getJavaClass().getFields()) {
        if (f.getName().equals(fieldName)) {
            return !f.isPrivate();
        }
    }
    return true;
}
Also used : Field(org.apache.bcel.classfile.Field) JavaClass(org.apache.bcel.classfile.JavaClass) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

Example 45 with JavaClass

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

the class LostExceptionStackTrace method isPossibleExBuilder.

/**
 * returns whether the method called might be a method that builds an exception using the original exception. It does so by looking to see if the method
 * returns an exception, and if one of the parameters is the original exception
 *
 * @param excReg
 *            the register of the original exception caught
 * @return whether this method call could be an exception builder method
 *
 * @throws ClassNotFoundException
 *             if the class of the return type can't be found
 */
public boolean isPossibleExBuilder(int excReg) throws ClassNotFoundException {
    String sig = getSigConstantOperand();
    String returnSig = SignatureUtils.getReturnSignature(sig);
    if (returnSig.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX)) {
        returnSig = SignatureUtils.trimSignature(returnSig);
        JavaClass retCls = Repository.lookupClass(returnSig);
        if (retCls.instanceOf(throwableClass)) {
            int numParms = SignatureUtils.getNumParameters(sig);
            if (stack.getStackDepth() >= numParms) {
                for (int p = 0; p < numParms; p++) {
                    OpcodeStack.Item item = stack.getStackItem(p);
                    if (item.getRegisterNumber() == excReg) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

Aggregations

JavaClass (org.apache.bcel.classfile.JavaClass)144 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)45 BugInstance (edu.umd.cs.findbugs.BugInstance)43 Method (org.apache.bcel.classfile.Method)28 ToString (com.mebigfatguy.fbcontrib.utils.ToString)27 Field (org.apache.bcel.classfile.Field)17 HashSet (java.util.HashSet)14 HashMap (java.util.HashMap)11 ClassParser (org.apache.bcel.classfile.ClassParser)10 ArrayList (java.util.ArrayList)9 IOException (java.io.IOException)8 ExceptionTable (org.apache.bcel.classfile.ExceptionTable)8 XField (edu.umd.cs.findbugs.ba.XField)7 Nullable (javax.annotation.Nullable)7 AnnotationEntry (org.apache.bcel.classfile.AnnotationEntry)7 Type (org.apache.bcel.generic.Type)7 Iterator (java.util.Iterator)6 List (java.util.List)6 Map (java.util.Map)6 Set (java.util.Set)6