You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2020/07/07 18:53:00 UTC

[jira] [Commented] (GROOVY-7232) Delegation inconsistent between methods and properties in nested closures

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

Eric Milles commented on GROOVY-7232:
-------------------------------------

This is found in {{ClosureMetaClass#invokeMethod}}:
{code}
            default: // owner first
                // owner first means we start with the outer most owner that is not a generated closure
                // this owner is equal to the this object, so we check that one first.
                method = getDelegateMethod(closure, thisObject, methodName, argClasses);
{code}

I think it is supposed to use the variable "owner" and not "thisObject".  It will take some testing to figure out the impact.

For reference, {{Closure}} implements {{getProperty}} like this: https://github.com/apache/groovy/blob/master/src/main/java/groovy/lang/Closure.java#L294

> Delegation inconsistent between methods and properties in nested closures
> -------------------------------------------------------------------------
>
>                 Key: GROOVY-7232
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7232
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 2.3.2, 2.3.7, 2.4.0-rc-1
>            Reporter: Matthew Farrell
>            Priority: Major
>         Attachments: DelegateTest.groovy
>
>
> When a closure's resolveStrategy is set to DELEGATE_ONLY or DELEGATE_FIRST, resolution is different in nested closures between methods and properties of the delegate. For example, in the following, {{x}} resolves to {{f}}'s delegate (what I expect), but {{keySet()}} resolves to {{g}}'s delegate.
> {code}
> ​def g = {->
>     def f = {
>         {-> [x, keySet()]}()
>     }
>     f.resolveStrategy = Closure.DELEGATE_ONLY
>     f.delegate = [x: 1, f: 0]
>     f()
> }      
> g.delegate = [x: 0, g: 0]
> g()
> {code}
> ​ Result: {code}[1, ['x', 'g']]{code}
> Whereas without the nested closure
> {code}
> def g = {->
>     def f = {
>         [x, keySet()]
>     }
>     f.resolveStrategy = Closure.DELEGATE_ONLY
>     f.delegate = [x: 1, f: 0]
>     f()
> }      
> g.delegate = [x: 0, g: 0]
> g()
> {code}
> Result: {code}[1, ['x', 'f']]{code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)