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

EnumSet varargs constructor is not recognized (broken) in runtime

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.5.22, 3.0.17
    • 3.0.18
    • None
    • None

    Description

      EnumSet.of() has 6 overloaded variants, the one with varargs is throwing GroovyRuntimeException, claiming it can not find a proper constructor. But it is working for jdk 8 with any Groovy version: 2.4, 2.5, 3.0 and 4.0, broken for jdk 11 and 17 with 2.5 and 3.0 and working fine for groovy 4.0 with any jdk.

      It is a runtime exception and the code needs to access the field:

       

      package test
      enum Test1 {
          ONE,
          TWO,
          THREE,
          FOUR,
          FIVE,
          SIX
          
          public static EnumSet<Test1> test1 = EnumSet.of(ONE, TWO, THREE, FOUR, FIVE, SIX)
          //public static EnumSet<Test1> test2 = EnumSet.of(ONE, TWO, THREE, FOUR) + EnumSet.of(FIVE, SIX) // workaround to not use varargs, works for any combinations
      }
      class Application {
          static void main(String[] args) {
              Test1.test1
         }
      }
      

      It was working fine in groovy 2.4 and all groovy version using jdk 8. But in groovy 2.5 and 3.0, using JDK 11 and 17 it is throwing an exception:

      Exception in thread "main" java.lang.ExceptionInInitializerError
          at java.base/jdk.internal.misc.Unsafe.ensureClassInitialized0(Native Method)
          at java.base/jdk.internal.misc.Unsafe.ensureClassInitialized(Unsafe.java:1042)
          at java.base/jdk.internal.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:43)
          at java.base/jdk.internal.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:186)
          at java.base/java.lang.reflect.Field.acquireFieldAccessor(Field.java:1105)
          at java.base/java.lang.reflect.Field.getFieldAccessor(Field.java:1086)
          at java.base/java.lang.reflect.Field.get(Field.java:418)
          at org.codehaus.groovy.reflection.CachedField.getProperty(CachedField.java:70)
          at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1890)
          at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:3827)
          at org.codehaus.groovy.runtime.callsite.ClassMetaClassGetPropertySite.getProperty(ClassMetaClassGetPropertySite.java:50)
          at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:329)
          at test.Application.main(Application.groovy:20)
      Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[FOUR, ONE, TWO, SIX, FIVE, THREE]' with class 'java.util.ImmutableCollections$SetN' to class 'java.util.EnumSet' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.util.EnumSet(test.Test1, test.Test1, test.Test1, test.Test1, test.Test1, test.Test1)
          at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:402)
          at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:315)
          at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnCollection(DefaultTypeTransformation.java:273)
          at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:230)
          at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:615)
          at test.Test1.<clinit>(Application.groovy:14)
          ... 13 more
          Suppressed: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.util.EnumSet(SetN)
              at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1835)
              at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1605)
              at org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:1067)
              at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:378)
              ... 18 more

      Although it is working fine in groovy 4 in all JDK: 8, 11 and 17.

       

      Groovy\JDK JDK8 jdk-11.0.19+7 jdk-17.0.7+7
      2.4.21 V N/A N/A
      2.5.22 V X X
      3.0.17 V X X
      4.0.12 V V V

      Also, static compilation of enum is also fixing the problem. I am using CentOS 7 and AdoptNet(Temurin) Java, see versions of groovy and java in the table above.

       

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              ntn.immortal Anton Panikov
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: