Community
Participate
Working Groups
Build Identifier: 20090920-1017 Eclipse doesn't always throw NullPointerException when switching on a null expression. It can even allow execution of a default case. See: JLS 14.11 The Switch Statement (http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.11) SwitchStatement: switch ( Expression ) SwitchBlock 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. Reproducible: Always Steps to Reproduce: java.math.RoundingMode x = null; switch(x) {}; switch((Integer) null) {}; switch((Character) null) { default: System.out.println("I've got sunshine!"); }
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.
Created attachment 170539 [details] Proposed fix + regression tests
Srikanth, please review.
(In reply to comment #2) > Created an attachment (id=170539) [details] > Proposed fix + regression tests A quick glance doesn't reveal any test on enum. I'd just like to point out that currently, enum seems "special" somehow. java.math.RoundingMode mode = null; switch (mode) {}; Integer ii = null; switch (ii) {}; Given the above snippet, currently Eclipse (mine at least) DOES throw NullPointerException, but only on the second switch.
(In reply to comment #3) > Srikanth, please review. Other than what is already pointed out in comment #4 it looks good.
Created attachment 170844 [details] Proposed fix + regression tests Patch to fix the enum case. Under testing.
Srikanth, please review the last patch. Thanks.
Looks good.
Released in HEAD and 3.6 maintenance stream.
Verified for 3.6.1 RC2 using Build id: M20100825-0800