【发布时间】:2010-12-22 05:48:38
【问题描述】:
使用 PMD,如果您想忽略特定警告,可以使用 // NOPMD 忽略该行。
FindBugs 有类似的东西吗?
【问题讨论】:
标签: java warnings findbugs suppress-warnings
使用 PMD,如果您想忽略特定警告,可以使用 // NOPMD 忽略该行。
FindBugs 有类似的东西吗?
【问题讨论】:
标签: java warnings findbugs suppress-warnings
FindBugs 的初始方法涉及 XML 配置文件,也就是 filters。这确实不如 PMD 解决方案方便,但 FindBugs 适用于字节码,而不适用于源代码,因此 cmets 显然不是一个选项。示例:
<Match>
<Class name="com.mycompany.Foo" />
<Method name="bar" />
<Bug pattern="DLS_DEAD_STORE_OF_CLASS_LITERAL" />
</Match>
但是,为了解决这个问题,FindBugs 后来推出了另一种基于annotations(参见SuppressFBWarnings)的解决方案,您可以在类或方法级别使用它(我认为比 XML 更方便)。示例(也许不是最好的,但是,这只是一个示例):
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
value="HE_EQUALS_USE_HASHCODE",
justification="I know what I'm doing")
请注意,由于与 Java 的 SuppressWarnings 名称冲突,已弃用 FindBugs 3.0.0 SuppressWarnings 以支持 @SuppressFBWarnings。
【讨论】:
<dependency> <groupId>net.sourceforge.findbugs</groupId> <artifactId>annotations</artifactId> <version>1.3.2</version> <scope>provided</scope> </dependency>
@SuppressFBWarnings,应该将<dependency><groupId>com.google.code.findbugs</groupId><artifactId>annotations</artifactId><version>3.0.0</version><scope>provided</scope></dependency> 添加到他们的POM 中。
正如其他人提到的,您可以使用@SuppressFBWarnings 注释。
如果你不想或不能在你的代码中添加另一个依赖,你可以自己在你的代码中添加注解,Findbugs 并不关心注解在哪个包中。
@Retention(RetentionPolicy.CLASS)
public @interface SuppressFBWarnings {
/**
* The set of FindBugs warnings that are to be suppressed in
* annotated element. The value can be a bug category, kind or pattern.
*
*/
String[] value() default {};
/**
* Optional documentation of the reason why the warning is suppressed
*/
String justification() default "";
}
来源:https://sourceforge.net/p/findbugs/feature-requests/298/#5e88
【讨论】:
这是一个更完整的 XML 过滤器示例(上面的示例本身不起作用,因为它只显示一个 sn-p 并且缺少 <FindBugsFilter> 开始和结束标记):
<FindBugsFilter>
<Match>
<Class name="com.mycompany.foo" />
<Method name="bar" />
<Bug pattern="NP_BOOLEAN_RETURN_NULL" />
</Match>
</FindBugsFilter>
如果您使用的是 Android Studio FindBugs 插件,请使用 File->Other Settings->Default Settings->Other Settings->FindBugs-IDEA->Filter->Exclude filter files->Add 浏览到您的 XML 过滤器文件。
【讨论】:
更新 Gradle
dependencies {
compile group: 'findbugs', name: 'findbugs', version: '1.0.0'
}
找到 FindBugs 报告
file:///Users/your_user/IdeaProjects/projectname/build/reports/findbugs/main.html
查找具体消息
导入正确版本的注释
import edu.umd.cs.findbugs.annotations.SuppressWarnings;
在违规代码正上方添加注释
@SuppressWarnings("OUT_OF_RANGE_ARRAY_INDEX")
查看这里了解更多信息:findbugs Spring Annotation
【讨论】:
compile 'net.sourceforge.findbugs:annotations:1.3.2' 语法来代替,它更短。
testCompile 'com.google.code.findbugs:annotations:3.0.0' 和注释名称 @SuppressFBWarnings
【讨论】:
虽然此处的其他答案是有效的,但它们并不是解决此问题的完整方法。
本着完整的精神:
你需要在你的 pom 文件中有 findbugs 注释——它们只是编译时间,所以你可以使用provided 范围:
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>findbugs-annotations</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
这允许使用@SuppressFBWarnings,还有另一个依赖项提供@SuppressWarnings。不过上面说的比较清楚。
然后在方法上方添加注释:
例如
@SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE",
justification = "Scanning generated code of try-with-resources")
@Override
public String get() {
try (InputStream resourceStream = owningType.getClassLoader().getResourceAsStream(resourcePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(resourceStream, UTF_8))) { ... }
这包括 bug 的名称以及您禁用扫描它的原因。
【讨论】:
我要把这个留在这里:https://stackoverflow.com/a/14509697/1356953
请注意,这适用于java.lang.SuppressWarnings,因此无需使用单独的注释。
@SuppressWarnings 在字段上只抑制 findbugs 警告 报告该字段声明,而不是与相关的每个警告 那个字段。
例如,这会抑制“仅设置为空的字段” 警告:
@SuppressWarnings("UWF_NULL_FIELD") 字符串 s = null;我认为最好的 你可以做的是将带有警告的代码隔离成最小的 方法,然后抑制整个方法的警告。
【讨论】:
java.lang.SuppressWarnings 无法工作。它具有源代码保留,因此 findbugs 不可见。