forked from WorksApplications/findbugs-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
close WorksApplications#22: detect FindBugs warning which has no just…
…ification.
- Loading branch information
1 parent
e53e0e1
commit 525061c
Showing
10 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...main/java/jp/co/worksap/oss/findbugs/findbugs/UndocumentedSuppressFBWarningsDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' 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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/test/java/jp/co/worksap/oss/findbugs/findbugs/DocumentedSuppressFBWarnings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/test/java/jp/co/worksap/oss/findbugs/findbugs/DocumentedSuppressWarnings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/test/java/jp/co/worksap/oss/findbugs/findbugs/UndocumentedSuppressFBWarnings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
.../java/jp/co/worksap/oss/findbugs/findbugs/UndocumentedSuppressFBWarningsDetectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/test/java/jp/co/worksap/oss/findbugs/findbugs/UndocumentedSuppressWarnings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |