有没有办法忽略一个FindBugs警告?

Ben*_*n S 183 java warnings findbugs suppress-warnings

对于PMD,如果要忽略特定警告,可以使用// NOPMD该行来忽略该行.

FindBugs有类似的东西吗?

Pas*_*ent 292

FindBugs初始方法涉及XML配置文件aka 过滤器.这实际上不如PMD解决方案方便,但FindBugs适用于字节码,而不是源代码,因此评论显然不是一种选择.例:

<Match>
   <Class name="com.mycompany.Foo" />
   <Method name="bar" />
   <Bug pattern="DLS_DEAD_STORE_OF_CLASS_LITERAL" />
</Match>
Run Code Online (Sandbox Code Playgroud)

但是,为了解决这个问题,FindBugs后来引入了另一种基于注释的解决方案(请参阅参考资料SuppressFBWarnings),您可以在类或方法级别使用(在我看来比XML更方便).示例(也许不是最好的一个,但是,它只是一个例子):

@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
    value="HE_EQUALS_USE_HASHCODE", 
    justification="I know what I'm doing")
Run Code Online (Sandbox Code Playgroud)

请注意,由于名称与Java的冲突SuppressWarnings,@SuppressFBWarnings因此不推荐使用FindBugs 3.0.0 SuppressWarnings.

  • +1你的评论"我知道我在做什么" (90认同)
  • 当然使用注释方法的问题是你的代码相当不必要地导入(和后续依赖)Findbugs库:( (29认同)
  • 对于那些Maven用户,您可以使用以下内容导入注释.(Bonus,范围已设置,因此您的项目在运行时不依赖于FindBugs).`<dependency> <groupId> net.sourceforge.findbugs </ groupId> <artifactId> annotations </ artifactId> <version> 1.3.2 </ version> <scope>提供</ scope> </ dependency>` (17认同)
  • @AshleyWalton注释的保留是CLASS,所以至少它只是一个编译时依赖 (9认同)
  • Maven用户应添加`<dependency> <groupId> com.google.code.findbugs </ groupId> <artifactId> annotations </ artifactId> <version> 3.0.0 </ version> <scope>提供</ scope> </如果他们想使用`@ SuppressFBWarnings`,依赖>`到他们的POM. (7认同)
  • 奖金问题:如何为给定的报告"bug"(使用声纳)找到合适的值? (4认同)

hin*_*nks 19

正如其他人所提到的,您可以使用@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 "";
}
Run Code Online (Sandbox Code Playgroud)

资料来源:https://sourceforge.net/p/findbugs/feature-requests/298/#5e88


mbo*_*ess 15

这是一个更完整的XML过滤器示例(上面的示例本身不起作用,因为它只显示一个片段并且缺少<FindBugsFilter>begin和end标记):

<FindBugsFilter>
    <Match>
        <Class name="com.mycompany.foo" />
        <Method name="bar" />
        <Bug pattern="NP_BOOLEAN_RETURN_NULL" />
    </Match>
</FindBugsFilter>
Run Code Online (Sandbox Code Playgroud)

如果您使用的是Android Studio FindBugs插件,请使用文件 - >其他设置 - >默认设置 - >其他设置 - > FindBugs-IDEA->过滤器 - >排除过滤器文件 - >添加,浏览到您的XML过滤器文件.


ana*_*ocs 10

更新Gradle

dependencies {
    compile group: 'findbugs', name: 'findbugs', version: '1.0.0'
}
Run Code Online (Sandbox Code Playgroud)

找到FindBugs报告

文件:///Users/your_user/IdeaProjects/projectname/build/reports/findbugs/main.html

找到具体的消息

发现错误

导入正确版本的注释

import edu.umd.cs.findbugs.annotations.SuppressWarnings;
Run Code Online (Sandbox Code Playgroud)

在违规代码的正上方添加注释

@SuppressWarnings("OUT_OF_RANGE_ARRAY_INDEX")
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参见此处:findbugs Spring Annotation

  • +1,但请更新您的答案:gradle`testCompile'c​​om.google.code.findbugs:annotations:3.0.0'`和注释名称`@ SuppressFBWarnings` (3认同)

Ash*_*eze 10

虽然这里的其他答案是有效的,但它们并不是解决这个问题的完整秘诀。

本着完整性的精神:

您需要在 pom 文件中包含 findbugs 注释 - 它们只是编译时,因此您可以使用范围provided

<dependency>
  <groupId>com.google.code.findbugs</groupId>
  <artifactId>findbugs-annotations</artifactId>
  <version>3.0.1</version>
  <scope>provided</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

这允许使用@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))) { ... }
Run Code Online (Sandbox Code Playgroud)

这包括错误的名称以及您禁用其扫描的原因。

最后需要重新运行findbugs来清除错误。


小智 7

在撰写本文时(2018年5月),FindBugs似乎已被SpotBugs取代。使用SuppressFBWarnings注释要求您的代码使用Java 8或更高版本进行编译,并引入对的编译时间依赖性spotbugs-annotations.jar

使用过滤器文件过滤SpotBugs规则没有这样的问题。文档在这里