首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何抑制字段或局部变量的FindBugs警告?

如何抑制字段或局部变量的FindBugs警告?
EN

Stack Overflow用户
提问于 2013-01-24 21:54:33
回答 2查看 44.3K关注 0票数 28

我想抑制特定字段或局部变量的FindBugs警告。FindBugs文档的Target可以是TypeFieldMethodParameterConstructorPackage,因为它的edu.umd.cs.findbugs.annotations.SuppressWarning批注1。但我不能对字段进行批注,只有当我批注方法时,警告才会取消。

注释整个方法对我来说似乎很宽泛。有没有办法抑制特定字段上的警告?还有另一个相关的问题2,但没有答案。

1

2

演示代码:

代码语言:javascript
运行
复制
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
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-25 04:01:14

字段上的@SuppressFBWarnings只抑制为该字段声明报告的findbugs警告,而不是与该字段关联的所有警告。

例如,这将取消"Field only ever set to null“警告:

代码语言:javascript
运行
复制
@SuppressFBWarnings("UWF_NULL_FIELD")
String s = null;

我认为最好的做法是将带有警告的代码隔离到尽可能小的方法中,然后在整个方法中隐藏警告。

注意:@SuppressWarnings被标记为deprecated而支持@SuppressFBWarnings

票数 31
EN

Stack Overflow用户

发布于 2015-05-29 22:01:47

检查http://findbugs.sourceforge.net/manual/filter.html#d0e2318有一个可与方法标记一起使用的本地标记。在这里,您可以为特定的局部变量指定应该排除的错误。示例:

代码语言:javascript
运行
复制
<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>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14503001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档