为什么Eclipse会抱怨死代码?

时间:2012-11-26 10:32:23

标签: java eclipse dead-code

Eclipse继续声明最后的elseif和else是死代码,但我不明白。

if (img0 != null && img1 != null) {
    code;

} else if (img0 != null) {
    code;
} else if (img1 != null) {
    code;
} else {
    code;
}

我的理由是这样的:

  1. 如果bote img0和img1不为空,则if计算为真
  2. 如果它评估为假则
    • img0为空或
    • img1为空或
    • img0和img1都为空。
  3. 如果img0不为null,则第一个elseif求值为true,如果求值为false,则img1可能不为null或者img0和img1都为null
  4. 我错过了什么,“死亡”在哪里?

    提前致谢。

5 个答案:

答案 0 :(得分:4)

以这两种方式查看代码的使用: -

方式1: -

public static void main(String[] args)
{
    String img0 = null;
    String img1 = "Asdf";

    /** Currently there is no code here, that can modify the value of 
        `img0` and `img1` and Compiler is sure about that. 
    **/

    /** So, it's sure that the below conditions will always execute in a
        certain execution order. And hence it will show `Dead Code` warning in 
        either of the blocks depending upon the values.
    **/

    if (img0 != null && img1 != null) {
       // code;

    } else if (img0 != null) {
        //code;

    } else if (img1 != null) {
        //code;
    } else {
       // code;
    }
}

在这种情况下,您肯定会在一个块或另一个块上收到dead code警告,因为您正在块之前设置值,并且编译器确保值在初始化之间不会更改并执行这些街区。

方式2: -

public static void main(String[] args)
{
    String img0 = null;
    String img1 = "Asdf";

    show(img0, img1);
}

public static void show(String img0, String img1) {

    /** Now here, compiler cannot decide on the execution order, 
        as `img0` and `img1` can have any values depending upon where 
        this method was called from. And hence it cannot give dead code warning.
    **/

    if (img0 != null && img1 != null) {
       // code;

    } else if (img0 != null) {
        //code;

    } else if (img1 != null) {
        //code;
    } else {
       // code;
    }
}

现在,在这种情况下,您不会收到dead code警告,因为编译器不确定,可能会调用show方法。 img0img1的值可以是方法内的任何值。

  • 如果两者都是null,则会执行最后一次else
  • 如果one of them is nullelse if中的一个将被执行。
  • 并且,如果它们都不是null,您的if将被执行。

注意: -

如果需要,您可以将Eclipse配置为不对某些情况显示warnings,例如 - Unneccessary elseUnused Imports等。

  

转到Windows - >偏好 - > Java(在左侧面板上) - >编译器 - >   错误/警告

答案 1 :(得分:1)

Eclipse可以在U进入评估点之前弄清楚这些变量处于哪种状态。所以它知道它的状态,并通过这个设置其他条件“死”,例如

String img0 = "";
String img1 = null;
if(img0 != null && img1 != null)
{
    int i = 0;
}
else if(img0 != null)
{
    int i = 10;
}
else if(img1 != null)
{
    int i = 100;
}
else
{
    int i = 1000;
}

只有第二个条件没有死,彼此是。

答案 2 :(得分:0)

这没有任何问题。你的错误必须在其他地方!

public static void main(String[] args) {
    String img0 = nullOrString();
    String img1 = nullOrString();
    if (img0 != null && img1 != null) {
       // code;
    } else if (img0 != null) {
       // code;
    } else if (img1 != null) {
       // code;
    } else {
       // code;
    }
}
public static String nullOrString() {
    return Math.random() > 0.5 ? null : "abc";
}

你能告诉我们一些更多的上下文,比如变量的初始化地点,以及它们可能被改变的地方吗?

当我将我的例子改为

    String img0 = null;
    String img1 = null;

else之外的所有内容都会有死代码警告,这完全合理(因为分析代码显示只会执行else)。

答案 3 :(得分:0)

当编译器发现你已经在创建实例时,它有时会发生,其中一个对象永远不会为null ... 例如

Image img1 = new Image(); 

在此之后,如果上述情况,你正在写作。

if (img0 != null && img1 != null) {
    code;

} else if (img0 != null) {
    code;
} else if (img1 != null) {
    code;
} else {
    code;
}

在这种情况下,其他条件永远不会达到,因为img1永远不会为空...

答案 4 :(得分:0)

Eclipse可以判断是否已设置变量。此代码不提供有关未使用代码的警告。两个变量都不为空:

public void test() {
    Bitmap img1 = Bitmap.createBitmap(48, 48, null);
    Bitmap img0 = Bitmap.createBitmap(48, 48, null);
    if (img0 != null && img1 != null) {
        test();
    } else if (img0 != null) {
        test();
    } else if (img1 != null) {
        test();
    } else {
        test();
    }
}

然而,这段代码确实发出警告,因为Eclipse可以告诉img0null,因此if和第一个if永远不会被命中:

public void test() {
    Bitmap img1 = Bitmap.createBitmap(48, 48, null);
    Bitmap img0 = null;
    if (img0 != null && img1 != null) {
        test();
    } else if (img0 != null) {
        test();
    } else if (img1 != null) {
        test();
    } else {
        test();
    }
}
相关问题