Search in sources :

Example 26 with Method

use of org.apache.bcel.classfile.Method 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)

Example 27 with Method

use of org.apache.bcel.classfile.Method 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 28 with Method

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

the class OCSDebugger method visitCode.

@Override
public void visitCode(Code obj) {
    Method m = getMethod();
    String curMethodDesc = getClassContext().getJavaClass().getClassName() + '.' + m.getName() + m.getSignature();
    if (curMethodDesc.equals(METHOD_DESC)) {
        try {
            pw = new PrintWriter(OUTPUT_FILE_NAME, StandardCharsets.UTF_8.name());
            stack.resetForMethodEntry(this);
            super.visitCode(obj);
        } catch (IOException e) {
        // ignore
        } finally {
            pw.close();
            pw = null;
        }
    }
}
Also used : Method(org.apache.bcel.classfile.Method) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 29 with Method

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

the class BogusExceptionDeclaration method visitCode.

/**
 * implements the visitor to see if the method declares that it throws any checked exceptions.
 *
 * @param obj
 *            the context object of the currently parsed code block
 */
@Override
public void visitCode(Code obj) {
    Method method = getMethod();
    if (method.isSynthetic()) {
        return;
    }
    declaredCheckedExceptions.clear();
    stack.resetForMethodEntry(this);
    ExceptionTable et = method.getExceptionTable();
    if (et != null) {
        if (classIsFinal || classIsAnonymous || method.isStatic() || method.isPrivate() || method.isFinal() || ((Values.CONSTRUCTOR.equals(method.getName()) && !isAnonymousInnerCtor(method, getThisClass())))) {
            String[] exNames = et.getExceptionNames();
            for (String exName : exNames) {
                try {
                    JavaClass exCls = Repository.lookupClass(exName);
                    if (!exCls.instanceOf(runtimeExceptionClass)) {
                        declaredCheckedExceptions.add(exName);
                    }
                } catch (ClassNotFoundException cnfe) {
                    bugReporter.reportMissingClass(cnfe);
                }
            }
            if (!declaredCheckedExceptions.isEmpty()) {
                try {
                    super.visitCode(obj);
                    if (!declaredCheckedExceptions.isEmpty()) {
                        BugInstance bi = new BugInstance(this, BugType.BED_BOGUS_EXCEPTION_DECLARATION.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this, 0);
                        for (String ex : declaredCheckedExceptions) {
                            bi.addString(ex.replaceAll("/", "."));
                        }
                        bugReporter.reportBug(bi);
                    }
                } catch (StopOpcodeParsingException e) {
                // no exceptions left
                }
            }
        }
        String[] exNames = et.getExceptionNames();
        for (int i = 0; i < (exNames.length - 1); i++) {
            try {
                JavaClass exCls1 = Repository.lookupClass(exNames[i]);
                for (int j = i + 1; j < exNames.length; j++) {
                    JavaClass exCls2 = Repository.lookupClass(exNames[j]);
                    JavaClass childEx;
                    JavaClass parentEx;
                    if (exCls1.instanceOf(exCls2)) {
                        childEx = exCls1;
                        parentEx = exCls2;
                    } else if (exCls2.instanceOf(exCls1)) {
                        childEx = exCls2;
                        parentEx = exCls1;
                    } else {
                        continue;
                    }
                    if (!parentEx.equals(exceptionClass)) {
                        bugReporter.reportBug(new BugInstance(this, BugType.BED_HIERARCHICAL_EXCEPTION_DECLARATION.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addString(childEx.getClassName() + " derives from " + parentEx.getClassName()));
                        return;
                    }
                }
            } catch (ClassNotFoundException cnfe) {
                bugReporter.reportMissingClass(cnfe);
            }
        }
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException) BugInstance(edu.umd.cs.findbugs.BugInstance) ExceptionTable(org.apache.bcel.classfile.ExceptionTable) Method(org.apache.bcel.classfile.Method)

Example 30 with Method

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

the class CloneUsability method visitCode.

/**
 * overrides the visitor to grab the method name and reset the state.
 *
 * @param obj
 *            the method being parsed
 */
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(value = "FCBL_FIELD_COULD_BE_LOCAL", justification = "False positives occur when state is maintained across callbacks")
@Override
public void visitCode(Code obj) {
    try {
        Method m = getMethod();
        if (m.isPublic() && !m.isSynthetic() && "clone".equals(m.getName()) && (m.getArgumentTypes().length == 0)) {
            String returnClsName = m.getReturnType().getSignature();
            returnClsName = SignatureUtils.stripSignature(returnClsName);
            if (!clsName.equals(returnClsName)) {
                if (Values.DOTTED_JAVA_LANG_OBJECT.equals(returnClsName)) {
                    bugReporter.reportBug(new BugInstance(this, BugType.CU_CLONE_USABILITY_OBJECT_RETURN.name(), NORMAL_PRIORITY).addClass(this).addMethod(this));
                } else {
                    JavaClass clonedClass = Repository.lookupClass(returnClsName);
                    if (!cls.instanceOf(clonedClass)) {
                        bugReporter.reportBug(new BugInstance(this, BugType.CU_CLONE_USABILITY_MISMATCHED_RETURN.name(), HIGH_PRIORITY).addClass(this).addMethod(this));
                    }
                }
            }
            ExceptionTable et = m.getExceptionTable();
            if ((et != null) && (et.getLength() > 0)) {
                throwsCNFE = false;
                if (prescreen(m)) {
                    stack.resetForMethodEntry(this);
                    super.visitCode(obj);
                }
                if (!throwsCNFE) {
                    bugReporter.reportBug(new BugInstance(this, BugType.CU_CLONE_USABILITY_THROWS.name(), NORMAL_PRIORITY).addClass(this).addMethod(this));
                }
            }
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) BugInstance(edu.umd.cs.findbugs.BugInstance) ExceptionTable(org.apache.bcel.classfile.ExceptionTable) Method(org.apache.bcel.classfile.Method)

Aggregations

Method (org.apache.bcel.classfile.Method)79 JavaClass (org.apache.bcel.classfile.JavaClass)28 BugInstance (edu.umd.cs.findbugs.BugInstance)20 ToString (com.mebigfatguy.fbcontrib.utils.ToString)12 StopOpcodeParsingException (com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException)11 FQMethod (com.mebigfatguy.fbcontrib.utils.FQMethod)7 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 Field (org.apache.bcel.classfile.Field)6 Type (org.apache.bcel.generic.Type)6 AnnotationEntry (org.apache.bcel.classfile.AnnotationEntry)5 ExceptionTable (org.apache.bcel.classfile.ExceptionTable)5 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)4 SourceLineAnnotation (edu.umd.cs.findbugs.SourceLineAnnotation)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 ConstantPoolGen (org.apache.bcel.generic.ConstantPoolGen)4 BugType (com.mebigfatguy.fbcontrib.utils.BugType)3 QMethod (com.mebigfatguy.fbcontrib.utils.QMethod)3 XMethod (edu.umd.cs.findbugs.ba.XMethod)3