使用PMD,如果您想忽略特定的警告,可以使用// NOPMD来忽略该行。
FindBugs也有类似的东西吗?
使用PMD,如果您想忽略特定的警告,可以使用// NOPMD来忽略该行。
FindBugs也有类似的东西吗?
当前回答
正如其他人提到的,您可以使用@SuppressFBWarnings Annotation。 如果不希望或不能向代码中添加另一个依赖项,则可以自己将Annotation添加到代码中,Findbugs并不关心Annotation位于哪个包中。
@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/ # 5 e88
其他回答
FindBugs最初的方法涉及XML配置文件,也就是过滤器。这确实不如PMD解决方案方便,但是FindBugs处理字节码,而不是源代码,因此注释显然不是一个选项。例子:
<Match>
<Class name="com.mycompany.Foo" />
<Method name="bar" />
<Bug pattern="DLS_DEAD_STORE_OF_CLASS_LITERAL" />
</Match>
但是,为了解决这个问题,FindBugs后来引入了另一种基于注释的解决方案(参见SuppressFBWarnings),您可以在类或方法级别使用它(在我看来比XML更方便)。例子(也许不是最好的,但这只是一个例子):
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
value="HE_EQUALS_USE_HASHCODE",
justification="I know what I'm doing")
注意,由于与Java的SuppressWarnings名称冲突,FindBugs 3.0.0已经弃用了SuppressWarnings,取而代之的是@SuppressFBWarnings。
下面是一个更完整的XML过滤器示例(上面的示例本身将不起作用,因为它只显示了一个片段,并且缺少<FindBugsFilter>开始和结束标记):
<FindBugsFilter>
<Match>
<Class name="com.mycompany.foo" />
<Method name="bar" />
<Bug pattern="NP_BOOLEAN_RETURN_NULL" />
</Match>
</FindBugsFilter>
如果您正在使用Android Studio FindBugs插件,浏览到您的XML过滤器文件使用文件->其他设置->默认设置->其他设置->FindBugs- idea ->过滤器->排除过滤文件->添加。
我把这个留在这里:https://stackoverflow.com/a/14509697/1356953
请注意,这适用于java.lang. suppresswarningso不需要使用单独的注释。
字段上的@SuppressWarnings仅抑制findbugs警告 为该字段声明报告,而不是与之关联的每个警告 该字段。 例如,这将抑制“字段只设置为null” 警告: @SuppressWarnings("UWF_NULL_FIELD") String s = null;我认为最好的 您所能做的就是将带有警告的代码隔离到最小 方法可以,然后压制整个方法上的警告。
更新它
dependencies {
compile group: 'findbugs', name: 'findbugs', version: '1.0.0'
}
查找FindBugs报告
file:///Users/your_user/IdeaProjects/projectname/build/reports/findbugs/main.html
找到特定的消息
导入注释的正确版本
import edu.umd.cs.findbugs.annotations.SuppressWarnings;
将注释直接添加到违规代码的上方
@SuppressWarnings("OUT_OF_RANGE_ARRAY_INDEX")
更多信息请参见这里:findbugs Spring Annotation
正如其他人提到的,您可以使用@SuppressFBWarnings Annotation。 如果不希望或不能向代码中添加另一个依赖项,则可以自己将Annotation添加到代码中,Findbugs并不关心Annotation位于哪个包中。
@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/ # 5 e88