Bug 77151

Summary: [1.5] Enum constant cannot be qualified when used as a case label
Product: [Eclipse Project] JDT Reporter: Olivier Thomann <Olivier_Thomann>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3    
Version: 3.1   
Target Milestone: 3.1 M3   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Apply on HEAD none

Description Olivier Thomann CLA 2004-10-27 16:16:27 EDT
Try to compile these two classes using javac.

1) package p;

public enum A {
	CONST1, CONST2, CONST3
}

2) package p;

public class B {
	public static void main(String[] args) {
		for (A a : A.values())
			System.out.println(foo(a));
	}

	private static String foo(A a) {
		switch (a) {
		case A.CONST1:
			return "CONST1";
		case A.CONST2:
			return "CONST2";
		case A.CONST3:
			return "CONST3";
		}
		return "DOESN'T EXIST";
	}
}

The compilation of B returns:
p/B.java:11: an enum switch case label must be the unqualified name of an
enumeration constant
                case A.CONST1:
                      ^
p/B.java:13: an enum switch case label must be the unqualified name of an
enumeration constant
                case A.CONST2:
                      ^
p/B.java:13: duplicate case label
                case A.CONST2:
                ^
p/B.java:15: an enum switch case label must be the unqualified name of an
enumeration constant
                case A.CONST3:
                      ^
p/B.java:15: duplicate case label
                case A.CONST3:
                ^
5 errors

You need to replace B.java with:
package p;

import p.A.*;

public class B {
	public static void main(String[] args) {
		for (A a : A.values())
			System.out.println(foo(a));
	}

	private static String foo(A a) {
		switch (a) {
		case CONST1:
			return "CONST1";
		case CONST2:
			return "CONST2";
		case CONST3:
			return "CONST3";
		}
		return "DOESN'T EXIST";
	}
}

and then it compiles fine.

Looking at the specs they changed the specs to say that a case label can be:
- constant expression (doesn't include enum types)
- enum constant where enum constant is an identifier.

So we need to check that the name reference in a switch statement is a single
name reference if it resolves to a enum type.
Comment 1 Olivier Thomann CLA 2004-10-27 16:55:55 EDT
Created attachment 15425 [details]
Apply on HEAD
Comment 2 Philipe Mulet CLA 2004-10-27 18:15:42 EDT
Integrating patch.
Added regression test: EnumTest#test022.

Fixed
Comment 3 David Audel CLA 2004-11-04 07:27:48 EST
Verified for 3.1M3 with build I200411040100