Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-6747

Problem With Anonymous Inner Enum

    XMLWordPrintableJSON

Details

    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.

      Attachments

        1. enumquestion.zip
          51 kB
          Jeff Brown

        Activity

          People

            emilles Eric Milles
            brownj Jeff Brown
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 0.5h
                0.5h