javac数据流分析的奇怪的假阳性 [英] Weird false-positive of javac data flow analysis

查看:201
本文介绍了javac数据流分析的奇怪的假阳性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下形式的代码:

  class Test {
private final A t;

public Test(){

for(...:...){
final A u = null;
}

t = new A();
}

私人类A {}
}

编译器说:

 变量t可能已分配





  • 将循环内容更改为 A u = null

  • 删除循环(但保留 final A u = null;

  • 经典计数循环



这是怎么回事?



注意:我无法得到最小的例子,导致错误,所以有可能是环境(约1400 loc)有问题。我不能看到什么可以打扰 t 的初始化,虽然,因为 t 写到其他地方。 p>

有趣的事实:IntelliJ IDEA说如果我删除它,变量'u'可以有'final'修饰符...。



我使用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;
}
}
}

c $ c> javac 1.6.0_26 但编译在 javac 1.7.0_02



请注意,你可以做任何




  • 删除任何一个成员
  • 删除中的 ()

  • 替换循环,例如 for(int i = 0; i <100; i ++){...}


$ b b

它会编译。

解决方案

由于问题在Java 7中已修复, Java 6编译器。


I have code of the following form:

class Test {
  private final A t;

  public Test() {

    for ( ... : ... ) {
      final A u = null;
    }

    t = new A();
  }

  private class A {}
}

Compiler says:

variable t might already have been assigned

Interestingly, if I perform any of the following changes to the loop it works out!

  • Change the loop's content to A u = null
  • Remove the loop (but keep final A u = null;)
  • Replace the foreach-style loop with a classic counting loop

What is going on here?

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.

Fun fact: IntelliJ IDEA says "Variable 'u' can have 'final' modifier..." if I remove it.

I use javac 1.6.0_26.

Update: There you go, this example so so minimal:

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;
    }
  }
}

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

  • Remove any one member
  • Remove final inside the loop in A()
  • Replace the loop with a normal for loop, e.g. for ( int i=0; i<100; i++ ) { ... }

and it will compile.

解决方案

As the problem is fixed in Java 7, it is probably a bug in the Java 6 compiler.

这篇关于javac数据流分析的奇怪的假阳性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆