Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
2.1.8, 2.4.0-rc-1
-
None
Description
Related to http://jira.grails.org/browse/GRAILS-11348.
See the attached enumquestion.zip which demonstrates the behavior without involving Grails. Run "./gradlew test" to see a relevant test failure.
src/main/groovy/com/demo/SampleAppIndicatorCodes.groovy
package com.demo public enum SampleAppIndicatorCodes { YES("Y") { @Override public String getCode() { return booleanCode; } }, NO("N") { @Override public String getCode() { return booleanCode; } }; private String booleanCode = null private SampleAppIndicatorCodes(String booleanCode) { this.booleanCode = booleanCode } public abstract String getCode(); public static SampleAppIndicatorCodes getSourceIndicator(String booleanCode) { if ("Y".equalsIgnoreCase(booleanCode)) { return YES } else if ("L".equalsIgnoreCase(booleanCode)) { return NO } else { throw new IllegalArgumentException("No Indicator for the code $booleanCode") } } }
src/test/groovy/com/demo/SampleAppIndicatorCodesSpec.groovy
package com.demo import spock.lang.Specification class SampleAppIndicatorCodesSpec extends Specification { def 'test accessing YES code'() { expect: 'Y' == SampleAppIndicatorCodes.YES.code } }
For this particular case the anonymous inner classes seems like a peculiar way to do this but in general apparently this syntax is supposed to be valid. I ran this example by Jochen this morning and he felt like this is probably a bug.