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

Groovy tries to invoke an instance method in a static context when using ExpandoMetaClass

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.4.7
    • None
    • None

    Description

      The following script:

      ExpandoMetaClass.enableGlobally()
      
      trait Attributes {
          void attributes(Map attrs) {}
      }    
      
      class Foo implements Attributes {
          String attributes
          static stuff = {
              attributes foo:"bar"
          }
      }
      
      new Foo().attributes(foo:'bar')
      
      Foo.stuff.call()
      

      Produces

      java.lang.IllegalArgumentException: object is not an instance of declaring class
      	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:497)
      	at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
      	at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
      	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1212)
      	at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:1125)
      	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1021)
      	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1157)
      

      I debugged the issue and there is some kind of caching logic that is broken. There is a `CachedMethod` instance that is invoked, even though it is not a static method.

      There should never be a case where the `attributes` method from the trait is invoked since it is an instance method and this is a static context.

      Attachments

        Activity

          People

            Unassigned Unassigned
            graemerocher1 Graeme Rocher
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: