Search in sources :

Example 36 with JavaClass

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

the class WeakExceptionMessaging method checkForWEM.

private void checkForWEM() throws ClassNotFoundException {
    if (stack.getStackDepth() == 0) {
        return;
    }
    OpcodeStack.Item item = stack.getStackItem(0);
    if (item.getUserValue() == null) {
        return;
    }
    JavaClass exClass = item.getJavaClass();
    if ((exClass == null) || !ignorableExceptionTypes.contains(exClass.getClassName())) {
        bugReporter.reportBug(new BugInstance(this, BugType.WEM_WEAK_EXCEPTION_MESSAGING.name(), LOW_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) JavaClass(org.apache.bcel.classfile.JavaClass) BugInstance(edu.umd.cs.findbugs.BugInstance)

Example 37 with JavaClass

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

the class OverlyConcreteParameter method buildParameterDefiners.

/**
 * builds a map of method information for each method of each interface that each parameter implements of this method
 *
 * @return a map by parameter id of all the method signatures that interfaces of that parameter implements
 *
 * @throws ClassNotFoundException
 *             if the class can't be loaded
 */
private boolean buildParameterDefiners() throws ClassNotFoundException {
    Method m = getMethod();
    Type[] parms = m.getArgumentTypes();
    if (parms.length == 0) {
        return false;
    }
    ParameterAnnotationEntry[] annotations = m.getParameterAnnotationEntries();
    boolean hasPossiblyOverlyConcreteParm = false;
    for (int i = 0; i < parms.length; i++) {
        if ((annotations.length <= i) || (annotations[i] == null) || (annotations[i].getAnnotationEntries().length == 0)) {
            String parm = parms[i].getSignature();
            if (parm.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX)) {
                String clsName = SignatureUtils.stripSignature(parm);
                if (clsName.startsWith("java.lang.")) {
                    continue;
                }
                JavaClass clz = Repository.lookupClass(clsName);
                if (clz.isClass() && (!clz.isAbstract())) {
                    Map<JavaClass, List<MethodInfo>> definers = getClassDefiners(clz);
                    if (!definers.isEmpty()) {
                        parameterDefiners.put(Integer.valueOf(i + (methodIsStatic ? 0 : 1)), definers);
                        hasPossiblyOverlyConcreteParm = true;
                    }
                }
            }
        }
    }
    return hasPossiblyOverlyConcreteParm;
}
Also used : Type(org.apache.bcel.generic.Type) BugType(com.mebigfatguy.fbcontrib.utils.BugType) JavaClass(org.apache.bcel.classfile.JavaClass) ParameterAnnotationEntry(org.apache.bcel.classfile.ParameterAnnotationEntry) ArrayList(java.util.ArrayList) List(java.util.List) Method(org.apache.bcel.classfile.Method) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

Example 38 with JavaClass

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

the class OverlyConcreteParameter method removeUselessDefiners.

/**
 * parses through the interface that 'may' define a parameter defined by reg, and look to see if we can rule it out, because a method is called on the
 * object that can't be satisfied by the interface, if so remove that candidate interface.
 *
 * @param reg
 *            the parameter register number to look at
 */
private void removeUselessDefiners(final int reg) {
    Map<JavaClass, List<MethodInfo>> definers = parameterDefiners.get(Integer.valueOf(reg));
    if (CollectionUtils.isEmpty(definers)) {
        return;
    }
    String methodSig = getSigConstantOperand();
    String methodName = getNameConstantOperand();
    MethodInfo methodInfo = new MethodInfo(methodName, methodSig);
    Iterator<List<MethodInfo>> it = definers.values().iterator();
    while (it.hasNext()) {
        boolean methodDefined = false;
        List<MethodInfo> methodSigs = it.next();
        for (MethodInfo mi : methodSigs) {
            if (methodInfo.equals(mi)) {
                methodDefined = true;
                String[] exceptions = mi.getMethodExceptions();
                if (exceptions != null) {
                    for (String ex : exceptions) {
                        if (!isExceptionHandled(ex)) {
                            methodDefined = false;
                            break;
                        }
                    }
                }
                break;
            }
        }
        if (!methodDefined) {
            it.remove();
        }
    }
    if (definers.isEmpty()) {
        parameterDefiners.remove(Integer.valueOf(reg));
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) ArrayList(java.util.ArrayList) List(java.util.List) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

Example 39 with JavaClass

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

the class ParallelLists method visitClassContext.

@Override
public void visitClassContext(final ClassContext classContext) {
    try {
        JavaClass cls = classContext.getJavaClass();
        listFields = new HashSet<>();
        Field[] flds = cls.getFields();
        for (Field f : flds) {
            String sig = f.getSignature();
            if (sig.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX)) {
                sig = SignatureUtils.trimSignature(sig);
                if (sig.startsWith("java/util/") && sig.endsWith("List")) {
                    listFields.add(f.getName());
                }
            } else if (sig.startsWith(Values.SIG_ARRAY_PREFIX) && !sig.startsWith(Values.SIG_ARRAY_OF_ARRAYS_PREFIX)) {
                listFields.add(f.getName());
            }
        }
        if (!listFields.isEmpty()) {
            stack = new OpcodeStack();
            indexToFieldMap = new HashMap<>();
            super.visitClassContext(classContext);
        }
    } finally {
        stack = null;
        indexToFieldMap = null;
    }
}
Also used : Field(org.apache.bcel.classfile.Field) XField(edu.umd.cs.findbugs.ba.XField) JavaClass(org.apache.bcel.classfile.JavaClass) OpcodeStack(edu.umd.cs.findbugs.OpcodeStack)

Example 40 with JavaClass

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

the class PartiallyConstructedObjectAccess method reportChainedMethods.

private void reportChainedMethods() {
    Set<Method> checkedMethods = new HashSet<>();
    JavaClass cls = getClassContext().getJavaClass();
    for (Map.Entry<Method, Map<Method, SourceLineAnnotation>> entry : methodToCalledMethods.entrySet()) {
        Method m = entry.getKey();
        if (Values.CONSTRUCTOR.equals(m.getName())) {
            checkedMethods.clear();
            Deque<SourceLineAnnotation> slas = foundPrivateInChain(m, checkedMethods);
            if (slas != null) {
                BugInstance bi = new BugInstance(this, BugType.PCOA_PARTIALLY_CONSTRUCTED_OBJECT_ACCESS.name(), LOW_PRIORITY).addClass(cls).addMethod(cls, m);
                for (SourceLineAnnotation sla : slas) {
                    bi.addSourceLine(sla);
                }
                bugReporter.reportBug(bi);
            }
        }
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) SourceLineAnnotation(edu.umd.cs.findbugs.SourceLineAnnotation) BugInstance(edu.umd.cs.findbugs.BugInstance) Method(org.apache.bcel.classfile.Method) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

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