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

Jeff Scott Brown commented on GROOVY-9662:
------------------------------------------

The project at https://github.com/jeffbrown/groovy9662 represents one of the 
scenarios we have had trouble with.

https://github.com/jeffbrown/groovy9662/blob/f823bec082aa59fc1e6166fc2bf71390c0a025a5/lib/src/main/groovy/groovy9662/MyCustomDelegate.groovy

{code:title=lib/src/main/groovy/groovy9662/MyCustomDelegate.groovy|borderStyle=solid}
package groovy9662

class MyCustomDelegate {
    String getMessage() {
        'Message From Closure Delegate'
    }
}
{code}

https://github.com/jeffbrown/groovy9662/blob/f823bec082aa59fc1e6166fc2bf71390c0a025a5/lib/src/main/groovy/groovy9662/Library.groovy

{code:title=lib/src/main/groovy/groovy9662/Library.groovy|borderStyle=solid}

package groovy9662

class Library {

    String invokeClosure(@DelegatesTo(value = MyCustomDelegate) Closure c) {
        c.resolveStrategy = Closure.DELEGATE_FIRST
        c.delegate = new MyCustomDelegate()
        c()
    }

    String doSomeWork() {
        invokeClosure {
            // this will be dispatched to the delegate
            // as expected because of the code above
            // inside of the invokeClosure method
            // which is setting the strategy to DELEGATE_FIRST
            getMessage()
        }
    }

    String getMessage() {
        'Message From Closure Owner (Library)'
    }

}
{code}

https://github.com/jeffbrown/groovy9662/blob/f823bec082aa59fc1e6166fc2bf71390c0a025a5/lib/src/main/groovy/groovy9662/LibraryCompileStatic.groovy

{code:title=lib/src/main/groovy/groovy9662/LibraryCompileStatic.groovy|borderStyle=solid}
package groovy9662

import groovy.transform.CompileStatic

@CompileStatic
class LibraryCompileStatic {

    String invokeClosure(@DelegatesTo(value = MyCustomDelegate) Closure c) {
        // this resolve strategy does not have an affect
        // because the closure has been statically
        // compiled to delegate to the owner, possibly
        // because that is the default value of the strategy
        // attribute in @DelegatesTo.
        c.resolveStrategy = Closure.DELEGATE_FIRST
        c.delegate = new MyCustomDelegate()
        c()
    }

    String doSomeWork() {
        invokeClosure {
            // this will be dispatched to the owner even
            // thou the invokeClosure method is setting
            // strategy to DELEGATE_FIRST.
            getMessage()
        }
    }

    String getMessage() {
        'Message From Closure Owner (Library)'
    }
}
{code}

https://github.com/jeffbrown/groovy9662/blob/f823bec082aa59fc1e6166fc2bf71390c0a025a5/lib/src/test/groovy/groovy9662/LibraryTest.groovy

{code:title=lib/src/test/groovy/groovy9662/LibraryTest.groovy|borderStyle=solid}
package groovy9662

import spock.lang.Specification

class LibraryTest extends Specification {

    // this test passes
    def "test dynamically dispatched library"() {
        expect:
        new Library().doSomeWork() == 'Message From Closure Delegate'
    }

    // this test fails
    def "test statically  dispatched library"() {
        expect:
        new LibraryCompileStatic().doSomeWork() == 'Message From Closure 
Delegate'
    }
}
{code}

> Groovy 3.0.5: Closure delegate is not working properly
> ------------------------------------------------------
>
>                 Key: GROOVY-9662
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9662
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 3.0.2, 3.0.3, 3.0.4, 3.0.5, 2.5.14
>            Reporter: Puneet Behl
>            Assignee: Eric Milles
>            Priority: Major
>         Attachments: JsonViewWritableScript.groovy, StreamingJsonBuilder.java
>
>
> In Grails Views, we are using closures to generate JSON using 
> `StreamingJsonBuilder`. The HAL implementation for an entity now generates an 
> incorrect JSON after updating to Groovy 3.
>  
> Here is the problem code snipped from 
> \{{grails.plugin.json.view.api.internal.DefaultHalViewHelper#links}}: 
> {code:java}
> jsonDelegate.call(LINKS_ATTRIBUTE) {
>     call(SELF_ATTRIBUTE) {
>         call HREF_ATTRIBUTE, viewHelper.link(resource: object, method: 
> HttpMethod.GET, absolute: true)
>         call HREFLANG_ATTRIBUTE, locale.toString()
>         call TYPE_ATTRIBUTE, contentType
>     }
>     Set<Link> links = getLinks(object)
>     for (link in links) {
>         call(link.rel) {
>             call HREF_ATTRIBUTE, link.href
>             call HREFLANG_ATTRIBUTE, link.hreflang?.toString() ?: 
> locale.toString()
>             def linkType = link.contentType
>             if (linkType) {
>                 call TYPE_ATTRIBUTE, linkType
>             }
>         }
>     }
> }
> {code}
>  
> Although the `resolveStatergy` for the closures is `DELEGATE_FIRST` but I 
> believe that is not working correctly because it works if I change the above 
> to:
> {code:java}
> delegate.call (...
> {code}
>  
> To reproduce the issue please checkout 
> [https://github.com/grails/grails-views/commits/bugs/groovy-9962]
>  



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

Reply via email to