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

Marc Ewert updated GROOVY-7687:
-------------------------------
    Component/s: Compiler

> Bug with @CompileStatic and nested closures
> -------------------------------------------
>
>                 Key: GROOVY-7687
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7687
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 2.4.5
>            Reporter: Marc Ewert
>
> I've encountered a strange bug in conjunction with closures, delegates and 
> @CompileStatic. The following code works as expected:
> {code}
> import groovy.transform.CompileStatic
> @CompileStatic
> class SimpleTest {
>     static class Foo {
>         List<Bar> bars = Arrays.asList(new Bar())
>     }
>     static class Bar {
>         String message
>         public String toString() {return message}
>     }
>     void execute(Foo foo) {
>         interactions(foo, {
>             bars.each ( { bar -> bar.message = "hello world"})
>         })
>     }
>     void interactions(Foo foo, @DelegatesTo(Foo) Closure closure) {
>         closure.delegate = foo
>         closure.resolveStrategy = Closure.DELEGATE_FIRST
>         closure()
>     }
>     static void main(String... args) {
>         SimpleTest test = new SimpleTest()
>         Foo foo = new Foo()
>         test.execute(foo)
>         println foo.bars
>     }
> }
> {code}
> But when the bars member is prefixed with a public modifier the following 
> compilation error is thrown
> {code}
> Exception in thread "main" java.lang.ClassCastException: 
> SimpleTest$_execute_closure1 cannot be cast to SimpleTest$Foo
>       at SimpleTest$_execute_closure1.doCall(SimpleTest.groovy:18)
>       at SimpleTest$_execute_closure1.call(SimpleTest.groovy)
>       at SimpleTest.interactions(SimpleTest.groovy:25)
>       at SimpleTest.execute(SimpleTest.groovy:17)
>       at SimpleTest.main(SimpleTest.groovy:31)
>       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>       at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>       at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>       at java.lang.reflect.Method.invoke(Method.java:497)
>       at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
> {code}
> Here the code causing the error:
> {code}
> import groovy.transform.CompileStatic
> @CompileStatic
> class SimpleTest {
>     static class Foo {
>         public List<Bar> bars = Arrays.asList(new Bar())
>     }
>     static class Bar {
>         String message
>         public String toString() {return message}
>     }
>     void execute(Foo foo) {
>         interactions(foo, {
>             bars.each ( { bar -> bar.message = "hello world"})
>         })
>     }
>     void interactions(Foo foo, @DelegatesTo(Foo) Closure closure) {
>         closure.delegate = foo
>         closure.resolveStrategy = Closure.DELEGATE_FIRST
>         closure()
>     }
>     static void main(String... args) {
>         SimpleTest test = new SimpleTest()
>         Foo foo = new Foo()
>         test.execute(foo)
>         println foo.bars
>     }
> }
> {code}



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

Reply via email to