检索Groovy Closure /方法参数列表 [英] Retrieve Groovy Closure/Method parameters list
问题描述
我们如何通过参数数组动态获取常规闭包/方法的参数列表
例如说我想记录一个这样的消息
def closure = {name,id - >
log.debug使用参数名称执行方法:$ {} id:$ {id}
}
OR
void方法(String name,String id){
log.debug 使用参数名称执行方法:$ {} id:$ {id}
}
我读过一次关于引用闭包参数列表的方法,但我没有回忆这一点,并查看了 Closure 只显示 getParametersType()方法。至于该方法,有一种方法可以调用一个方法作为一个闭包,然后我可以检索方法参数
$ b $ ken
b
API访问在Groovy Closure
或Java 方法中声明的参数列表
。
我还查看了相关类型,包括(对于Groovy) MetaClass
和子类型,以及 org.codehaus.groovy.reflection
包中的类型以及 java.lang中的(用于Java)类型.reflect
package。
此外,我还对Google追踪外星人进行了广泛的搜索。 ; - )
如果我们需要一个可变长度的闭包或方法参数列表,我们可以使用 Object []
数组,a List
或 varargs
作为参数:
def closure = {id,Object ... args - >
println id
args.each {println it}
}
closure.call(1,foo,bar)
好的,这就是限制和选项!
How do we retrieve the list of parameters of a closure/method in groovy dynamically, javascript style through the arguments array
say for example that i want to log a message this way
def closure = {name,id -> log.debug "Executing method with params name:${} id:${id}" }
OR
void method (String name,String id) { log.debug "Executing method with params name:${} id:${id}" }
I read once about a way to reference the list of parameters of a closure, but i have no recollection of that and looking at the groovy API for Closure reveals only getParametersType() method. As for the method, there is a way to call a method as a closure and then i can retrieve the method parameters
ken
解决方案You won't like it (and I hope it's not my bad to do research and to answer), however:
There is no API to access the list of parameters declared in a Groovy
Closure
or in a JavaMethod
.I've also looked at related types, including (for Groovy)
MetaClass
, and sub-types, and types in theorg.codehaus.groovy.reflection
package, and (for Java) types in thejava.lang.reflect
package.Furthermore, I did an extensive Google search to trace extraterrestrials. ;-)
If we need a variable-length list of closure or method arguments, we can use an
Object[]
array, aList
, orvarargs
as parameters:def closure = { id, Object... args -> println id args.each { println it } } closure.call(1, "foo", "bar")
Well, that's the limitations and options!
这篇关于检索Groovy Closure /方法参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!