score:27
This is a bug. Here's the specified behavior for a switch
statement according to the Java Language Specification, 3rd Edition:
JLS 14.11 The switch
Statement
SwitchStatement: switch ( Expression ) SwitchBlock
When the
switch
statement is executed, first theExpression
is evaluated. If theExpression
evaluates tonull
, aNullPointerException
is thrown and the entireswitch
statement completes abruptly for that reason.
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!");
}
}
}
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.
The cause
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
}
It seems that the Eclipse compiler gets rid of the entire switch
constructs entirely. Unfortunately this optimization breaks the language specification.
The official words
The bug has been filed and assigned for fix.
Olivier Thomann 2010-05-28 08:37:21 EDT
We are too aggressive on the optimization.
For:
switch((Integer) null) {};
we optimize out the whole
switch
statement when we should at least evaluate the expression.I'll take a look.
Candidate for 3.6.1.
See also
score:1
Yep. According to the JLS it's a bug:
If the switch expression is of a reference type, such as a boxed primitive type or an enum, a run-time error will occur if the expression evaluates to null at run-time.
score:4
Definitly. If we look at the chapter 14.11 of the java language specification, it clearly states (under 'discussion'):
The prohibition against using null as a switch label prevents one from writing code that can never be executed. If the switch expression is of a reference type, such as a boxed primitive type or an enum, a run-time error will occur if the expression evaluates to null at run-time.
Source: stackoverflow.com
Related Query
- Eclipse bug? Switching on a null with only default case
- Android app crashes with SIGABRT Signal 6 only while Eclipse debugging
- Bug with Override annotations in Eclipse
- How does Eclipse compile classes with only a JRE?
- When using Eclipse with FindBugs can you mark a bug as not a bug and have it removed from the bug list?
- How to deal with Camel Case for Eclipse Templates?
- Eclipse bug with nine patch files?
- What would make Ant execute 'javac' in Eclipse with UTF-8 instead of my system default Cp1252?
- Make Eclipse default to JUnit4 rather than JUnit5 when the class only contains JUnit4 annotations
- How to debug CUDA using eclipse Nsight with only one GPU
- Is it possible with Eclipse to only find results in string literals?
- Bug with installing ObjectAid into eclipse LUNA
- adding buttons with shortcuts, or shortcuts only for ant tasks in Eclipse
- Annotation-based null analysis - warning appears only with array parameter
- a bug with eclipse android's graphical layout editor?
- Java 17 null case with pattern matching
- Eclipse Mars bug with Map.Entry methods when using streams?
- Eclipse only running with -clean -refresh
- Problems with clear case plugin eclipse
- Eclipse + Git - How to show only modified files in compare with option?
- Is eclipse AutoCompleteField only suggesting entries starting with entered text?
- Switching editors in Eclipse with keyboard, rather than switching Design/Source
- Eclipse null check doesn't work with functions
- How to use default values with Lombok and Eclipse save actions
- "Open with..." in Eclipse without switching default editor
- Bug with Eclipse + Processing 3.2.1 + UnfoldingMaps
- Trying to remotely compile, using the command line, a Java program with multiple dependencies that I can currently only compile locally in Eclipse
- My Eclipse E4 plugins only works with new workspaces, workbench.xmi not updating
- adding external jars with bundles to project only in Eclipse
- UndeclaredThrowableException with a client only eclipse scout application
More Query from same tag
- Want to create a table using org.eclipse.swt.widgets.Table, working on a pugin project
- Hidden Viewpart in Eclipse RCP with iFolderLayout
- Using a Custom Android Library with Maven and ADT
- Eclipse executable launcher error
- How to create a maven project which will work in eclipse and from command line
- how can fix stopped application when stop playing sound?
- Adding custom trim to Eclipse workbench window does not work in Kepler 4.3
- android maven automation
- Delete entry in SQLite database automatically (android)
- Is there a way to quickly capitalize the variable name in Eclipse
- Round up to two decimals in eclipse android
- Adding 3rd party jars to WEB-INF/lib automatically using Eclipse/Tomcat
- Eclipse XML autocomplete in liquibase files stopped working
- Finding jar dependencies for eclipse/jdt in Java
- Converting a .jar into a Mac OS X app
- Is there a way to compile Objective-C in windows with eclipse without launching the MSYS terminal
- Usb tether and debug at the same time
- testng.xml is not running from pom.xml during executing selenium tests
- Can't arrange my elements nicely with borderlayout
- How can I change libraries?
- Eclipse Juno WindowBuilder Palette is Empty
- can't create jpa tables in eclipse
- IWAE0006E Archive is not a valid WAR File because the deployment descriptor can not be found (case sensitive): WEB-INF/web.xml
- Router issues with Vertx
- headless xml generation from xsd with eclipse
- Cannot install JBoss AS, Wildfly, EAP tools in Eclipse Photon
- Java Eclipse: creating new class but doesn't show git staging
- ESP32-S2 Eclipse step over (F6) enters the function
- Google chrome with seleneium web driver
- How can one show the gdb traces in eclipse?