检索Groovy Closure /方法参数列表 [英] Retrieve Groovy Closure/Method parameters list

查看:118
本文介绍了检索Groovy Closure /方法参数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何通过参数数组动态获取常规闭包/方法的参数列表

例如说我想记录一个这样的消息

  def closure = {name,id  - > 
log.debug使用参数名称执行方法:$ {} id:$ {id}
}

OR

  void方法(String name,String id){
log.debug 使用参数名称执行方法:$ {} id:$ {id}
}

我读过一次关于引用闭包参数列表的方法,但我没有回忆这一点,并查看了 Closure 只显示 getParametersType()方法。至于该方法,有一种方法可以调用一个方法作为一个闭包,然后我可以检索方法参数
$ b $ ken

$ b

解决方案

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 Java Method.

I've also looked at related types, including (for Groovy) MetaClass, and sub-types, and types in the org.codehaus.groovy.reflection package, and (for Java) types in the java.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, a List, or varargs 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屋!

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