简体   繁体   English

ThreadLocal初始化上的FindBugs警告

[英]FindBugs warning on ThreadLocal init

I have ThreadLocal instance which was initialized with overridden initValue method. 我有ThreadLocal实例,该实例已使用重写的initValue方法初始化。 Also I have annotated it with @edu.umd.cs.findbugs.annotations.SuppressWarnings("SIC_INNER_SHOULD_BE_STATIC_ANON") as follows. 我也用@edu.umd.cs.findbugs.annotations.SuppressWarnings("SIC_INNER_SHOULD_BE_STATIC_ANON")进行了注释,如下所示。

@edu.umd.cs.findbugs.annotations.SuppressWarnings("SIC_INNER_SHOULD_BE_STATIC_ANON")
private ThreadLocal<Integer> dbSwitchCount=new ThreadLocal<Integer>() {
    @Override 
    protected Integer initialValue() {
        return 0;
    }
};

Still Sonar report complains about "Performance - Could be re factored into a named static inner class". 声纳报告仍然抱怨“性能-可以分解为命名的静态内部类”。 How can I make sure the above complain being ignored or what best way I can avoid that complain. 我如何确保上述抱怨被忽略,或者如何避免该抱怨?

Do what Sonar suggests "Could be re factored into a named static inner class". 进行Sonar建议的操作:“可以将其分解为命名的静态内部类”。

Named static inner class: 命名为静态内部类:

class MyClass {
    static class MyThreadLocal extends ThreadLocal<Integer> {
        @Override 
        protected Integer initialValue() {
            return 0;
        }
    }
    private ThreadLocal<Integer> dbSwitchCount = new MyThreadLocal();
}

I think the reason Sonar thinks that's a "Performance" improvement is because the anonymous class is non-static, and making it static improves memory management. 我认为Sonar认为这是“性能”改进的原因是因为匿名类是非静态的,而使其变为静态可改善内存管理。

The annotation does not work, because you are annotating the dbSwitchCount field, not the anonymous class and the bug report is not bound to the field. 注释不起作用,因为您正在注释dbSwitchCount字段,而不是匿名类,并且错误报告未绑定到该字段。 Here's how bug report looks like in XML: 错误报告在XML中的样子如下:

<BugInstance type="SIC_INNER_SHOULD_BE_STATIC_ANON" priority="3" rank="20" abbrev="SIC" 
             category="PERFORMANCE" first="1">
  <Class classname="MyClass$1">
    <SourceLine classname="MyClass$1" start="1" end="10" 
                sourcefile="MyClass.java" sourcepath="MyClass.java"/>
  </Class>
  <SourceLine classname="MyClass$1" start="7" end="7" startBytecode="0" endBytecode="0" 
              sourcefile="MyClass.java" sourcepath="MyClass.java"/>
</BugInstance>

See, nothing about the dbSwitchCount field. dbSwitchCount有关dbSwitchCount字段的任何内容。 Thus when suppress-filter works, it does not know that dbSwitchCount annotation is somehow connected with this bug report. 因此,当抑制过滤器工作时,它不知道dbSwitchCount批注以某种方式与此错误报告相关联。 And unfortunately I see no way to annotate the anonymous class itself. 不幸的是,我没有办法注释匿名类本身。 The only thing you can do to suppress this warning without changing the actual code is to annotate the outer class instead: 在不更改实际代码的情况下,唯一可以禁止显示此警告的方法是注释外部类:

@SuppressFBWarnings("SIC_INNER_SHOULD_BE_STATIC_ANON")
public class MyClass {

    private final ThreadLocal<Integer> dbSwitchCount=new ThreadLocal<Integer>() {
        @Override 
        protected Integer initialValue() {
            return 0;
        }
    };
}

This way the warning disappears (btw it's recommended to use @SuppressFBWarnings annotation instead). 这样,警告消失了(建议使用@SuppressFBWarnings注释代替)。

In general instance-bound (non-static) thread-locals are suspicious. 通常,实例绑定(非静态)线程本地是可疑的。 See, for example, this question . 参见,例如, 这个问题 Thus probably the original problem is that dbSwitchCount should be declared as static (this way the SIC_INNER_SHOULD_BE_STATIC_ANON warning will disappear as well). 因此,可能最初的问题是dbSwitchCount应该声明为静态(这样SIC_INNER_SHOULD_BE_STATIC_ANON警告也将消失)。

UPDATE I examined the FindBugs code for this detector. 更新我检查了此检测器的FindBugs代码。 Looks like it's possible to add the missing bug annotation, to make it possible to suppress warning annotating the field or the enclosing method. 看起来有可能添加缺少的bug注释,从而可以消除警告注释字段或封闭方法的警告。 I've created a ticket in our bug tracker. 我已经在我们的错误跟踪器中创建了一个票证

UPDATE-2 Fixed in FindBugs trunk . UPDATE-2已 在FindBugs主干中修复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM