I have the following Groovy file "test.groovy":
import groovy.transform.CompileStatic
@CompileStatic
class Test {
final Set<String> HISTORY = [] as HashSet
Set<String> getHistory() {
return HISTORY.clone() as HashSet<String>
}
}
Test test = new Test()
println test.history
Compiling it with Groovy 2.4.1 works fine, however, when I run "groovy test.class" I get the following error:
Caught: java.lang.VerifyError:
(class: Test, method: getHistory signature:()Ljava/util/Set;)
Bad access to protected data
java.lang.VerifyError:
(class: Test, method: getHistory
signature: ()Ljava/util/Set;)
Bad access to protected data
at test.run(test.groovy:12)
Any ideas what I am doing wrong here?
This actually is a bug in Groovy. A ticket was filed: https://issues.apache.org/jira/browse/GROOVY-7325
Workaround in this case:
It works if you are using a final HashSet<String> and then cast the clone up. Since the getter overrides the property basically anyway (make it private if you wanna be sure), it should not harm the intention of the original code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With