Search in sources :

Example 1 with StringAnnotation

use of edu.umd.cs.findbugs.StringAnnotation in project fb-contrib by mebigfatguy.

the class RuntimeExceptionDeclared method visitMethod.

/**
 * overrides the visitor to find declared runtime exceptions
 *
 * @param obj
 *            the method object of the currently parsed method
 */
@Override
public void visitMethod(final Method obj) {
    if (obj.isSynthetic()) {
        return;
    }
    ExceptionTable et = obj.getExceptionTable();
    if (et != null) {
        String[] exNames = et.getExceptionNames();
        Set<String> methodRTExceptions = new HashSet<>(6);
        int priority = LOW_PRIORITY;
        boolean foundRuntime = false;
        for (String ex : exNames) {
            boolean isRuntime = false;
            if (runtimeExceptions.contains(ex)) {
                isRuntime = true;
            } else {
                try {
                    JavaClass exClass = Repository.lookupClass(ex);
                    if (exClass.instanceOf(runtimeExceptionClass)) {
                        runtimeExceptions.add(ex);
                        if (ex.startsWith("java.lang.")) {
                            priority = NORMAL_PRIORITY;
                        }
                        isRuntime = true;
                    }
                } catch (ClassNotFoundException cnfe) {
                    bugReporter.reportMissingClass(cnfe);
                }
            }
            if (isRuntime) {
                foundRuntime = true;
                methodRTExceptions.add(ex);
            }
        }
        if (foundRuntime) {
            BugInstance bug = new BugInstance(this, BugType.DRE_DECLARED_RUNTIME_EXCEPTION.name(), priority).addClass(this).addMethod(this);
            for (String ex : methodRTExceptions) {
                bug.add(new StringAnnotation(ex));
            }
            bugReporter.reportBug(bug);
        }
    }
}
Also used : StringAnnotation(edu.umd.cs.findbugs.StringAnnotation) JavaClass(org.apache.bcel.classfile.JavaClass) BugInstance(edu.umd.cs.findbugs.BugInstance) ExceptionTable(org.apache.bcel.classfile.ExceptionTable) HashSet(java.util.HashSet)

Aggregations

BugInstance (edu.umd.cs.findbugs.BugInstance)1 StringAnnotation (edu.umd.cs.findbugs.StringAnnotation)1 HashSet (java.util.HashSet)1 ExceptionTable (org.apache.bcel.classfile.ExceptionTable)1 JavaClass (org.apache.bcel.classfile.JavaClass)1