本文介绍了在Eclipse中,无法访问的代码错误与死亡代码警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道为什么:

  public void foo()
{
System.out。的println( 你好);
return;
System.out.println(World!);
}

将在Eclipse下报告为不可达错误,但

  public void foo()
{
System.out.println(Hello);
if(true)return;
System.out.println(World!);
}

只会触发死码警告?



我可以想到的唯一的解释是Java编译器只标记第一个,而在Eclipse中的一些额外的分析显示了第二个。但是,如果是这样的话,为什么Java编译器在编译时才能解决这个问题?



Java编译器不会在编译时弄清楚if(true)没有效果,因此产生基本相同的字节码?可访问的代码分析在什么时候应用?



我想一个更一般的方式来想到这个问题是:什么时候可以访问的代码分析应用?在将第二个Java代码片段转换为最终的字节码时,我确定在某些时候,if(true)运行时等价物被删除,并且两个程序的表示变得相同。 Java编译器不会再次应用其可达到的代码分析?

解决方案

第一个编译(你有错误),第二个编译得到警告)。关于为什么Eclipse检测到死代码,那么这只是一个集成开发工具的便利性,内置的编译器可以更精确地更新为反对JDK来检测这种代码。



更新:JDK实际上消除了死亡代码。

  public class Test {
public void foo(){
System.out.println(foo);
if(true)return;
System.out.println(foo);
}
public void bar(){
System.out.println(bar);
if(false)return;
System.out.println(bar);
}
}

javap -c 说:

 
public class Test extends java.lang.Object {
public Test();
代码:
0:aload_0
1:invokespecial#1; //方法java / lang / Object。:()V
4:return

public void foo();
代码:
0:getstatic#2; // Field java / lang / System.out:Ljava / io / PrintStream;
3:ldc#3; // String foo
5:invokevirtual#4; //方法java / io / PrintStream.println:(Ljava / lang / StrV
8:return

public void bar();
代码:
0: getstatic#2; // Field java / lang / System.out:Ljava / io / PrintStream;
3:ldc#5; // String bar
5:invokevirtual#4; //方法java / io / PrintStream.println:(Ljava / lang / String;)V
8:getstatic#2; //字段java / lang / System.out:Ljava / io / PrintStream;
11:ldc# 5; // String bar
13:invokevirtual#4; //方法java / io / PrintStream.println:(Ljava / lang / String;)V
16:return
$ b $关于为什么(Sun)没有给出警告,我不知道:)至少JDK的




Does anyone know why:

public void foo()
{
    System.out.println("Hello");
    return;
    System.out.println("World!");
}

Would be reported as an "unreachable error" under Eclipse, but

public void foo()
{
    System.out.println("Hello");
    if(true) return;
    System.out.println("World!");
}

Only triggers a "Dead code" warning?

The only explanation I can think of is that the Java compiler only flags the first, and that some extra analysis in Eclipse figures out the second. However, if that is the case, why can't the Java compiler figure out this case at compile time?

Wouldn't the Java compiler figure out at compile time that the if(true) has no effect, thus yielding bytecode that is essentially identical? At what point is the reachable code analysis applied?

I guess a more general way to think of this question is: "when is the reachable code analysis applied"? In the transformation of the second Java code fragment to the final bytecode, I am sure that at some point the "if(true)" runtime equivalent is removed, and the representations of the two programs become identical. Wouldn't the Java compiler then apply its reachable code analysis again?

解决方案

The first does not compile (you got an error), the second compiles (you just got a warning). That's the difference.

As to why Eclipse detects dead code, well, that's just the convenience of an integrated development tool with a built-in compiler which can be finetuned more as opposed to JDK to detect this kind of code.

Update: the JDK actually eliminates dead code.

public class Test {
    public void foo() {
        System.out.println("foo");
        if(true)return;
        System.out.println("foo");
    }
    public void bar() {
        System.out.println("bar");
        if(false)return;
        System.out.println("bar");
    }
}

javap -c says:

public class Test extends java.lang.Object{
public Test();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."":()V
   4:   return

public void foo();
  Code:
   0:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   ldc             #3; //String foo
   5:   invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/StrV
   8:   return

public void bar();
  Code:
   0:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   ldc             #5; //String bar
   5:   invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   8:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   11:  ldc             #5; //String bar
   13:  invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   16:  return

}

As to why it (Sun) doesn't give a warning about that, I have no idea :) At least the JDK compiler has actually DCE (Dead Code Elimination) builtin.

这篇关于在Eclipse中,无法访问的代码错误与死亡代码警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 21:51
查看更多