Eclipse bug?仅使用默认情况打开null [英] Eclipse bug? Switching on a null with only default case

查看:228
本文介绍了Eclipse bug?仅使用默认情况打开null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用枚举,我发现以下编译并在Eclipse上运行正常(Build id:20090920-1017,不知道确切的编译器版本):

I was experimenting with enum, and I found that the following compiles and runs fine on Eclipse (Build id: 20090920-1017, not sure exact compiler version):

public class SwitchingOnAnull {
    enum X { ,; }
    public static void main(String[] args) {
        X x = null;
        switch(x) {
            default: System.out.println("Hello world!");
        }
    }
}

使用Eclipse编译并运行时,这将打印Hello world!并正常退出。

When compiled and run with Eclipse, this prints "Hello world!" and exits normally.

使用 javac 编译器,这将按预期的方式抛出一个 NullPointerException

With the javac compiler, this throws a NullPointerException as expected.

Eclipse中有一个错误Java编译器?

So is there a bug in Eclipse Java compiler?

推荐答案

这是一个错误。以下是根据Java语言规范第3版

This is a bug. Here's the specified behavior for a switch statement according to the Java Language Specification, 3rd Edition:


SwitchStatement:
    switch ( Expression ) SwitchBlock

开关被执行,首先评估 Expression 。如果 Expression 评估为 null ,则会抛出一个 NullPointerException 并且因为这个原因,整个开关语句突然完成。

When the switch statement is executed, first the Expression is evaluated. If the Expression evaluates to null, a NullPointerException is thrown and the entire switch statement completes abruptly for that reason.

显然是bug在Eclipse中与 default case或 enum 无关。

Apparently the bug in Eclipse has nothing to do with default case or enum at all.

public class SwitchingOnAnull {
    public static void main(String[] args) {        
        java.math.RoundingMode x = null;
        switch(x) {};

        switch((Integer) null) {};

        switch((Character) null) {
            default: System.out.println("I've got sunshine!");
        }       
    }
}

上述代码编译并运行好(至少有一些版本的)Eclipse。每个个人开关在使用 javac 编译时抛出一个 NullPointerException 这完全符合规范的要求。

The above code compiles and runs "fine" on (at least some version of) Eclipse. Each individual switch throws a NullPointerException when compiled with javac, which is exactly as the specification mandates.

在Eclipse下编译时,这里是 javap -c SwitchingOnAnull

Here's javap -c SwitchingOnAnull when compiled under Eclipse:

Compiled from "SwitchingOnAnull.java"
public class SwitchingOnAnull extends java.lang.Object{
public SwitchingOnAnull();
Code:
 0: aload_0
 1: invokespecial  #8; //Method java/lang/Object."<init>":()V
 4: return

public static void main(java.lang.String[]);
Code:
 0: aconst_null
 1: astore_1
 2: getstatic     #16; //Field java/lang/System.out:Ljava/io/PrintStream;
 5: ldc           #22; //String I've got sunshine!
 7: invokevirtual #24; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
10: return

}

似乎Eclipse编译器完全摆脱了整个开关构造。不幸的是,这种优化打破了语言规范。

It seems that the Eclipse compiler gets rid of the entire switch constructs entirely. Unfortunately this optimization breaks the language specification.

该错误已经提交并分配修复。

The bug has been filed and assigned for fix.


Olivier Thomann 2010-05-28 08:37:21 EDT

Olivier Thomann 2010-05-28 08:37:21 EDT

我们对优化过于激进。

For:

  switch((Integer) null) {};

我们优化了整个开关语句我们应该至少评估
表达式。

we optimize out the whole switch statement when we should at least evaluate the expression.

我会看看。

候选人对于3.6.1。

Candidate for 3.6.1.



另请参见



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