You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (JIRA)" <ji...@apache.org> on 2019/01/22 04:46:00 UTC

[jira] [Resolved] (GROOVY-7687) Bug with @CompileStatic and nested closures

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

Paul King resolved GROOVY-7687.
-------------------------------
       Resolution: Fixed
         Assignee: Paul King
    Fix Version/s: 3.0.0-alpha-5
                   2.5.6

I believe this was fixed as part of GROOVY-7996.

> 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
>            Assignee: Paul King
>            Priority: Major
>             Fix For: 2.5.6, 3.0.0-alpha-5
>
>
> 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
(v7.6.3#76005)