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
.
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
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来清除错误。
归档时间: |
|
查看次数: |
94725 次 |
最近记录: |