反映在Groovy脚本中声明的函数 [英] Reflect on functions declared in a Groovy script
问题描述
是否有一种方法可以获取已通过 GroovyShell
对象评估过的Groovy脚本中声明的函数的反射数据?具体来说,我想列举一个脚本中的函数并访问附加到它们的注释。
经过一些实验,我发现最简单的方法:
$ b $ pre $ GroovyShell shell = new GroovyShell();
Script script =(Script)shell.parse(new FileReader(x.groovy));
Method [] methods = script.getClass()。getMethods();
Is there a way to obtain reflection data on functions declared in a Groovy script that has been evaluated via a GroovyShell
object? Specifically, I want to enumerate the functions in a script and access annotations attached to them.
After some experimenting, I found this to be the easiest way:
GroovyShell shell = new GroovyShell();
Script script = (Script)shell.parse(new FileReader("x.groovy"));
Method[] methods = script.getClass().getMethods();
The method
array has all of the functions defined in the script and I can get the annotations from them.
这篇关于反映在Groovy脚本中声明的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!