You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (JIRA)" <ji...@apache.org> on 2017/05/04 11:32:04 UTC

[jira] [Commented] (GROOVY-7866) Groovy tries to invoke an instance method in a static context when using ExpandoMetaClass

    [ https://issues.apache.org/jira/browse/GROOVY-7866?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15996584#comment-15996584 ] 

Paul King commented on GROOVY-7866:
-----------------------------------

Just simplifying the example to remove the trait:
{code}
ExpandoMetaClass.enableGlobally()

class Foo {
    void attributes(Map attrs) {}
    static stuff = {
        attributes foo:"bar"
    }
}

Foo.stuff()
{code}

> Groovy tries to invoke an instance method in a static context when using ExpandoMetaClass
> -----------------------------------------------------------------------------------------
>
>                 Key: GROOVY-7866
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7866
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.4.7
>            Reporter: Graeme Rocher
>
> The following script:
> {code}
> 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()
> {code}
> Produces 
> {code}
> 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)
> {code}
> 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. 



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)