[ https://issues.apache.org/jira/browse/GROOVY-8764?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16641645#comment-16641645 ]
Paul King edited comment on GROOVY-8764 at 10/8/18 11:57 PM: ------------------------------------------------------------- This doesn't occur if only the method has @CompileStatic like in this script: {code} import groovy.transform.* @Field String foo = 'FOO' @CompileStatic void test() { new Runnable() { void run() { def c = { println foo } c() } } .run() } test() {code} Or in this class: {code} import groovy.transform.* class GroovyTest { private String foo = 'FOO' @CompileStatic void test() { new Runnable() { void run() { def c = { println foo } c() } }.run() } } new GroovyTest().test() {code} Basically the inner closure will be dynamic code in those cases. was (Author: paulk): This doesn't occur if only the method has @CompileStatic like in this script: {code} import groovy.transform.* @Field String foo = 'FOO' @CompileStatic void test() { new Runnable() { void run() { def c = { println foo } c() } } .run() } test() {code} Or in this class: {code} import groovy.transform.* class GroovyTest { private String foo = 'FOO' @CompileStatic void test() { new Runnable() { void run() { def c = { println foo } c() } }.run() } } new GroovyTest().test() {code} > Closure inside an anonymous class uses wrong outside class instance with @CS > ---------------------------------------------------------------------------- > > Key: GROOVY-8764 > URL: https://issues.apache.org/jira/browse/GROOVY-8764 > Project: Groovy > Issue Type: Bug > Components: Compiler, Static compilation > Affects Versions: 2.4.15, 3.0.0-alpha-3, 2.5.2 > Reporter: Xiaoguang Wang > Priority: Major > > This bug only occurs with @ComplieStatic > Affects 2.4/2.5/3.0 > > {code:java} > @CompileStatic > class GroovyTest { > private String foo > void test() { > new Runnable() { > void run() { > def c = { > foo > } > c() > } > } .run() > } > static void main(String ...args) { > new GroovyTest().test() > } > } > {code} > Crash: > {code:java} > Exception in thread "main" java.lang.ClassCastException: > com.test.spring4mvc.controller.GroovyTest$1 cannot be cast to > com.test.spring4mvc.controller.GroovyTest > at > com.test.spring4mvc.controller.GroovyTest$1$_run_closure1.doCall(GroovyTest.groovy) > at > com.test.spring4mvc.controller.GroovyTest$1$_run_closure1.call(GroovyTest.groovy) > at com.test.spring4mvc.controller.GroovyTest$1.run(GroovyTest.groovy:16) > at com.test.spring4mvc.controller.GroovyTest.test(GroovyTest.groovy:11) > at com.test.spring4mvc.controller.GroovyTest.main(GroovyTest.groovy:22) > {code} -- This message was sent by Atlassian JIRA (v7.6.3#76005)