You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Marc Ewert (JIRA)" <ji...@apache.org> on 2015/12/03 11:38:11 UTC
[jira] [Commented] (GROOVY-7687) Bug with @CompileStatic and nested
closures
[ https://issues.apache.org/jira/browse/GROOVY-7687?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15037632#comment-15037632 ]
Marc Ewert commented on GROOVY-7687:
------------------------------------
Here is an even simpler TestCase that fails:
{code}
@groovy.transform.CompileStatic
class NestedClosureWithCompileStaticBug extends TestSupport {
static class Foo {
public List<String> messages = Arrays.asList("hello world")
}
void execute(Foo foo) {
interactions(foo, {
messages.each ( { message -> println message})
})
}
void interactions(Foo foo, @DelegatesTo(Foo) Closure closure) {
closure.delegate = foo
closure()
}
void testBug() {
execute(new Foo())
}
}
{code}
> 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)