Bug 77151 - [1.5] Enum constant cannot be qualified when used as a case label
Summary: [1.5] Enum constant cannot be qualified when used as a case label
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.1 M3   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-10-27 16:16 EDT by Olivier Thomann CLA
Modified: 2004-11-04 07:27 EST (History)
0 users

See Also:


Attachments
Apply on HEAD (3.76 KB, patch)
2004-10-27 16:55 EDT, Olivier Thomann CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
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