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

Verify error cloning a HashSet under CompileStatic conditions

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.1
    • 2.5.0-alpha-1
    • Static compilation
    • None

    Description

      Taken from:

      http://stackoverflow.com/questions/28761736/java-lang-verifyerror-bad-access-to-protected-data

      The code:

      import groovy.transform.CompileStatic
      
      @CompileStatic
      class Test {
        final Set<String> HISTORY = [] as HashSet
      
        Set<String> getHistory() {
          return HISTORY.clone() as HashSet<String>
        }
      }
      
      Test test = new Test()
      println test.history
      

      Results in the VerifyError:

      java.lang.VerifyError: (class: Test, method: getHistory signature: ()Ljava/util/Set;) Bad access to protected data
      	at java.lang.Class.getDeclaredConstructors0(Native Method)
      	at java.lang.Class.privateGetDeclaredConstructors(Class.java:2663)
      	at java.lang.Class.getDeclaredConstructors(Class.java:2012)
      	at org.codehaus.groovy.reflection.CachedClass$2$1.run(CachedClass.java:71)
      	at java.security.AccessController.doPrivileged(Native Method)
      	at org.codehaus.groovy.reflection.CachedClass$2.initValue(CachedClass.java:68)
      	at org.codehaus.groovy.reflection.CachedClass$2.initValue(CachedClass.java:66)
      	at org.codehaus.groovy.util.LazyReference.getLocked(LazyReference.java:46)
      	at org.codehaus.groovy.util.LazyReference.get(LazyReference.java:33)
      	at org.codehaus.groovy.reflection.CachedClass.getConstructors(CachedClass.java:265)
      	at groovy.lang.MetaClassImpl.<init>(MetaClassImpl.java:215)
      	at groovy.lang.MetaClassImpl.<init>(MetaClassImpl.java:225)
      	at groovy.lang.MetaClassRegistry$MetaClassCreationHandle.createNormalMetaClass(MetaClassRegistry.java:168)
      	at groovy.lang.MetaClassRegistry$MetaClassCreationHandle.createWithCustomLookup(MetaClassRegistry.java:158)
      	at groovy.lang.MetaClassRegistry$MetaClassCreationHandle.create(MetaClassRegistry.java:141)
      	at org.codehaus.groovy.reflection.ClassInfo.getMetaClassUnderLock(ClassInfo.java:250)
      	at org.codehaus.groovy.reflection.ClassInfo.getMetaClass(ClassInfo.java:282)
      	at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl.getMetaClass(MetaClassRegistryImpl.java:255)
      	at org.codehaus.groovy.runtime.InvokerHelper.getMetaClass(InvokerHelper.java:872)
      	at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallConstructorSite(CallSiteArray.java:84)
      	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:232)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:236)
      	at ConsoleScript0.run(ConsoleScript0:12)
      	at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:261)
      	at groovy.lang.GroovyShell.run(GroovyShell.java:522)
      	at groovy.lang.GroovyShell.run(GroovyShell.java:501)
      	at groovy.lang.GroovyShell.run(GroovyShell.java:168)
      	at groovy.lang.GroovyShell$run$1.call(Unknown Source)
      	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:138)
      	at groovy.ui.Console$_runScriptImpl_closure17.doCall(Console.groovy:977)
      	at groovy.ui.Console$_runScriptImpl_closure17.doCall(Console.groovy)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:483)
      	at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
      	at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324)
      	at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:292)
      	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1016)
      	at groovy.lang.Closure.call(Closure.java:423)
      	at groovy.lang.Closure.call(Closure.java:417)
      	at groovy.lang.Closure.run(Closure.java:504)
      	at java.lang.Thread.run(Thread.java:745)
      

      A workaround is to change the line:

        final Set<String> HISTORY = [] as HashSet
      

      To

        final HashSet<String> HISTORY = [] as HashSet
      

      Then you get expected behaviour

      Attachments

        Issue Links

          Activity

            People

              melix Cédric Champeau
              tim_yates Tim Yates
              Votes:
              1 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: