Search in sources :

Example 51 with JavaClass

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

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

the class BogusExceptionDeclaration method removeThrownExceptionHierarchy.

/**
 * removes this thrown exception the list of declared thrown exceptions, including all exceptions in this exception's hierarchy. If an exception class is
 * found that can't be loaded, then just clear the list of declared checked exceptions and get out.
 *
 * @param thrownException
 *            the exception and it's hierarchy to remove
 */
private void removeThrownExceptionHierarchy(String thrownException) {
    try {
        if (Values.DOTTED_JAVA_LANG_EXCEPTION.equals(thrownException) || Values.DOTTED_JAVA_LANG_THROWABLE.equals(thrownException)) {
            // Exception/Throwable can be thrown even tho the method isn't declared to throw
            // Exception/Throwable in the case of templated Exceptions
            clearExceptions();
        } else {
            removeException(thrownException);
            JavaClass exCls = Repository.lookupClass(thrownException);
            String clsName;
            do {
                exCls = exCls.getSuperClass();
                if (exCls == null) {
                    break;
                }
                clsName = exCls.getClassName();
                removeException(clsName);
            } while (!declaredCheckedExceptions.isEmpty() && !Values.DOTTED_JAVA_LANG_EXCEPTION.equals(clsName) && !Values.DOTTED_JAVA_LANG_ERROR.equals(clsName));
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
        clearExceptions();
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass)

Example 53 with JavaClass

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

the class BogusExceptionDeclaration method visitClassContext.

/**
 * overrides the visitor to create the opcode stack
 *
 * @param classContext
 *            the context object of the currently parsed class
 */
@Override
public void visitClassContext(ClassContext classContext) {
    try {
        if ((runtimeExceptionClass != null) && (exceptionClass != null)) {
            stack = new OpcodeStack();
            declaredCheckedExceptions = new HashSet<>(6);
            JavaClass cls = classContext.getJavaClass();
            classIsFinal = cls.isFinal();
            classIsAnonymous = cls.isAnonymous();
            super.visitClassContext(classContext);
        }
    } finally {
        declaredCheckedExceptions = null;
        stack = null;
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) JavaClass(org.apache.bcel.classfile.JavaClass)

Example 54 with JavaClass

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

the class ClassEnvy method countClassAccess.

/**
 * increment the count of class access of the class on the stack
 *
 * @param classAtStackIndex
 *            the position on the stack of the class in question
 *
 * @return true if the class is counted
 */
private boolean countClassAccess(final int classAtStackIndex) {
    String calledClass;
    try {
        if (stack.getStackDepth() > classAtStackIndex) {
            OpcodeStack.Item itm = stack.getStackItem(classAtStackIndex);
            JavaClass cls = itm.getJavaClass();
            if (cls != null) {
                calledClass = cls.getClassName();
                countClassAccess(calledClass);
                return true;
            }
        }
    } catch (ClassNotFoundException cfne) {
        bugReporter.reportMissingClass(cfne);
    }
    return false;
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) JavaClass(org.apache.bcel.classfile.JavaClass)

Example 55 with JavaClass

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

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