[ 
https://issues.apache.org/jira/browse/GROOVY-5065?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Daniel Sun reassigned GROOVY-5065:
----------------------------------

    Fix Version/s: 7.0.0-alpha-1
         Assignee: Daniel Sun

> Overriding methods on a Java class metaClass doesn't take effect until 
> instance metaClass is changed
> ----------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-5065
>                 URL: https://issues.apache.org/jira/browse/GROOVY-5065
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 1.7.1, 1.7.4, 1.8.2
>         Environment: Can be reproduced on a Groovy Console on any of the 
> affected versions, e.g., 1.7.4
>            Reporter: Christian Hang
>            Assignee: Daniel Sun
>            Priority: Major
>              Labels: metaprogramming
>             Fix For: 7.0.0-alpha-1
>
>
> When using the metaClass of a Java class to override a method, that change 
> doesn't take effect on new instances of that class, until the metaClass on 
> the instance is changed (though the change doesn't need to have any relation 
> to the overriden method):
> {noformat}
> String.metaClass.toString = { -> return "meta method" }
> def s = new String("original string")
> println s.toString()
> s.metaClass.randomChange = {}
> println s.toString()
> {noformat}
> The expected output would be
> {noformat}
> meta method
> meta method
> {noformat}
> However, the actual output is
> {noformat}
> original string
> meta method
> {noformat}
> It seems that the instance metaClass doesn't recognize the change until it 
> has been changed itself.
> My first intuition was that there is some caching involved and the change to 
> the instance metaClass invalidates the cached metaClass and the overriden 
> method is pulled in afterwards, but fiddling with 
> {{InvokerHelper.metaRegistry.setMetaClass()}} didn't change anything either.
> A simple unit test would be
> {noformat}
> String.metaClass.toString = { -> return "meta method" }
> def s = new String("original string")
> assert "meta method", s.toString()
> {noformat}



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to