我想抑制特定字段或局部变量的FindBugs警告.FindBugs文档指出Target的edu.umd.cs.findbugs.annotations.SuppressWarning注释[1]可以是Type,Field,Method,Parameter,Constructor,Package.但是,只有当我注释警告被抑制的方法时,它才能对我进行注释.
注释整个方法似乎对我来说很广泛.有没有办法抑制特定字段的警告?还有另一个相关问题[2],但没有答案.
[1] http://findbugs.sourceforge.net/manual/annotations.html
演示代码:
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 …
Run Code Online (Sandbox Code Playgroud)