我想抑制特定字段或局部变量的FindBugs警告。FindBugs文档的Target
可以是Type
、Field
、Method
、Parameter
、Constructor
、Package
,因为它的edu.umd.cs.findbugs.annotations.SuppressWarning
批注1。但我不能对字段进行批注,只有当我批注方法时,警告才会取消。
注释整个方法对我来说似乎很宽泛。有没有办法抑制特定字段上的警告?还有另一个相关的问题2,但没有答案。
演示代码:
public class SyncOnBoxed
{
static int counter = 0;
// The following SuppressWarnings does NOT prevent the FindBugs warning
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value="DL_SYNCHRONIZATION_ON_BOXED_PRIMITIVE")
final static Long expiringLock = new Long(System.currentTimeMillis() + 10);
public static void main(String[] args) {
while (increment(expiringLock)) {
System.out.println(counter);
}
}
// The following SuppressWarnings prevents the FindBugs warning
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value="DL_SYNCHRONIZATION_ON_BOXED_PRIMITIVE")
protected static boolean increment(Long expiringLock)
{
synchronized (expiringLock) { // <<< FindBugs warning is here: Synchronization on Long in SyncOnBoxed.increment()
counter++;
}
return expiringLock > System.currentTimeMillis(); // return false when lock is expired
}
}
发布于 2013-01-25 04:01:14
字段上的@SuppressFBWarnings
只抑制为该字段声明报告的findbugs警告,而不是与该字段关联的所有警告。
例如,这将取消"Field only ever set to null“警告:
@SuppressFBWarnings("UWF_NULL_FIELD")
String s = null;
我认为最好的做法是将带有警告的代码隔离到尽可能小的方法中,然后在整个方法中隐藏警告。
注意:@SuppressWarnings
被标记为deprecated而支持@SuppressFBWarnings
发布于 2015-05-29 22:01:47
检查http://findbugs.sourceforge.net/manual/filter.html#d0e2318有一个可与方法标记一起使用的本地标记。在这里,您可以为特定的局部变量指定应该排除的错误。示例:
<FindBugsFilter>
<Match>
<Class name="<fully-qualified-class-name>" />
<Method name="<method-name>" />
<Local name="<local-variable-name-in-above-method>" />
<Bug pattern="DLS_DEAD_LOCAL_STORE" />
</Match>
</FindBugsFilter>
https://stackoverflow.com/questions/14503001
复制相似问题