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

@CompileStatic type inference bug with overridden methods: causes GroovyCastException at runtime

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Blocker
    • Resolution: Fixed
    • Affects Version/s: 2.1.8
    • Fix Version/s: 2.1.9, 2.2.0-rc-2
    • Component/s: None
    • Labels:
      None

      Description

      import groovy.transform.CompileStatic
      
      @CompileStatic
      interface Greeter {
         public void sayHello()
      }
      
      @CompileStatic
      class HelloGreeter implements Greeter {
         public void sayHello() {
             println "Hello world!"
         }
      }
      
      @CompileStatic
      class A {
         Greeter createGreeter() {
             new HelloGreeter()
         }
         
         void sayHello() {
            // also fails: def greeter = createGreeter()
            // successfull: def greeter = (Greeter)createGreeter()
            Greeter greeter = createGreeter()
            greeter.sayHello()
         }   
      }
      
      @CompileStatic
      class HelloThereGreeter implements Greeter {
         public void sayHello() {
             println "Hello there!"
         }
      }
      
      @CompileStatic
      class B extends A {
         Greeter createGreeter() {
             new HelloThereGreeter()
         }
      }
      
      
      new B().sayHello()
      

      Throws exception

      Exception thrown
      loka 24, 2013 6:53:33 AP. org.codehaus.groovy.runtime.StackTraceUtils sanitize
      WARNING: Sanitizing stacktrace:
      org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'HelloThereGreeter@5a0e8a14' with class 'HelloThereGreeter' to class 'HelloGreeter'
      	at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:360)
      	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:599)
      	at A.sayHello(flowtyping_bug.groovy:24)
      	at A$sayHello.call(Unknown Source)
      	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
      	at flowtyping_bug.run(flowtyping_bug.groovy:44)
      	at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:257)
      	at groovy.lang.GroovyShell.run(GroovyShell.java:481)
      	at groovy.lang.GroovyShell.run(GroovyShell.java:163)
      	at groovy.lang.GroovyShell$run$0.call(Unknown Source)
      	at groovy.ui.Console$_runScriptImpl_closure17.doCall(Console.groovy:954)
      	at groovy.ui.Console$_runScriptImpl_closure17.doCall(Console.groovy)
      	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:606)
      	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:405)
      	at groovy.lang.Closure.run(Closure.java:492)
      	at java.lang.Thread.run(Thread.java:744)
      
      org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'HelloThereGreeter@5a0e8a14' with class 'HelloThereGreeter' to class 'HelloGreeter'
      	at A.sayHello(flowtyping_bug.groovy:24)
      	at A$sayHello.call(Unknown Source)
      	at flowtyping_bug.run(flowtyping_bug.groovy:44)
      

        Attachments

          Activity

            People

            • Assignee:
              melix Cédric Champeau
              Reporter:
              lhotari Lari Hotari
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: