反映在Groovy脚本中声明的函数 [英] Reflect on functions declared in a Groovy script

查看:353
本文介绍了反映在Groovy脚本中声明的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以获取已通过 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆