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

Wrong invokation target of static method

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Duplicate
    • 2.4.9
    • 2.4.11
    • groovy-runtime
    • None

    Description

      Given following set of java classes:

      Interface.java
      public interface Interface {
          class ExternalInterface implements Interface {}
      
          static Interface provide() {
              new Throwable().printStackTrace();
              return new ExternalInterface();
          }
      }
      
      MyInterface.java
      public abstract class MyInterface implements Interface {
      
          private static class MyInterfaceImpl extends MyInterface {}
      
          public static MyInterface provide() {
              return new MyInterfaceImpl();
          }
      }
      

      And below Groovy code:

      Test.groovy
      class Test {
          static void main(String[] args) {
              MyInterface myInterface = MyInterface.provide()
          }
      }
      

      It yields following result:

      Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'Interface$ExternalInterface@24a35978' with class 'Interface$ExternalInterface' to class 'MyInterface'
      	at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:405)
      	at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:319)
      	at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:232)
      	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:603)
      	at Test.main(Test.groovy:9)
      

      Stacktrace during creating object of invalid type is:

      at Interface.provide(Interface.java:7)
      	at Interface$provide.call(Unknown Source)
      	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
      	at Test.main(Test.groovy:9)
      

      To me this seems to be a bug. Or is this expected behavior?

      Attachments

        Activity

          People

            paulk Paul King
            jdabrowa Jędrzej Dąbrowa
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: