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

问题描述

我有以下形式的代码:

  
  • 删除中的 ()

  • 用替换循环,例如 for(int i = 0; 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数据流分析的奇怪的假阳性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    05-27 09:49
    查看更多