[
https://issues.apache.org/jira/browse/GROOVY-4612?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Paul King closed GROOVY-4612.
-----------------------------
Resolution: Duplicate
> Enums cause compilation errors in a switch
> ------------------------------------------
>
> Key: GROOVY-4612
> URL: https://issues.apache.org/jira/browse/GROOVY-4612
> Project: Groovy
> Issue Type: Bug
> Components: Compiler
> Affects Versions: 1.7.5, 2.4.0-beta-3
> Environment: Windows 7
> Reporter: Ron Gross
> Priority: Major
>
> Declaring an enum and using it in a switch causes compilation errors:
> {code}
> class EnumTest {
> enum Operation {
> Insert,
> Delete,
> Find,
> }
> public static void main(String[] args) {
> foo();
> }
> public static void foo() {
> final Random rand = new Random();
> int opVal = rand.nextInt(Operation.values().length);
> Operation op = Operation.values()[opVal];
> switch (op) {
> case Insert:
> System.out.println("Inserting");
> break;
> case Delete:
> System.out.println("Deleting");
> break;
> case Find:
> System.out.println("Finding");
> break;
> default:
> throw new RuntimeException("Unrecognized operation: " + op);
> }
> }
> }
> {code}
> The error:
> {noformat}
> C:\work\play\Basic\java\src\test\java>groovy EnumTest.groovy
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup
> failed:
> C:\work\play\Basic\java\src\test\java\EnumTest.groovy: 26: Apparent variable
> 'Insert' was found in a
> static scope but doesn't refer to a local variable, static field or class.
> Possible causes:
> You attempted to reference a variable in the binding or an instance variable
> from a static context.
> You misspelled a classname or statically imported field. Please check the
> spelling.
> You attempted to use a method 'Insert' but left out brackets in a place not
> allowed by the grammar.
> @ line 26, column 18.
> case Insert:
> ^
> C:\work\play\Basic\java\src\test\java\EnumTest.groovy: 30: Apparent variable
> 'Delete' was found in a
> static scope but doesn't refer to a local variable, static field or class.
> Possible causes:
> You attempted to reference a variable in the binding or an instance variable
> from a static context.
> You misspelled a classname or statically imported field. Please check the
> spelling.
> You attempted to use a method 'Delete' but left out brackets in a place not
> allowed by the grammar.
> @ line 30, column 18.
> case Delete:
> ^
> C:\work\play\Basic\java\src\test\java\EnumTest.groovy: 34: Apparent variable
> 'Find' was found in a s
> tatic scope but doesn't refer to a local variable, static field or class.
> Possible causes:
> You attempted to reference a variable in the binding or an instance variable
> from a static context.
> You misspelled a classname or statically imported field. Please check the
> spelling.
> You attempted to use a method 'Find' but left out brackets in a place not
> allowed by the grammar.
> @ line 34, column 18.
> case Find:
> ^
> 3 errors
> {noformat}
--
This message was sent by Atlassian Jira
(v8.3.4#803005)