You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "John Wagenleitner (JIRA)" <ji...@apache.org> on 2016/04/03 21:53:25 UTC

[jira] [Resolved] (GROOVY-3948) Replacing method on ExpandoMetaClass doesn't work

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

John Wagenleitner resolved GROOVY-3948.
---------------------------------------
       Resolution: Fixed
    Fix Version/s: 2.2.0

The following tests pass since version 2.2 and also in master.

{code}
Expando.metaClass.foo = { -> delegate.bar() }

def e = new Expando()

e.metaClass.bar = { -> 1 }
assert e.foo() == 1

e.metaClass.bar = { -> 2 }
assert e.foo() == 2
{code}

{code}
class Foo {}
Foo f = new Foo()
def results = []
for (idx in 1..3) {
    def sound = 'woof' * idx
    def x = { sound }
    f.metaClass.bark = x
    results << f.bark()
}
assert results == ['woof', 'woofwoof', 'woofwoofwoof']
{code}

> Replacing method on ExpandoMetaClass doesn't work
> -------------------------------------------------
>
>                 Key: GROOVY-3948
>                 URL: https://issues.apache.org/jira/browse/GROOVY-3948
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 1.6.7
>         Environment: Groovy Shell (1.6.7, JVM: 1.5.0_20)
>            Reporter: Lajos Papp
>             Fix For: 2.2.0
>
>
> {code}
> Expando.metaClass.foo={-> def directBar=delegate.bar()
>     println "directBar=${directBar}"    
>     def invokedBar=delegate.invokeMethod('bar',null)
>     println "invokedBar=$invokedBar"
> }
> e=new Expando()
> e.metaClass.bar={->1}
> e.foo()
> //directBar=1
> //invokedBar=1
> e.metaClass.bar={->2}
> e.foo()
> //directBar=1
> //invokedBar=2
> {code}
> for some reason delegate.bar() != delegate.invokeMethod('bar',null)
> looks like at the direct call the closure got cached ...
> if the previous example continues with:
> {code}
> x=new Expando()
> x.metaClass.bar={->3}
> e.foo()
> //directBar=2
> //invokedBar=2
> {code}
> creating the new 'x' object has side effect on the 'e' objects metaclass?



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)