You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Arthur Sengileyev (Jira)" <ji...@apache.org> on 2021/10/12 20:30:00 UTC
[jira] [Created] (GROOVY-10301) Closure passed to field annotation
fails to find static method of the same class
Arthur Sengileyev created GROOVY-10301:
------------------------------------------
Summary: 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
Affects Versions: 3.0.9
Reporter: Arthur Sengileyev
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.3.4#803005)