In Groovy 2.1.6 Script, I define a field:
import groovy.transform.Field @Field String test = "abc"; println "Script: ${test}"; def run = new Runnable() { void run() { println "Runnable0: ${test}"; new Runnable() { void run() { println "Runnable1: ${test}"; } }.run(); } }.run();
When accessing it from anonymous classes in Script, for example here , Groovy seems to try to pass this field to the link and throws the following Exception as soon as Runnable is detected:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'abc' with class 'java.lang.String' to class 'groovy.lang.Reference' at bug1.run(bug1:5)
Also, if I put anonymous Runnables in a function, for example here , Groovy has no casting problems, but does not find the Field in the internal Runnable:
groovy.lang.MissingFieldException: No such field: test for class: bug2$1 at bug2$1.this$dist$get$1(bug2.groovy) at bug2$1$2.propertyMissing(bug2.groovy) at bug2$1$2.run(bug2.groovy:14) at java_lang_Runnable$run.call(Unknown Source) at bug2$1.run(bug2.groovy:12) at java_lang_Runnable$run.call(Unknown Source) at bug2.fun(bug2.groovy:9) at bug2.run(bug2.groovy:5)
This can be fixed by overriding the field, for example here , but this fix only works inside the function
Is this a mistake in Groovy, or am I just breaking some rules and Groovy lacks proper exceptions?
NCode source share