diff --git a/README.md b/README.md index 27ad839..39e3440 100755 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/main/java/jp/co/worksap/oss/findbugs/findbugs/UndocumentedSuppressFBWarningsDetector.java b/src/main/java/jp/co/worksap/oss/findbugs/findbugs/UndocumentedSuppressFBWarningsDetector.java new file mode 100644 index 0000000..14e9294 --- /dev/null +++ b/src/main/java/jp/co/worksap/oss/findbugs/findbugs/UndocumentedSuppressFBWarningsDetector.java @@ -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; + +/** + *

A detector to ensure that FindBugs' SuppressWarnings annotation has justification.

+ * @see edu.umd.cs.findbugs.annotations.SuppressWarnings + * @see edu.umd.cs.findbugs.annotations.SuppressFBWarnings + * @author Kengo TODA (toda_k@worksap.co.jp) + */ +public class UndocumentedSuppressFBWarningsDetector extends BytecodeScanningDetector { + private static final Set 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 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); + } + } + +} diff --git a/src/main/meta/bugrank.txt b/src/main/meta/bugrank.txt index cef2572..3a8eb57 100755 --- a/src/main/meta/bugrank.txt +++ b/src/main/meta/bugrank.txt @@ -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 diff --git a/src/main/meta/findbugs.xml b/src/main/meta/findbugs.xml index a7885f7..5569f15 100755 --- a/src/main/meta/findbugs.xml +++ b/src/main/meta/findbugs.xml @@ -69,4 +69,9 @@ +