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;
}
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));
}
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();
}
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));
}
}
}
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));
}
}
Aggregations