开发者

Retrieve Groovy Closure/Method parameters list

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

开发者_JAVA百科
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!


The parameter names can be retrieved if the compiler included debugging symbols, though not through the standard Java Reflection API.

See this post for an example https://stackoverflow.com/a/2729907/395921


I think you may want to take a look at varargs http://www.javalobby.org/articles/groovy-intro3/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜