Search in sources :

Example 1 with ClassAnnotation

use of edu.umd.cs.findbugs.ClassAnnotation in project spotbugs by spotbugs.

the class MainFrameComponentFactory method bugSummaryComponent.

Component bugSummaryComponent(BugAnnotation value, BugInstance bug) {
    JLabel label = new JLabel();
    label.setFont(label.getFont().deriveFont(Driver.getFontSize()));
    label.setFont(label.getFont().deriveFont(Font.PLAIN));
    label.setForeground(Color.BLACK);
    ClassAnnotation primaryClass = bug.getPrimaryClass();
    String sourceCodeLabel = L10N.getLocalString("summary.source_code", "source code.");
    String summaryLine = L10N.getLocalString("summary.line", "Line");
    String summaryLines = L10N.getLocalString("summary.lines", "Lines");
    String clickToGoToText = L10N.getLocalString("tooltip.click_to_go_to", "Click to go to");
    if (value instanceof SourceLineAnnotation) {
        final SourceLineAnnotation link = (SourceLineAnnotation) value;
        if (sourceCodeExists(link)) {
            String srcStr = "";
            int start = link.getStartLine();
            int end = link.getEndLine();
            if (start < 0 && end < 0) {
                srcStr = sourceCodeLabel;
            } else if (start == end) {
                srcStr = " [" + summaryLine + " " + start + "]";
            } else if (start < end) {
                srcStr = " [" + summaryLines + " " + start + " - " + end + "]";
            }
            label.setToolTipText(clickToGoToText + " " + srcStr);
            label.addMouseListener(new BugSummaryMouseListener(bug, label, link));
        }
        label.setText(link.toString());
    } else if (value instanceof BugAnnotationWithSourceLines) {
        BugAnnotationWithSourceLines note = (BugAnnotationWithSourceLines) value;
        final SourceLineAnnotation link = note.getSourceLines();
        String srcStr = "";
        if (link != null && sourceCodeExists(link)) {
            int start = link.getStartLine();
            int end = link.getEndLine();
            if (start < 0 && end < 0) {
                srcStr = sourceCodeLabel;
            } else if (start == end) {
                srcStr = " [" + summaryLine + " " + start + "]";
            } else if (start < end) {
                srcStr = " [" + summaryLines + " " + start + " - " + end + "]";
            }
            if (!"".equals(srcStr)) {
                label.setToolTipText(clickToGoToText + " " + srcStr);
                label.addMouseListener(new BugSummaryMouseListener(bug, label, link));
            }
        }
        String noteText;
        if (note == bug.getPrimaryMethod() || note == bug.getPrimaryField()) {
            noteText = note.toString();
        } else {
            noteText = note.toString(primaryClass);
        }
        if (!srcStr.equals(sourceCodeLabel)) {
            label.setText(noteText + srcStr);
        } else {
            label.setText(noteText);
        }
    } else {
        label.setText(value.toString(primaryClass));
    }
    return label;
}
Also used : SourceLineAnnotation(edu.umd.cs.findbugs.SourceLineAnnotation) BugAnnotationWithSourceLines(edu.umd.cs.findbugs.BugAnnotationWithSourceLines) JLabel(javax.swing.JLabel) ClassAnnotation(edu.umd.cs.findbugs.ClassAnnotation)

Example 2 with ClassAnnotation

use of edu.umd.cs.findbugs.ClassAnnotation in project spotbugs by spotbugs.

the class SourceMatcherTest method testRealPathMatchWithRegexpAndAnalysisContext.

@Test
public void testRealPathMatchWithRegexpAndAnalysisContext() throws Exception {
    // add this test class as the bug target
    bug.addClass("SourceMatcherTest", null);
    ClassAnnotation primaryClass = bug.getPrimaryClass();
    // set source file
    primaryClass.setSourceLines(SourceLineAnnotation.createUnknown("SourceMatcherTest", "SourceMatcherTest.java"));
    // setup a testing project with source directory, as of right now the source directory should really exist!!
    Project testProject = new Project();
    String sourceDir = "src/test/java/edu/umd/cs/findbugs/filter";
    testProject.addSourceDirs(Collections.singletonList(sourceDir));
    // setup test analysis context
    AnalysisContext.setCurrentAnalysisContext(new AnalysisContext(testProject));
    // regexp match source folder with analysis context
    SourceMatcher sm = new SourceMatcher("~.*findbugs.*.java");
    assertTrue("The regex matches the source directory of the given java file", sm.match(bug));
    sm = new SourceMatcher("~.*notfound.*.java");
    assertFalse("The regex does not match the source directory of the given java file", sm.match(bug));
}
Also used : Project(edu.umd.cs.findbugs.Project) ClassAnnotation(edu.umd.cs.findbugs.ClassAnnotation) AnalysisContext(edu.umd.cs.findbugs.ba.AnalysisContext) Test(org.junit.Test)

Example 3 with ClassAnnotation

use of edu.umd.cs.findbugs.ClassAnnotation in project spotbugs by spotbugs.

the class OverridingEqualsNotSymmetrical method visit.

@Override
public void visit(Code obj) {
    if (EQUALS_NAME.equals(getMethodName()) && !getMethod().isStatic() && getMethod().isPublic() && EQUALS_SIGNATURE.equals(getMethodSig())) {
        sawCheckedCast = sawSuperEquals = sawInstanceOf = sawGetClass = sawReturnSuper = sawCompare = sawReturnNonSuper = prevWasSuperEquals = sawGoodEqualsClass = sawBadEqualsClass = dangerDanger = sawInstanceOfSupertype = alwaysTrue = alwaysFalse = sawStaticDelegate = sawEqualsBuilder = isRecord = false;
        sawInitialIdentityCheck = obj.getCode().length == 11 || obj.getCode().length == 9;
        equalsCalls = 0;
        super.visit(obj);
        EqualsKindSummary.KindOfEquals kind = EqualsKindSummary.KindOfEquals.UNKNOWN;
        if (alwaysTrue) {
            kind = EqualsKindSummary.KindOfEquals.ALWAYS_TRUE;
        } else if (alwaysFalse) {
            kind = EqualsKindSummary.KindOfEquals.ALWAYS_FALSE;
        } else if (sawReturnSuper && !sawReturnNonSuper) {
            kind = EqualsKindSummary.KindOfEquals.RETURNS_SUPER;
        } else if (sawSuperEquals) {
            kind = EqualsKindSummary.KindOfEquals.INVOKES_SUPER;
        } else if (sawInstanceOfSupertype) {
            kind = EqualsKindSummary.KindOfEquals.INSTANCE_OF_SUPERCLASS_EQUALS;
        } else if (sawInstanceOf) {
            kind = getThisClass().isAbstract() ? EqualsKindSummary.KindOfEquals.ABSTRACT_INSTANCE_OF : EqualsKindSummary.KindOfEquals.INSTANCE_OF_EQUALS;
        } else if (sawGetClass && sawGoodEqualsClass) {
            kind = getThisClass().isAbstract() ? EqualsKindSummary.KindOfEquals.ABSTRACT_GETCLASS_GOOD_EQUALS : EqualsKindSummary.KindOfEquals.GETCLASS_GOOD_EQUALS;
        } else if (sawGetClass && sawBadEqualsClass) {
            kind = EqualsKindSummary.KindOfEquals.GETCLASS_BAD_EQUALS;
        } else if (equalsCalls == 1 || sawStaticDelegate || sawEqualsBuilder) {
            kind = EqualsKindSummary.KindOfEquals.DELEGATE_EQUALS;
        } else if (sawInitialIdentityCheck) {
            kind = EqualsKindSummary.KindOfEquals.TRIVIAL_EQUALS;
        } else if (sawCheckedCast) {
            kind = EqualsKindSummary.KindOfEquals.CHECKED_CAST_EQUALS;
        } else if (sawCompare) {
            kind = EqualsKindSummary.KindOfEquals.COMPARE_EQUALS;
        } else if (isRecord) {
            kind = EqualsKindSummary.KindOfEquals.RECORD;
        } else {
            if (AnalysisContext.currentAnalysisContext().isApplicationClass(getThisClass())) {
                bugReporter.reportBug(new BugInstance(this, "EQ_UNUSUAL", Priorities.NORMAL_PRIORITY).addClassAndMethod(this));
            }
        }
        ClassAnnotation classAnnotation = new ClassAnnotation(getDottedClassName());
        equalsKindSummary.put(classAnnotation, kind);
        count(kind);
        if (kind == EqualsKindSummary.KindOfEquals.GETCLASS_GOOD_EQUALS || kind == EqualsKindSummary.KindOfEquals.ABSTRACT_GETCLASS_GOOD_EQUALS || kind == EqualsKindSummary.KindOfEquals.GETCLASS_BAD_EQUALS) {
            ClassDescriptor classDescriptor = getClassDescriptor();
            try {
                Set<ClassDescriptor> subtypes = AnalysisContext.currentAnalysisContext().getSubtypes2().getSubtypes(classDescriptor);
                if (subtypes.size() > 1) {
                    classesWithGetClassBasedEquals.put(classDescriptor, subtypes);
                }
            } catch (ClassNotFoundException e) {
                assert true;
            }
        }
        if (kind == EqualsKindSummary.KindOfEquals.INSTANCE_OF_EQUALS || kind == EqualsKindSummary.KindOfEquals.ABSTRACT_INSTANCE_OF) {
            ClassDescriptor classDescriptor = getClassDescriptor();
            try {
                Set<ClassDescriptor> subtypes = AnalysisContext.currentAnalysisContext().getSubtypes2().getSubtypes(classDescriptor);
                if (subtypes.size() > 1) {
                    classesWithInstanceOfBasedEquals.put(classDescriptor, subtypes);
                }
            } catch (ClassNotFoundException e) {
                assert true;
            }
        }
        String superClassName = getSuperclassName().replace('/', '.');
        if (!Values.DOTTED_JAVA_LANG_OBJECT.equals(superClassName)) {
            parentMap.put(classAnnotation, new ClassAnnotation(superClassName));
        }
        equalsMethod.put(classAnnotation, getMethodDescriptor());
    }
    bugAccumulator.reportAccumulatedBugs();
}
Also used : ClassDescriptor(edu.umd.cs.findbugs.classfile.ClassDescriptor) BugInstance(edu.umd.cs.findbugs.BugInstance) ClassAnnotation(edu.umd.cs.findbugs.ClassAnnotation) EqualsKindSummary(edu.umd.cs.findbugs.ba.EqualsKindSummary)

Example 4 with ClassAnnotation

use of edu.umd.cs.findbugs.ClassAnnotation in project spotbugs by spotbugs.

the class OverridingEqualsNotSymmetrical method report.

@Override
public void report() {
    for (Map.Entry<ClassAnnotation, ClassAnnotation> e : parentMap.entrySet()) {
        ClassAnnotation childClass = e.getKey();
        EqualsKindSummary.KindOfEquals childKind = equalsKindSummary.get(childClass);
        ClassAnnotation parentClass = e.getValue();
        EqualsKindSummary.KindOfEquals parentKind = equalsKindSummary.get(parentClass);
        if (childKind == EqualsKindSummary.KindOfEquals.INSTANCE_OF_EQUALS && parentKind == EqualsKindSummary.KindOfEquals.INSTANCE_OF_EQUALS) {
            bugReporter.reportBug(new BugInstance(this, "EQ_OVERRIDING_EQUALS_NOT_SYMMETRIC", NORMAL_PRIORITY).add(childClass).addMethod(equalsMethod.get(childClass)).addMethod(equalsMethod.get(parentClass)).describe(MethodAnnotation.METHOD_OVERRIDDEN));
        }
    }
}
Also used : ClassAnnotation(edu.umd.cs.findbugs.ClassAnnotation) BugInstance(edu.umd.cs.findbugs.BugInstance) HashMap(java.util.HashMap) Map(java.util.Map) EnumMap(java.util.EnumMap) TreeMap(java.util.TreeMap) EqualsKindSummary(edu.umd.cs.findbugs.ba.EqualsKindSummary)

Example 5 with ClassAnnotation

use of edu.umd.cs.findbugs.ClassAnnotation in project spotbugs by spotbugs.

the class NoteSuppressedWarnings method suppressWarning.

private void suppressWarning(String pattern) {
    String className = getDottedClassName();
    ClassAnnotation clazz = new ClassAnnotation(className);
    if (className.endsWith(".package-info")) {
        suppressionMatcher.addPackageSuppressor(new PackageWarningSuppressor(pattern, getPackageName().replace('/', '.')));
    } else if (visitingMethod()) {
        suppressionMatcher.addSuppressor(new MethodWarningSuppressor(pattern, clazz, MethodAnnotation.fromVisitedMethod(this)));
    } else if (visitingField()) {
        suppressionMatcher.addSuppressor(new FieldWarningSuppressor(pattern, clazz, FieldAnnotation.fromVisitedField(this)));
    } else {
        suppressionMatcher.addSuppressor(new ClassWarningSuppressor(pattern, clazz));
    }
}
Also used : MethodWarningSuppressor(edu.umd.cs.findbugs.MethodWarningSuppressor) FieldWarningSuppressor(edu.umd.cs.findbugs.FieldWarningSuppressor) ClassAnnotation(edu.umd.cs.findbugs.ClassAnnotation) ClassWarningSuppressor(edu.umd.cs.findbugs.ClassWarningSuppressor) PackageWarningSuppressor(edu.umd.cs.findbugs.PackageWarningSuppressor)

Aggregations

ClassAnnotation (edu.umd.cs.findbugs.ClassAnnotation)19 BugInstance (edu.umd.cs.findbugs.BugInstance)8 SourceLineAnnotation (edu.umd.cs.findbugs.SourceLineAnnotation)5 MethodAnnotation (edu.umd.cs.findbugs.MethodAnnotation)4 FieldAnnotation (edu.umd.cs.findbugs.FieldAnnotation)3 Test (org.junit.Test)3 BugAnnotation (edu.umd.cs.findbugs.BugAnnotation)2 Project (edu.umd.cs.findbugs.Project)2 EqualsKindSummary (edu.umd.cs.findbugs.ba.EqualsKindSummary)2 XClass (edu.umd.cs.findbugs.ba.XClass)2 XMethod (edu.umd.cs.findbugs.ba.XMethod)2 ClassDescriptor (edu.umd.cs.findbugs.classfile.ClassDescriptor)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 AppVersion (edu.umd.cs.findbugs.AppVersion)1 BugAnnotationWithSourceLines (edu.umd.cs.findbugs.BugAnnotationWithSourceLines)1 BugCollection (edu.umd.cs.findbugs.BugCollection)1 ClassWarningSuppressor (edu.umd.cs.findbugs.ClassWarningSuppressor)1 FieldWarningSuppressor (edu.umd.cs.findbugs.FieldWarningSuppressor)1 LocalVariableAnnotation (edu.umd.cs.findbugs.LocalVariableAnnotation)1 MethodWarningSuppressor (edu.umd.cs.findbugs.MethodWarningSuppressor)1