Skip to content

Commit

Permalink
close WorksApplications#22: detect FindBugs warning which has no just…
Browse files Browse the repository at this point in the history
…ification.
  • Loading branch information
worksap-bot committed Apr 7, 2014
1 parent e53e0e1 commit 525061c
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ To use this product, please configure your findbugs-maven-plugin like below.
## 0.0.3

- added UnexpectedAccessDetector
- added UndocumentedSuppressFBWarningsDetector

## 0.0.2

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package jp.co.worksap.oss.findbugs.findbugs;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Collections;
import java.util.Map;
import java.util.Set;

import javax.annotation.Nonnull;

import org.apache.bcel.classfile.ElementValue;

import com.google.common.collect.Sets;

import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.BytecodeScanningDetector;
import edu.umd.cs.findbugs.internalAnnotations.DottedClassName;

/**
* <p>A detector to ensure that FindBugs&apos; SuppressWarnings annotation has justification.</p>
* @see edu.umd.cs.findbugs.annotations.SuppressWarnings
* @see edu.umd.cs.findbugs.annotations.SuppressFBWarnings
* @author Kengo TODA ([email protected])
*/
public class UndocumentedSuppressFBWarningsDetector extends BytecodeScanningDetector {
private static final Set<String> TARGET_ANNOTATIONS = Collections.unmodifiableSet(Sets.newHashSet(
"edu.umd.cs.findbugs.annotations.SuppressWarnings",
"edu.umd.cs.findbugs.annotations.SuppressFBWarnings"
));

@Nonnull
private final BugReporter bugReporter;

public UndocumentedSuppressFBWarningsDetector(BugReporter bugReporter) {
this.bugReporter = checkNotNull(bugReporter);
}

@Override
public void visitAnnotation(@DottedClassName String annotationClass,
Map<String, ElementValue> map, boolean runtimeVisible) {
if (! TARGET_ANNOTATIONS.contains(annotationClass)) {
return;
}

final ElementValue reason = map.get("justification");
if (reason == null || reason.stringifyValue().trim().isEmpty()) {
BugInstance bugInstance = new BugInstance("FINDBUGS_UNDOCUMENTED_SUPPRESS_WARNINGS",
HIGH_PRIORITY).addClass(this);
if (visitingMethod()) {
bugInstance.addMethod(this).addSourceLine(this);
}
bugReporter.reportBug(bugInstance);
}
}

}
1 change: 1 addition & 0 deletions src/main/meta/bugrank.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
0 BugPattern USE_COLUMN_DEFINITION
0 BugPattern NULLABLE_PRIMITIVE
0 BugPattern GUAVA_UNEXPECTED_ACCESS_TO_VISIBLE_FOR_TESTING
0 BugPattern FINDBUGS_UNDOCUMENTED_SUPPRESS_WARNINGS
5 changes: 5 additions & 0 deletions src/main/meta/findbugs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,9 @@
<BugPattern type="GUAVA_UNEXPECTED_ACCESS_TO_VISIBLE_FOR_TESTING" abbrev="GUAVA"
category="CORRECTNESS" />

<Detector class="jp.co.worksap.oss.findbugs.findbugs.UndocumentedSuppressFBWarningsDetector"
speed="fast" hidden="false" reports="FINDBUGS_UNDOCUMENTED_SUPPRESS_WARNINGS" />
<BugPattern type="FINDBUGS_UNDOCUMENTED_SUPPRESS_WARNINGS" abbrev="FINDBUGS"
category="BAD_PRACTICE" />

</FindbugsPlugin>
20 changes: 20 additions & 0 deletions src/main/meta/messages.xml
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,29 @@
</Details>
</BugPattern>

<Detector class="jp.co.worksap.oss.findbugs.findbugs.UndocumentedSuppressFBWarningsDetector">
<Details>
This detector will find suppressed FindBugs warning which has no justification about why it is suppressed.
</Details>
</Detector>

<BugPattern type="FINDBUGS_UNDOCUMENTED_SUPPRESS_WARNINGS">
<ShortDescription>To tell why this FindBugs warning is suppressed, put justification as a parameter.
</ShortDescription>
<LongDescription>
To tell why this FindBugs warning is suppressed, put justification as a parameter.
</LongDescription>
<Details>
<![CDATA[
<p>To tell why this FindBugs warning is suppressed, put justification as a parameter.</p>
]]>
</Details>
</BugPattern>

<BugCode abbrev="SYS">SYS</BugCode>
<BugCode abbrev="JSR305">JSR305</BugCode>
<BugCode abbrev="JPA">JPA</BugCode>
<BugCode addrev="JUNIT">JUnit</BugCode>
<BugCode addrev="GUAVA">Google Guava</BugCode>
<BugCode addrev="FINDBUGS">FindBugs</BugCode>
</MessageCollection>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package jp.co.worksap.oss.findbugs.findbugs;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public class DocumentedSuppressFBWarnings {
@SuppressFBWarnings(justification = "only for unit test.")
public void method() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package jp.co.worksap.oss.findbugs.findbugs;

import edu.umd.cs.findbugs.annotations.SuppressWarnings;

@SuppressWarnings(justification = "only for unit test.")
public class DocumentedSuppressWarnings {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package jp.co.worksap.oss.findbugs.findbugs;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public class UndocumentedSuppressFBWarnings {
@SuppressFBWarnings
public void method() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package jp.co.worksap.oss.findbugs.findbugs;

import static com.youdevise.fbplugins.tdd4fb.DetectorAssert.*;

import org.junit.Before;
import org.junit.Test;

import edu.umd.cs.findbugs.BugReporter;

public class UndocumentedSuppressFBWarningsDetectorTest {

private UndocumentedSuppressFBWarningsDetector detector;
private BugReporter bugReporter;

@Before
public void setup() {
bugReporter = bugReporterForTesting();
detector = new UndocumentedSuppressFBWarningsDetector(bugReporter);
}

@Test
public void testDocumentedClasses() throws Exception {
assertNoBugsReported(DocumentedSuppressWarnings.class, detector, bugReporter);
assertNoBugsReported(DocumentedSuppressFBWarnings.class, detector, bugReporter);
}

@Test
public void testUndocumentedClasses() throws Exception {
assertBugReported(UndocumentedSuppressWarnings.class, detector, bugReporter, ofType("FINDBUGS_UNDOCUMENTED_SUPPRESS_WARNINGS"));
assertBugReported(UndocumentedSuppressFBWarnings.class, detector, bugReporter, ofType("FINDBUGS_UNDOCUMENTED_SUPPRESS_WARNINGS"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package jp.co.worksap.oss.findbugs.findbugs;

import edu.umd.cs.findbugs.annotations.SuppressWarnings;

@SuppressWarnings
public class UndocumentedSuppressWarnings {

}

0 comments on commit 525061c

Please sign in to comment.