[
https://issues.apache.org/jira/browse/GROOVY-10301?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Paul King closed GROOVY-10301.
------------------------------
> Closure passed to field annotation fails to find static method of the same
> class
> --------------------------------------------------------------------------------
>
> Key: GROOVY-10301
> URL: https://issues.apache.org/jira/browse/GROOVY-10301
> Project: Groovy
> Issue Type: Bug
> Components: Compiler, groovy-runtime
> Affects Versions: 3.0.9
> Reporter: Arthur Sengileyev
> Assignee: Eric Milles
> Priority: Major
>
> Closure passed to field annotation fails to find static method of the same
> class if Compiled to dynamic code.
>
> Code example, which shows how the same code works for statically compiled
> class, but fails for precise copy compiled dynamically.
> {code:java}
> import groovy.transform.CompileDynamic
> import groovy.transform.CompileStatic
> import java.lang.annotation.Retention
> import java.lang.annotation.RetentionPolicy
> @Retention(RetentionPolicy.RUNTIME)
> @interface TestInterface {
> Class<?> value();
> }
> @CompileStatic
> class TestStatic {
> @TestInterface({o -> method(o)})
> Map map
> static String method(Object o) {
> o?.toString()
> }
> }
> @CompileDynamic
> class TestDynamic {
> @TestInterface({o -> method(o)})
> Map map
> static String method(Object o) {
> o?.toString()
> }
> }
> class TestMain {
> static void main(String[] args) {
> TestStatic ts = new TestStatic()
> TestInterface tsi =
> ts.getClass().getDeclaredField("map").getAnnotation(TestInterface)
> Class<?> vcs = tsi.value()
> if (Closure.isAssignableFrom(vcs)) {
> Closure closure = (Closure)vcs.newInstance(null, null)
> println(closure.call("OK"))
> }
> TestDynamic td = new TestDynamic()
> TestInterface tdi =
> td.getClass().getDeclaredField("map").getAnnotation(TestInterface)
> Class<?> vcd = tdi.value()
> if (Closure.isAssignableFrom(vcd)) {
> Closure closure = (Closure)vcd.newInstance(null, null)
> println(closure.call("FAILURE")) // Will throw exception
> // Exception in thread "main" groovy.lang.MissingMethodException:
> No signature of method: grapp.TestDynamic$_closure1.method() is applicable
> for argument types: (String) values: [FAILURE]
> }
> }
> }
> {code}
--
This message was sent by Atlassian Jira
(v8.20.1#820001)