对于PMD,如果您想忽略某个特定的警告,可以使用// NOPMD
让该行被忽略。
FindBugs也有类似的东西吗?
发布于 2009-12-02 08:34:33
FindBugs的初始方法涉及到XML文件,也称为filters。这确实不如PMD解决方案方便,但是FindBugs在字节码上工作,而不是在源代码上工作,所以注释显然不是一个选项。示例:
<Match>
<Class name="com.mycompany.Foo" />
<Method name="bar" />
<Bug pattern="DLS_DEAD_STORE_OF_CLASS_LITERAL" />
</Match>
但是,为了解决这个问题,FindBugs后来引入了另一个基于annotations的解决方案(请参阅SuppressFBWarnings
),您可以在类或方法级别使用该解决方案(我认为它比XML更方便)。示例(可能不是最好的示例,但这只是一个示例):
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
value="HE_EQUALS_USE_HASHCODE",
justification="I know what I'm doing")
请注意,从Java3.0.0开始,SuppressWarnings
就被弃用,取而代之的是@SuppressFBWarnings
,因为它的名称与FindBugs的SuppressWarnings
冲突。
发布于 2017-07-06 19:09:37
正如其他人所提到的,您可以使用@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 "";
}
来源:https://sourceforge.net/p/findbugs/feature-requests/298/#5e88
发布于 2014-06-02 23:56:53
下面是一个更完整的XML filter示例(上面的示例本身不会起作用,因为它只显示了一个代码片段,并且缺少<FindBugsFilter>
开始和结束标记):
<FindBugsFilter>
<Match>
<Class name="com.mycompany.foo" />
<Method name="bar" />
<Bug pattern="NP_BOOLEAN_RETURN_NULL" />
</Match>
</FindBugsFilter>
如果您使用的是Android Studio插件,请使用文件->其他设置->默认设置->其他设置-> FindBugs -IDEA->过滤器->排除过滤器文件->添加来浏览您的FindBugs过滤器文件。
https://stackoverflow.com/questions/1829904
复制相似问题