Anton Panikov created GROOVY-11107:
--------------------------------------

             Summary: EnumSet varargs constructor is not recognized (broken) in 
runtime
                 Key: GROOVY-11107
                 URL: https://issues.apache.org/jira/browse/GROOVY-11107
             Project: Groovy
          Issue Type: Bug
    Affects Versions: 4.0.12, 3.0.17, 2.5.22
            Reporter: Anton Panikov


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:

 
{code:java}
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
   }
}
{code}
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:
{code:java}
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{code}
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|{color:#00875a}V{color}|N/A|N/A|
|2.5.22|{color:#00875a}V{color}|{color:#de350b}X{color}|{color:#de350b}X{color}|
|3.0.17|{color:#00875a}V{color}|{color:#de350b}X{color}|{color:#de350b}X{color}|
|4.0.12|{color:#00875a}V{color}|{color:#00875a}V{color}|{color:#00875a}V{color}|

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.

 



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to