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

Java 有没有办法忽略单个FindBugs警告?,java,warnings,findbugs,suppress-warnings,Java,Warnings,Findbugs,Suppress Warnings,对于PMD,如果要忽略特定警告,可以使用//NOPMD忽略该行 FindBugs有类似的功能吗?FindBugs的初始方法涉及XML配置文件。这确实不如PMD解决方案方便,但FindBugs在字节码上工作,而不是在源代码上,因此注释显然不是一个选项。例如: 然而,为了解决这个问题,FindBugs后来引入了另一个基于(请参阅)的解决方案,您可以在类或方法级别使用它(我认为比XML更方便)。示例(可能不是最好的示例,但它只是一个示例): 请注意,自从FindBugs 3.0.0Suppress

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


FindBugs有类似的功能吗?

FindBugs的初始方法涉及XML配置文件。这确实不如PMD解决方案方便,但FindBugs在字节码上工作,而不是在源代码上,因此注释显然不是一个选项。例如:


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


请注意,自从FindBugs 3.0.0
SuppressWarnings
被弃用,取而代之的是
@SuppressFBWarnings
,因为名称与Java的
SuppressWarnings
冲突,下面是一个更完整的XML过滤器示例(上面的示例本身不起作用,因为它只显示了一个片段,并且缺少
开始和结束标记):



如果您使用的是Android Studio FindBugs插件,请使用file->Other Settings->Default Settings->Other Settings->FindBugs IDEA->filter->Exclude filter files->Add浏览到您的XML筛选文件。

我将此文件留在这里:


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

@字段上的SuppressWarnings仅抑制findbugs警告 为该字段声明报告,而不是与 那块地

例如,这将禁止“字段仅设置为null” 警告:

@SuppressWarnings(“UWF\u NULL\u字段”)字符串s=NULL;我认为最好 您可以做的是将带有警告的代码隔离到最小值 方法,然后抑制整个方法上的警告


更新梯度

dependencies {
    compile group: 'findbugs', name: 'findbugs', version: '1.0.0'
}
找到FindBugs报告

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

查找特定消息

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

导入注释的正确版本

import edu.umd.cs.findbugs.annotations.SuppressWarnings;
将注释直接添加到有问题的代码上方

@SuppressWarnings("OUT_OF_RANGE_ARRAY_INDEX")

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

如其他人所述,您可以使用
@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 "";
}

来源:

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


使用筛选文件筛选SpotBugs规则没有此类问题。文档是。

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

本着完整性的精神:

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

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

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

附加问题:如何为给定报告的“错误”(使用sonar)找到合适的值?当然,使用注释方法的问题是,您的代码不必要地导入(和后续依赖项)Findbugs库:(@AshleyWalton注释的保留期是类,因此至少对于那些Maven用户来说,它只是一个编译时依赖项,您可以使用以下内容导入注释。(另外,设置范围是为了使您的项目在运行时不依赖FindBugs).net.sourceforge.findbugs annotations 1.3.2提供的
Maven用户应将
com.google.code.FindBugsAnnotations 3.0.0提供的
添加到POM中,如果他们想使用
@SuppressFBWarnings
java.lang.SuppressWarnings
无法工作。它具有源代码保留,因此对findbugs不可见。您可以使用
compile'net.sourceforge.findbugs:annotations:1.3.2'
语法较短。+1,但请更新您的答案:gradle
testCompile'com.google.code.findbugs:annotations:3.0.0'
和annotation name
@suppressFBWarning/code>您指向Spotbug的链接似乎不正确。我在上找到了它。
<dependency>
  <groupId>com.google.code.findbugs</groupId>
  <artifactId>findbugs-annotations</artifactId>
  <version>3.0.1</version>
  <scope>provided</scope>
</dependency>
@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))) { ... }