如何正确使用@SuppressFBWarnings忽略printStackTrace findbugs警告

tho*_*inn 3 java findbugs printstacktrace

在我的junit 4测试代码中,我正在使用包含如下代码的测试规则:

catch (Throwable t) {
   t.printStackTrace();
   throw t;
}
Run Code Online (Sandbox Code Playgroud)

Findbugs对此有所抱怨,这是正确的-我们的生产代码中不应这样做。但是,在这种情况下,我认为用法是合理的,我尝试使用@SuppressFBWarnings注释使findbugs静音。但是,两者都没有

@SuppressFBWarnings
private void warmUp() throws Throwable {
Run Code Online (Sandbox Code Playgroud)

也不

@SuppressFBWarnings("IMC_IMMATURE_CLASS_PRINTSTACKTRACE")
private void warmUp() throws Throwable {
Run Code Online (Sandbox Code Playgroud)

也不

@SuppressFBWarnings({"IMC_IMMATURE_CLASS_PRINTSTACKTRACE"})
private void warmUp() throws Throwable {
Run Code Online (Sandbox Code Playgroud)

有理想的结果。

如何正确使用@SuppressFBWarnings取消警告?

小智 6

通过对FindBugs批注使用以下gradle依赖关系:

compile 'com.google.code.findbugs:findbugs-annotations:3.0.1'
Run Code Online (Sandbox Code Playgroud)

这是应该起作用的方法:

@SuppressFBWarnings(value = "IMC_IMMATURE_CLASS_PRINTSTACKTRACE", justification = "Document why this should be ignored here")
private void warmUp() throws Throwable {}
Run Code Online (Sandbox Code Playgroud)