Bug 81773

Summary: JDK5.0 enum bug
Product: [Eclipse Project] JDT Reporter: Xavier Méhaut <xavier.mehaut>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: RESOLVED INVALID QA Contact:
Severity: normal    
Priority: P3    
Version: 3.1   
Target Milestone: 3.1 M5   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Xavier Méhaut CLA 2004-12-22 04:00:38 EST
Hello,
I've found a bug in the processing of the enumerated types in JDK5.0 in Eclipse
3.1M4.
let the following definition :
enum EnumType {ONE, TWO, THREE);

The following expression is currently impossible :
EnumType currentType=EnumType.ONE;
switch (currentType) {
		case EnumType.ONE:
			break;
			
		case EnumType.TWO:
			break;
			
		case EnumType.THREE:
			break;

		default:
			break;
		}
Comment 1 Philipe Mulet CLA 2004-12-22 04:41:13 EST
Following code is illegal, since enum constants cannot be qualified in switch
cases. You should remove qualification, and it would still resolve as they are
resolved relatively to the switch expression type (EnumType here).

Also note that javac rejects this code, slightly wrong though (false duplicate
case labels):
>javac X.java -d ..\bin -Xlint
X.java:7: an enum switch case label must be the unqualified name of an
enumeration constant
                                case EnumType.ONE:
                                             ^
X.java:10: an enum switch case label must be the unqualified name of an
enumeration constant
                                case EnumType.TWO:
                                             ^
X.java:10: duplicate case label
                                case EnumType.TWO:
                                ^
X.java:13: an enum switch case label must be the unqualified name of an
enumeration constant
                                case EnumType.THREE:
                                             ^
X.java:13: duplicate case label
                                case EnumType.THREE:
                                ^
5 errors
public class X {
	enum EnumType {ONE, TWO, THREE}
	public static void main(String[] args) {
		EnumType currentType=EnumType.ONE;
		switch (currentType) {
				case EnumType.ONE:
					break;
					
				case EnumType.TWO:
					break;
					
				case EnumType.THREE:
					break;
		
				default:
					break;
				}
	}
}
Comment 2 Xavier Méhaut CLA 2004-12-22 04:46:08 EST
OK Philippe ;) Sun has not well done its job with enumeration. No switch for an
enumeration is silly imo :( 
thanks
regards
Xavier
Comment 3 Philipe Mulet CLA 2004-12-22 04:50:33 EST
You can switch on enum constants no problem. You cannot though qualify enum
constant references *only* in case labels (they may re-enable them later on).
You may still use static imports to grant access to them, or simply rely on the
fact that they are implicitly resolved relatively to the switch expression (so
not need to qualify individual constants); in your example, simply remove the
qualification and it will compile clean.