Search in sources :

Example 46 with JavaClass

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

the class MapUsageIssues method sawOpcode.

@Override
public void sawOpcode(int seen) {
    try {
        if (!mapContainsKeyUsed.isEmpty()) {
            Iterator<Map.Entry<MapRef, ContainsKey>> it = mapContainsKeyUsed.entrySet().iterator();
            int pc = getPC();
            while (it.hasNext()) {
                Map.Entry<MapRef, ContainsKey> entry = it.next();
                if (!entry.getKey().isValid() || entry.getValue().outOfScope(pc)) {
                    it.remove();
                }
            }
        }
        // checking for a branch might be overkill, but for now lets go with it
        if (!mapGetUsed.isEmpty() && OpcodeUtils.isBranch(seen)) {
            Iterator<Map.Entry<MapRef, Get>> it = mapGetUsed.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<MapRef, Get> entry = it.next();
                it.remove();
            }
        }
        if ((seen == Const.IFNULL) || (seen == Const.IFNONNULL)) {
            if (stack.getStackDepth() > 0) {
                OpcodeStack.Item itm = stack.getStackItem(0);
                XMethod method = itm.getReturnValueOf();
                if ((method != null) && (mapClass != null)) {
                    if (COLLECTION_ACCESSORS.contains(method.getName())) {
                        JavaClass cls = Repository.lookupClass(method.getClassName());
                        if (cls.implementationOf(mapClass)) {
                            bugReporter.reportBug(new BugInstance(this, BugType.MUI_NULL_CHECK_ON_MAP_SUBSET_ACCESSOR.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                        }
                    }
                }
            }
        } else if (seen == Const.INVOKEINTERFACE) {
            FQMethod fqm = new FQMethod(getClassConstantOperand(), getNameConstantOperand(), getSigConstantOperand());
            if (CONTAINS_METHOD.equals(fqm)) {
                if (stack.getStackDepth() >= 2) {
                    OpcodeStack.Item item = stack.getStackItem(1);
                    if ((item.getRegisterNumber() < 0) && (item.getXField() == null)) {
                        XMethod xm = item.getReturnValueOf();
                        if ((xm != null) && COLLECTION_ACCESSORS.contains(xm.getName()) && Values.DOTTED_JAVA_UTIL_MAP.equals(xm.getClassName())) {
                            bugReporter.reportBug(new BugInstance(this, BugType.MUI_USE_CONTAINSKEY.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                        }
                    }
                }
            } else if (CONTAINSKEY_METHOD.equals(fqm)) {
                if (getNextOpcode() == Const.IFEQ) {
                    int ifEnd = getNextPC() + CodeByteUtils.getshort(getCode().getCode(), getNextPC() + 1);
                    if (stack.getStackDepth() >= 2) {
                        OpcodeStack.Item itm = stack.getStackItem(1);
                        mapContainsKeyUsed.put(new MapRef(itm), new ContainsKey(stack.getStackItem(0), ifEnd));
                    }
                }
            } else if (GET_METHOD.equals(fqm)) {
                if (stack.getStackDepth() >= 2) {
                    OpcodeStack.Item itm = stack.getStackItem(1);
                    ContainsKey ck = mapContainsKeyUsed.remove(new MapRef(itm));
                    if ((ck != null) && new ContainsKey(stack.getStackItem(0), 0).equals(ck)) {
                        bugReporter.reportBug(new BugInstance(this, BugType.MUI_CONTAINSKEY_BEFORE_GET.name(), ck.getReportLevel()).addClass(this).addMethod(this).addSourceLine(this));
                    }
                    mapGetUsed.put(new MapRef(itm), new Get(stack.getStackItem(0)));
                }
            } else if (REMOVE_METHOD.equals(fqm)) {
                if (stack.getStackDepth() >= 2) {
                    OpcodeStack.Item itm = stack.getStackItem(1);
                    Get get = mapGetUsed.remove(new MapRef(itm));
                    if ((get != null) && new Get(stack.getStackItem(0)).equals(get)) {
                        bugReporter.reportBug(new BugInstance(this, BugType.MUI_GET_BEFORE_REMOVE.name(), get.getReportLevel()).addClass(this).addMethod(this).addSourceLine(this));
                    }
                }
            }
            QMethod qm = new QMethod(getNameConstantOperand(), getSigConstantOperand());
            if (SIZE_METHOD.equals(qm)) {
                if (stack.getStackDepth() > 0) {
                    OpcodeStack.Item itm = stack.getStackItem(0);
                    if ((itm.getRegisterNumber() < 0) && (itm.getXField() == null)) {
                        XMethod xm = itm.getReturnValueOf();
                        if ((xm != null) && Values.DOTTED_JAVA_UTIL_MAP.equals(xm.getClassName()) && COLLECTION_ACCESSORS.contains(xm.getName())) {
                            bugReporter.reportBug(new BugInstance(this, BugType.MUI_CALLING_SIZE_ON_SUBCONTAINER.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                        }
                    }
                }
            }
        }
    } catch (ClassNotFoundException e) {
        bugReporter.reportMissingClass(e);
    } finally {
        stack.sawOpcode(this, seen);
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) BugInstance(edu.umd.cs.findbugs.BugInstance) QMethod(com.mebigfatguy.fbcontrib.utils.QMethod) FQMethod(com.mebigfatguy.fbcontrib.utils.FQMethod) JavaClass(org.apache.bcel.classfile.JavaClass) XMethod(edu.umd.cs.findbugs.ba.XMethod) FQMethod(com.mebigfatguy.fbcontrib.utils.FQMethod) HashMap(java.util.HashMap) Map(java.util.Map)

Example 47 with JavaClass

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

the class MisleadingOverloadModel method visitClassContext.

@Override
public void visitClassContext(ClassContext classContext) {
    JavaClass cls = classContext.getJavaClass();
    String clsName = cls.getClassName();
    Method[] methods = cls.getMethods();
    Map<String, MethodFoundType> declMethods = new HashMap<>(methods.length);
    for (Method m : methods) {
        String methodName = m.getName();
        boolean report;
        MethodFoundType newType;
        if (m.isStatic()) {
            report = declMethods.get(methodName) == MethodFoundType.Instance;
            if (report) {
                newType = MethodFoundType.Both;
            } else {
                newType = MethodFoundType.Static;
            }
        } else {
            report = declMethods.get(m.getName()) == MethodFoundType.Static;
            if (report) {
                newType = MethodFoundType.Both;
            } else {
                newType = MethodFoundType.Instance;
            }
        }
        declMethods.put(methodName, newType);
        if (report) {
            bugReporter.reportBug(new BugInstance(this, BugType.MOM_MISLEADING_OVERLOAD_MODEL.name(), NORMAL_PRIORITY).addClass(cls).addMethod(XFactory.createXMethod(clsName, m)).addString(methodName));
        }
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) HashMap(java.util.HashMap) BugInstance(edu.umd.cs.findbugs.BugInstance) Method(org.apache.bcel.classfile.Method)

Example 48 with JavaClass

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

the class AbstractClassEmptyMethods method visitClassContext.

/**
 * overrides the visitor to check for abstract classes.
 *
 * @param classContext
 *            the context object that holds the JavaClass being parsed
 */
@Override
public void visitClassContext(ClassContext classContext) {
    try {
        JavaClass cls = classContext.getJavaClass();
        if (cls.isAbstract()) {
            interfaceMethods = collectInterfaceMethods(cls);
            super.visitClassContext(classContext);
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    } finally {
        interfaceMethods = null;
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass)

Example 49 with JavaClass

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

the class AnnotationIssues method visitClassContext.

@Override
public void visitClassContext(ClassContext classContext) {
    try {
        JavaClass cls = classContext.getJavaClass();
        if (cls.getMajor() >= Const.MAJOR_1_5) {
            if (isCollecting() || !cls.isAnonymous()) {
                stack = new OpcodeStack();
                assumedNullTill = new HashMap<>();
                assumedNonNullTill = new HashMap<>();
                noAssumptionsPossible = new HashSet<>();
                super.visitClassContext(classContext);
            }
        }
    } finally {
        stack = null;
        assumedNullTill = null;
        assumedNonNullTill = null;
        noAssumptionsPossible = null;
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) OpcodeStack(edu.umd.cs.findbugs.OpcodeStack)

Example 50 with JavaClass

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

the class BackportReusePublicIdentifiers method visitClassContext.

/**
 * overrides the visitor to make sure this is a 'modern' class better than 1.4
 *
 * @param classContext
 *            the currently parsed class
 */
@Override
public void visitClassContext(ClassContext classContext) {
    JavaClass cls = classContext.getJavaClass();
    clsVersion = cls.getMajor();
    super.visitClassContext(classContext);
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass)

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