Community
Participate
Working Groups
consider the following: enum X { a,b } ... X x = ... switch(x) { case a: ... case b: ... } The above is valid Java 1.5 code. However the current eclipse build I am running (integration build 11/1/04) flags the case statements as errors. If I change them to: case X.a: ... case X.b: ... The JDT compiler accepts them. However, this is *invalid* Java 1.5 code (don't ask me why).
Could you please provide a complete test case?
Ok, I think I got a test case. This code reports two errors, but it compiles fine using JDK1.5.0. package p; public enum X { a,b } class A { void foo() { for (X x : X.values()) { switch(x) { case a: case b: System.out.println(x.name()); } } } } If the class A is defined in a separate file, javac also compiles it. package p; public class A { void foo() { for (X x : X.values()) { switch(x) { case a: case b: System.out.println(x.name()); } } } } Eclipse compiler requires a static import to be added.
In I20041101 this compiled fine: public class Enums2 { enum Color { RED, GREEN }; public static void main(String[] args) { Color c= Color.GREEN; switch (c) { case Color.RED: System.out.println(Color.RED); break; case Color.GREEN: System.out.println(c); break; } } } In I20041102 I have to remove the qualyfier "Color" in the case labels. However the resulting code doesn't compile: public class Enums2 { enum Color { RED, GREEN }; public static void main(String[] args) { Color c= Color.GREEN; switch (c) { case RED: System.out.println(Color.RED); break; case GREEN: System.out.println(c); break; } } }
Color.RED is illegal. Only the simple name has to be used. It seems that the enum constants of a enum type are implicitely visible.
Qualifying is indeed forbidden, and we tightened the compiler recently. However we are missing the free access to member enum members.
FYI: I have registered an RFE at Sun regarding use of qualified enums in case statements. "(Review ID: 328931) - Ability to use qualified enums in case statements" If enough people is requesting this feature, we may see it in the future!
Would indeed make the most sense, as one can already qualify non enum constants in case statements. Feels like an artificial limitation.
Additional information: SUN has now decided that the inability to use qualified enums is likely to be a bug. The current implementation actually conflicts with JSR201, (http://jcp.org/en/jsr/detail?id=201) regarding the enum definition (http://jcp.org/aboutJava/communityprocess/jsr/tiger/enum.html). Following is an email from SUN: ------------------------------- Thank you for helping us narrow the focus of this issue. We have determined that this report is a new bug and entered the bug into our internal bug tracking system under Bug Id: 6191812. You can monitor this bug and look for related issues on The Java Developer Connection Bug Database at: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6191812 It may take a day or two before your bug shows up in this external database. Note that bug 4936393 (http://bugs.sun.com/bugdatabase/view_bug.do? bug_id=4936393) mentions that we explicitly chose this behavior. However, this RFE gives the development team a chance to explain its reasoning in the Evaluation and reconsider the decision. The Java Developer Connection is a free channel that is maintained by staff here at Sun. Access this web page to join: http://developer.java.sun.com/servlet/RegistrationServlet. The home page for the Java Developer Connection is: http://java.sun.com/jdc. -------------------------------
*** Bug 78394 has been marked as a duplicate of this bug. ***
*** Bug 79169 has been marked as a duplicate of this bug. ***
*** Bug 80115 has been marked as a duplicate of this bug. ***
I suspect that even if they abandon the restriction on simple name for enum constant cases, we will still have to honour resolving to sibling member enums.
To enable qualified enum constants we need to remove the check in CaseStatement resolveCase() if (constantExpression instanceof QualifiedNameReference) { scope.problemReporter().cannotUseQualifiedEnumConstantInCaseLabel ((QualifiedNameReference)constantExpression); } As for the following testcase: enum X { a } class A { public static void main(String[] args) { for (X x : X.values()) { int a = 3; switch(x) { case a : System.out.println(a); // prints '3' } } } } We need to know that we are resolving the constantExpression of a case & walk the Enum X before looking for a normal variable. I think this needs to be done in CaseStatement resolveCase(), before we call: TypeBinding caseType = constantExpression.resolveType(scope);
This test should generate 3 errors: enum X { a } class A { public static void main(String[] args) { test(X.a, 9); test2(X.a, 3); } static void test(X x, int a) { if (x == a) a++; // incomparable types: X and int switch(x) { case a : System.out.println(a); // prints '9' } } static void test2(X x, final int aa) { switch(x) { case aa : // unqualified enum constant error System.out.println(a); // cannot find a } } }
Added support to resolve simple name relatively to enum type (switchExpressionType). Added EnumTest#test057-058. Fixed
Verified in 200412140800