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

@CompileStatic class throws runtime errors accessing outer class methods inside nested closures

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.1.4
    • Fix Version/s: 2.1.7, 2.2.0-beta-2
    • Component/s: Static compilation
    • Labels:
      None
    • Environment:
      Windows 7, Java 6, Groovy 2.1.4

      Description

      The following class throws an exception at runtime when executed. It works fine without @CompileStatic.

      @groovy.transform.CompileStatic
      class MyClass {
          public static void main(String[] args) {
              new MyClass().run()
          }
      
          void run() {
              1.times {
                  1.times {
                      myMethod()
                  }
              }
          }
      
          void myMethod() {
              println 'hi'
          }
      }
      


      org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'MyClass$_run_closure1@4cf92ef9' with class 'MyClass$_run_closure1' to class 'MyClass'

      at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:360)

      at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:599)

      at MyClass$_run_closure1_closure2.doCall(ConsoleScript0:10)

      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

      at java.lang.reflect.Method.invoke(Method.java:601)

      at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)

      at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)

      at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)

      at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:909)

      at groovy.lang.Closure.call(Closure.java:411)

      at groovy.lang.Closure.call(Closure.java:427)

      at org.codehaus.groovy.runtime.DefaultGroovyMethods.times(DefaultGroovyMethods.java:10646)

      at MyClass$_run_closure1.doCall(ConsoleScript0:9)

      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

      at java.lang.reflect.Method.invoke(Method.java:601)

      at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)

      at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)

      at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)

      at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:909)

      at groovy.lang.Closure.call(Closure.java:411)

      at groovy.lang.Closure.call(Closure.java:427)

      at org.codehaus.groovy.runtime.DefaultGroovyMethods.times(DefaultGroovyMethods.java:10646)

      at MyClass.run(ConsoleScript0:8)

      at MyClass.main(ConsoleScript0:4)

      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

      at java.lang.reflect.Method.invoke(Method.java:601)

      at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)

      at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)

      at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1318)

      at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:822)

        Attachments

          Activity

            People

            • Assignee:
              blackdrag Jochen Theodorou
              Reporter:
              bborchardt Brett Borchardt
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: