ThreadLocal init 上的 FindBugs 警告
FindBugs warning on ThreadLocal init
我有 ThreadLocal
实例,它是用覆盖的 initValue
方法初始化的。我还用 @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;
}
};
声纳报告仍然抱怨 "Performance - Could be re factored into a named static inner class"。
我怎样才能确保上述投诉被忽略,或者我可以采取什么最好的方式来避免这种投诉。
按照 Sonar 的建议进行操作 "Could be re factored into a named static inner class"。
命名静态内部 class:
class MyClass {
static class MyThreadLocal extends ThreadLocal<Integer> {
@Override
protected Integer initialValue() {
return 0;
}
}
private ThreadLocal<Integer> dbSwitchCount = new MyThreadLocal();
}
我认为 Sonar 认为这是 "Performance" 改进的原因是因为匿名 class 是非静态的,将其设为静态可以改善内存管理。
注释不起作用,因为您注释的是 dbSwitchCount
字段,而不是匿名 class 并且错误报告未绑定到该字段。 XML:
中的错误报告如下所示
<BugInstance type="SIC_INNER_SHOULD_BE_STATIC_ANON" priority="3" rank="20" abbrev="SIC"
category="PERFORMANCE" first="1">
<Class classname="MyClass">
<SourceLine classname="MyClass" start="1" end="10"
sourcefile="MyClass.java" sourcepath="MyClass.java"/>
</Class>
<SourceLine classname="MyClass" start="7" end="7" startBytecode="0" endBytecode="0"
sourcefile="MyClass.java" sourcepath="MyClass.java"/>
</BugInstance>
看,dbSwitchCount
字段没有任何内容。因此,当 suppress-filter 起作用时,它不知道 dbSwitchCount
注释与此错误报告有某种联系。不幸的是,我看不到注释匿名 class 本身的方法。在不更改实际代码的情况下抑制此警告的唯一方法是注释外部 class 而不是:
@SuppressFBWarnings("SIC_INNER_SHOULD_BE_STATIC_ANON")
public class MyClass {
private final ThreadLocal<Integer> dbSwitchCount=new ThreadLocal<Integer>() {
@Override
protected Integer initialValue() {
return 0;
}
};
}
这样警告就会消失(顺便说一下,建议改用 @SuppressFBWarnings
注释)。
一般情况下,实例绑定(非静态)线程局部变量是可疑的。例如,参见 this question。因此,最初的问题可能是 dbSwitchCount
应该声明为静态的(这样 SIC_INNER_SHOULD_BE_STATIC_ANON
警告也会消失)。
更新 我检查了这个检测器的 FindBugs 代码。看起来可以添加缺少的错误注释,从而可以禁止警告注释字段或封闭方法。我在我们的错误跟踪器中创建了一个 ticket。
UPDATE-2 Fixed in FindBugs trunk.
我有 ThreadLocal
实例,它是用覆盖的 initValue
方法初始化的。我还用 @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;
}
};
声纳报告仍然抱怨 "Performance - Could be re factored into a named static inner class"。 我怎样才能确保上述投诉被忽略,或者我可以采取什么最好的方式来避免这种投诉。
按照 Sonar 的建议进行操作 "Could be re factored into a named static inner class"。
命名静态内部 class:
class MyClass {
static class MyThreadLocal extends ThreadLocal<Integer> {
@Override
protected Integer initialValue() {
return 0;
}
}
private ThreadLocal<Integer> dbSwitchCount = new MyThreadLocal();
}
我认为 Sonar 认为这是 "Performance" 改进的原因是因为匿名 class 是非静态的,将其设为静态可以改善内存管理。
注释不起作用,因为您注释的是 dbSwitchCount
字段,而不是匿名 class 并且错误报告未绑定到该字段。 XML:
<BugInstance type="SIC_INNER_SHOULD_BE_STATIC_ANON" priority="3" rank="20" abbrev="SIC"
category="PERFORMANCE" first="1">
<Class classname="MyClass">
<SourceLine classname="MyClass" start="1" end="10"
sourcefile="MyClass.java" sourcepath="MyClass.java"/>
</Class>
<SourceLine classname="MyClass" start="7" end="7" startBytecode="0" endBytecode="0"
sourcefile="MyClass.java" sourcepath="MyClass.java"/>
</BugInstance>
看,dbSwitchCount
字段没有任何内容。因此,当 suppress-filter 起作用时,它不知道 dbSwitchCount
注释与此错误报告有某种联系。不幸的是,我看不到注释匿名 class 本身的方法。在不更改实际代码的情况下抑制此警告的唯一方法是注释外部 class 而不是:
@SuppressFBWarnings("SIC_INNER_SHOULD_BE_STATIC_ANON")
public class MyClass {
private final ThreadLocal<Integer> dbSwitchCount=new ThreadLocal<Integer>() {
@Override
protected Integer initialValue() {
return 0;
}
};
}
这样警告就会消失(顺便说一下,建议改用 @SuppressFBWarnings
注释)。
一般情况下,实例绑定(非静态)线程局部变量是可疑的。例如,参见 this question。因此,最初的问题可能是 dbSwitchCount
应该声明为静态的(这样 SIC_INNER_SHOULD_BE_STATIC_ANON
警告也会消失)。
更新 我检查了这个检测器的 FindBugs 代码。看起来可以添加缺少的错误注释,从而可以禁止警告注释字段或封闭方法。我在我们的错误跟踪器中创建了一个 ticket。
UPDATE-2 Fixed in FindBugs trunk.