Search in sources :

Example 1 with FieldDescriptor

use of edu.umd.cs.findbugs.classfile.FieldDescriptor in project wcomponents by BorderTech.

the class CheckExpectedWarningsCustom method visitClass.

/**
 * {@inheritDoc}
 */
@Override
public void visitClass(final ClassDescriptor classDescriptor) throws CheckedAnalysisException {
    if (reporter == null) {
        if (!warned) {
            System.err.println("*** NOTE ***: CheckExpectedWarnings disabled because bug reporter doesn't use a BugCollection");
            warned = true;
        }
        return;
    }
    if (warningsByMethod == null) {
        // 
        // Build index of all warnings reported so far, by method.
        // Because this detector runs in a later pass than any
        // reporting detector, all warnings should have been
        // produced by this point.
        // 
        warningsByMethod = new HashMap<>();
        warningsByField = new HashMap<>();
        warningsByClass = new HashMap<>();
        BugCollection bugCollection = reporter.getBugCollection();
        for (Iterator<BugInstance> i = bugCollection.iterator(); i.hasNext(); ) {
            BugInstance warning = i.next();
            MethodAnnotation method = warning.getPrimaryMethod();
            FieldAnnotation field = warning.getPrimaryField();
            ClassAnnotation clazz = warning.getPrimaryClass();
            if (method != null) {
                MethodDescriptor methodDesc = method.toXMethod().getMethodDescriptor();
                Collection<BugInstance> warnings = warningsByMethod.get(methodDesc);
                if (warnings == null) {
                    warnings = new LinkedList<>();
                    warningsByMethod.put(methodDesc, warnings);
                }
                warnings.add(warning);
            } else if (field != null) {
                FieldDescriptor fieldDesc = XFactory.createXField(field).getFieldDescriptor();
                Collection<BugInstance> warnings = warningsByField.get(fieldDesc);
                if (warnings == null) {
                    warnings = new LinkedList<>();
                    warningsByField.put(fieldDesc, warnings);
                }
                warnings.add(warning);
            } else if (clazz != null) {
                ClassDescriptor classDesc = DescriptorFactory.createClassDescriptorFromDottedClassName(clazz.getClassName());
                Collection<BugInstance> warnings = warningsByClass.get(classDesc);
                if (warnings == null) {
                    warnings = new LinkedList<>();
                    warningsByClass.put(classDesc, warnings);
                }
                warnings.add(warning);
            }
        }
        // 
        // Based on enabled detectors, figure out which bug codes
        // could possibly be reported. Don't complain about
        // expected warnings that would be produced by detectors
        // that aren't enabled.
        // 
        possibleBugCodes = new HashSet<>();
        ExecutionPlan executionPlan = Global.getAnalysisCache().getDatabase(ExecutionPlan.class);
        for (Iterator<AnalysisPass> i = executionPlan.passIterator(); i.hasNext(); ) {
            AnalysisPass pass = i.next();
            for (Iterator<DetectorFactory> j = pass.iterator(); j.hasNext(); ) {
                DetectorFactory factory = j.next();
                Collection<BugPattern> reportedPatterns = factory.getReportedBugPatterns();
                for (BugPattern pattern : reportedPatterns) {
                    possibleBugCodes.add(pattern.getAbbrev());
                }
            }
        }
        if (DEBUG) {
            log("CEW: possible warnings are " + possibleBugCodes);
        }
    }
    XClass xclass = Global.getAnalysisCache().getClassAnalysis(XClass.class, classDescriptor);
    for (XMethod xmethod : xclass.getXMethods()) {
        if (DEBUG) {
            log("CEW: checking " + xmethod.toString());
        }
        check(xmethod, expectWarning, true);
        check(xmethod, noWarning, false);
    }
    for (XField xfield : xclass.getXFields()) {
        if (DEBUG) {
            log("CEW: checking " + xfield.toString());
        }
        check(xfield, expectWarning, true);
        check(xfield, noWarning, false);
    }
    check(xclass, expectWarning, true);
    check(xclass, noWarning, false);
}
Also used : ClassDescriptor(edu.umd.cs.findbugs.classfile.ClassDescriptor) XClass(edu.umd.cs.findbugs.ba.XClass) FieldDescriptor(edu.umd.cs.findbugs.classfile.FieldDescriptor) ExecutionPlan(edu.umd.cs.findbugs.plan.ExecutionPlan) XMethod(edu.umd.cs.findbugs.ba.XMethod) MethodDescriptor(edu.umd.cs.findbugs.classfile.MethodDescriptor) LinkedList(java.util.LinkedList) XField(edu.umd.cs.findbugs.ba.XField) Collection(java.util.Collection) AnalysisPass(edu.umd.cs.findbugs.plan.AnalysisPass)

Aggregations

XClass (edu.umd.cs.findbugs.ba.XClass)1 XField (edu.umd.cs.findbugs.ba.XField)1 XMethod (edu.umd.cs.findbugs.ba.XMethod)1 ClassDescriptor (edu.umd.cs.findbugs.classfile.ClassDescriptor)1 FieldDescriptor (edu.umd.cs.findbugs.classfile.FieldDescriptor)1 MethodDescriptor (edu.umd.cs.findbugs.classfile.MethodDescriptor)1 AnalysisPass (edu.umd.cs.findbugs.plan.AnalysisPass)1 ExecutionPlan (edu.umd.cs.findbugs.plan.ExecutionPlan)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1