Java 有没有办法忽略单个 FindBugs 警告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1829904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:03:28  来源:igfitidea点击:

Is there a way to ignore a single FindBugs warning?

javawarningsfindbugssuppress-warnings

提问by Ben S

With PMD, if you want to ignore a specific warning, you can use // NOPMDto have that line be ignored.

使用 PMD,如果您想忽略特定警告,您可以使用// NOPMD忽略该行。

Is there something similar for FindBugs?

FindBugs 有类似的东西吗?

采纳答案by Pascal Thivent

The FindBugs initial approach involves XML configuration files aka filters. This is really less convenient than the PMD solution but FindBugs works on bytecode, not on the source code, so comments are obviously not an option. Example:

FindBugs 最初的方法涉及 XML 配置文件又名过滤器。这确实不如 PMD 解决方案方便,但 FindBugs 适用于字节码,而不是源代码,因此注释显然不是一种选择。例子:

<Match>
   <Class name="com.mycompany.Foo" />
   <Method name="bar" />
   <Bug pattern="DLS_DEAD_STORE_OF_CLASS_LITERAL" />
</Match>

However, to solve this issue, FindBugs later introduced another solution based on annotations(see SuppressFBWarnings) that you can use at the class or at the method level (more convenient than XML in my opinion). Example (maybe not the best one but, well, it's just an example):

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

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

Note that since FindBugs 3.0.0 SuppressWarningshas been deprecated in favor of @SuppressFBWarningsbecause of the name clash with Java's SuppressWarnings.

请注意,由于 FindBugs 3.0.0SuppressWarnings已被弃用,@SuppressFBWarnings因为名称与 Java 的SuppressWarnings.

回答by mbonness

Here is a more complete example of an XML filter (the example above by itself will not work since it just shows a snippet and is missing the <FindBugsFilter>begin and end tags):

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

<FindBugsFilter>
    <Match>
        <Class name="com.mycompany.foo" />
        <Method name="bar" />
        <Bug pattern="NP_BOOLEAN_RETURN_NULL" />
    </Match>
</FindBugsFilter>

If you are using the Android Studio FindBugs plugin, browse to your XML filter file using File->Other Settings->Default Settings->Other Settings->FindBugs-IDEA->Filter->Exclude filter files->Add.

如果您使用的是 Android Studio FindBugs 插件,请使用 File->Other Settings->Default Settings->Other Settings->FindBugs-IDEA->Filter->Exclude filter files->Add 浏览到您的 XML 过滤器文件。

回答by 00500005

I'm going to leave this one here: https://stackoverflow.com/a/14509697/1356953

我要把这个留在这里:https: //stackoverflow.com/a/14509697/1356953

Please note that this works with java.lang.SuppressWarningsso no need to use a separate annotation.

请注意,这适用于java.lang.SuppressWarnings因此无需使用单独的注释。

@SuppressWarnings on a field only suppresses findbugs warnings reported for that field declaration, not every warning associated with that field.

For example, this suppresses the "Field only ever set to null" warning:

@SuppressWarnings("UWF_NULL_FIELD") String s = null; I think the best you can do is isolate the code with the warning into the smallest method you can, then suppress the warning on the whole method.

字段上的 @SuppressWarnings 仅抑制为该字段声明报告的 findbugs 警告,而不是与该字段关联的每个警告。

例如,这会抑制“Field only ever set to null”警告:

@SuppressWarnings("UWF_NULL_FIELD") String s = null; 我认为你能做的最好的事情就是将带有警告的代码隔离到最小的方法中,然后在整个方法上抑制警告。

回答by anataliocs

Update Gradle

更新 Gradle

dependencies {
    compile group: 'findbugs', name: 'findbugs', version: '1.0.0'
}

Locate the FindBugs Report

找到 FindBugs 报告

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

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

Find the specific message

查找特定消息

find bugs

发现错误

Import the correct version of the annotation

导入正确版本的注释

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

Add the annotation directly above the offending code

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

@SuppressWarnings("OUT_OF_RANGE_ARRAY_INDEX")

See here for more info: findbugs Spring Annotation

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

回答by hinneLinks

As others Mentioned, you can use the @SuppressFBWarningsAnnotation. If you don't want or can't add another Dependency to your code, you can add the Annotation to your Code yourself, Findbugs dosn't care in which Package the Annotation is.

正如其他人提到的,您可以使用@SuppressFBWarnings注释。如果你不想或不能在你的代码中添加另一个依赖,你可以自己在你的代码中添加 Annotation,Findbugs 并不关心 Annotation 在哪个 Package 中。

@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 "";
}

Source: https://sourceforge.net/p/findbugs/feature-requests/298/#5e88

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

回答by steve

At the time of writing this (May 2018), FindBugs seems to have been replaced by SpotBugs. Using the SuppressFBWarningsannotation requires your code to be compiled with Java 8 or later and introduces a compile time dependency on spotbugs-annotations.jar.

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

Using a filter file to filter SpotBugs rules has no such issues. The documentation is here.

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

回答by Ashley Frieze

While other answers on here are valid, they're not a full recipe for solving this.

虽然此处的其他答案是有效的,但它们并不是解决此问题的完整方法。

In the spirit of completeness:

本着完整性的精神:

You need to have the findbugs annotations in your pom file - they're only compile time, so you can use the providedscope:

你需要在你的 pom 文件中有 findbugs 注释——它们只是编译时,所以你可以使用provided范围:

<dependency>
  <groupId>com.google.code.findbugs</groupId>
  <artifactId>findbugs-annotations</artifactId>
  <version>3.0.1</version>
  <scope>provided</scope>
</dependency>

This allows the use of @SuppressFBWarningsthere is another dependency which provides @SuppressWarnings. However, the above is clearer.

这允许使用@SuppressFBWarnings另一个提供@SuppressWarnings. 不过,上面说的更清楚了。

Then you add the annotation above your method:

然后在方法上方添加注释:

E.g.

例如

@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))) { ... }

This includes both the name of the bugand also a reason why you're disabling the scan for it.

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