javac数据流分析的奇怪误报 [英] Weird false-positive of javac data flow analysis
问题描述
我有以下形式的代码:
class Test {
private final A t;
public Test() {
for ( ... : ... ) {
final A u = null;
}
t = new A();
}
private class A {}
}
编译器说:
variable t might already have been assigned
有趣的是,如果我对循环执行以下任何更改,它就会起作用!
Interestingly, if I perform any of the following changes to the loop it works out!
- 将循环的内容更改为
A u = null
- 移除循环(但保持
final A u = null;
) - 用经典的计数循环替换 foreach 风格的循环
这里发生了什么?
注意:我无法获得导致错误的最小示例,因此环境"(大约 1400 loc)可能有问题.不过,我看不出什么会干扰 t
的初始化,因为 t
没有写入其他任何地方.
Note: I could not get the minimal example to cause the error so there is probably something wrong with the "environment" (about 1400 loc). I can not see what could disturb the initialisation of t
, though, as t
is written to nowhere else.
有趣的事实:IntelliJ IDEA 说变量 'u' 可以有 'final' 修饰符..."如果我删除它.
Fun fact: IntelliJ IDEA says "Variable 'u' can have 'final' modifier..." if I remove it.
我使用 javac 1.6.0_26.
I use javac 1.6.0_26.
更新:好了,这个例子那么最小:
import java.util.List;
class A {
private final boolean a;
public A() {
for ( final Object o : new Object[] {} ) {
final Object sh = null;
}
a = true;
}
class B {
private final Object b1;
private final Object b2;
B() {
b1 = null;
b2 = null;
}
}
}
在 javac 1.6.0_26
上编译失败,但在 javac 1.7.0_02
上编译.所以我想我遇到了一些邪恶的角落案例......某事?
Fails to compile on javac 1.6.0_26
but compiles on javac 1.7.0_02
. So I guess I hit some wicked corner case of ... something?
请注意,您可以执行任何操作
Note that you can do any of
- 删除任何一名成员
- 删除
A()
中循环内的 - 用普通的
for
循环替换循环,例如for ( int i=0; i<100; i++ ) { ... }
final
- Remove any one member
- Remove
final
inside the loop inA()
- Replace the loop with a normal
for
loop, e.g.for ( int i=0; i<100; i++ ) { ... }
它会编译.
推荐答案
由于该问题已在 Java 7 中修复,因此可能是 Java 6 编译器中的错误.
As the problem is fixed in Java 7, it is probably a bug in the Java 6 compiler.
这篇关于javac数据流分析的奇怪误报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!