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

Lóránt Pintér commented on GROOVY-10514:
----------------------------------------

Using {{@CompileStatic}} fixes the problem:

{code:language=groovy}
@groovy.transform.CompileStatic
class Lajos {
    static void main(String[] args) {
        FileTreeBuilder builder = new FileTreeBuilder()

        builder {
            hello("world")
        }
    }

    static void hello(String param) {
        println "Hello $param!"
    }
}
{code}

This also works as a workaround for static methods:

{code:language=groovy}
class Lajos {
    static void main(String[] args) {
        FileTreeBuilder builder = new FileTreeBuilder()

        builder {
            Lajos.hello("world") // <-- use the class name to refer to the 
method more explicitly
        }
    }

    static void hello(String param) {
        println "Hello $param!"
    }
}
 {code}

> Class method is not called anymore from inside closure
> ------------------------------------------------------
>
>                 Key: GROOVY-10514
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10514
>             Project: Groovy
>          Issue Type: Bug
>          Components: syntax
>    Affects Versions: 4.0.0
>            Reporter: Lóránt Pintér
>            Priority: Major
>
> This code works with Groovy 3.0.9, but doesn't work with 4.0.0:
> {code:language=groovy}
> class Lajos {
>     static void main(String[] args) {
>         FileTreeBuilder builder = new FileTreeBuilder()
>         builder {
>             hello("world")
>         }
>     }
>     static void hello(String param) {
>         println "Hello $param!"
>     }
> }
> {code}
> Using 3.0.9 the expected result is printed:
> {code}
> $ ../groovy-3.0.9/bin/groovy script.groovy
> Hello world!
> $ ls -l
> total 8
> -rw-r--r--  1 lptr  staff  249 Feb 26 00:45 script.groovy
> {code}
> Using 4.0.0 instead of calling {{hello()}} the {{methodMissing()}} method is 
> called on {{FileTreeBuilder}} and a file is created.
> {code}
> $ ../groovy-4.0.0/bin/groovy script.groovy
> $ ls -l
> total 16
> -rw-r--r--  1 lptr  staff    5 Feb 26 00:53 hello
> -rw-r--r--  1 lptr  staff  249 Feb 26 00:45 script.groovy
> $ cat hello
> world
> {code}
> Same thing happens when {{hello()}} is called from an instance method, and 
> even if {{hello()}} itself is non-static.



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to