简体   繁体   English

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

[英]Is there a way to ignore a single FindBugs warning?

With PMD, if you want to ignore a specific warning, you can use // NOPMD to have that line be ignored.使用 PMD,如果要忽略特定警告,可以使用// NOPMD忽略该行。

Is there something similar for FindBugs? FindBugs 有类似的东西吗?

The FindBugs initial approach involves XML configuration files aka filters . FindBugs 最初的方法涉及 XML 配置文件又名过滤器 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.这确实不如 PMD 解决方案方便,但 FindBugs 适用于字节码,而不是源代码,因此注释显然不是一种选择。 Example:例子:

<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).但是,为了解决这个问题,FindBugs 后来引入了另一种基于注解的解决方案(参见SuppressFBWarnings ),您可以在类或方法级别使用(我认为比 XML 更方便)。 Example (maybe not the best one but, well, it's just an example):示例(可能不是最好的,但是,这只是一个示例):

@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 SuppressWarnings has been deprecated in favor of @SuppressFBWarnings because of the name clash with Java's SuppressWarnings .请注意,由于 FindBugs 3.0.0 SuppressWarnings已被弃用,取而代之的是@SuppressFBWarnings因为名称与 Java 的SuppressWarnings冲突。

As others Mentioned, you can use the @SuppressFBWarnings Annotation.正如其他人提到的,您可以使用@SuppressFBWarnings注释。 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.如果你不想或不能在你的代码中添加另一个依赖,你可以自己在你的代码中添加 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

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 过滤器文件。

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查找特定消息

发现错误

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

At the time of writing this (May 2018), FindBugs seems to have been replaced by SpotBugs .在撰写本文时(2018 年 5 月), FindBugs 似乎已被SpotBugs取代。 Using the SuppressFBWarnings annotation requires your code to be compiled with Java 8 or later and introduces a compile time dependency on spotbugs-annotations.jar .使用SuppressFBWarnings注释需要使用 Java 8 或更高版本编译您的代码,并引入对spotbugs-annotations.jar的编译时依赖性。

Using a filter file to filter SpotBugs rules has no such issues.使用过滤器文件过滤 SpotBugs 规则没有这样的问题。 The documentation is here .文档在这里

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 provided scope:你需要在你的 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 @SuppressFBWarnings there is another dependency which provides @SuppressWarnings .这允许使用@SuppressFBWarnings还有另一个提供@SuppressWarnings依赖@SuppressWarnings However, the above is clearer.不过,上面说的更清楚了。

Then you add the annotation above your method:然后在方法上方添加注释:

Eg例如

@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 bug and also a reason why you're disabling the scan for it.这包括错误的名称以及您禁用对其进行扫描的原因。

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.SuppressWarnings so 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.字段上的 @SuppressWarnings 仅抑制为该字段声明报告的 findbugs 警告,而不是与该字段关联的每个警告。

For example, this suppresses the "Field only ever set to null" warning:例如,这会抑制“Field only ever set to null”警告:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM