Reputation: 49
I need a list of all methods and their arguments at run time from all controllers in any project. I have not find a way or an example of retrieving the arguments. For example in the method:
def login(String username, String password) {
...
}
I need the arguments username and password with their type.
Many thanks for your help.
Upvotes: 2
Views: 441
Reputation: 75671
During compilation, an AST transformation adds an empty method for action methods with arguments. This is annotated with the grails.web.Action
annotation which has a commandObjects
attribute containing a Class[]
array of the classes of command objects and regular method argument types.
So you can loop through all of the controllers in the application, and find all annotated methods:
import grails.web.Action
for (cc in grailsApplication.controllerClasses) {
for (m in cc.clazz.methods) {
def ann = m.getAnnotation(Action)
if (ann) {
String controller = cc.logicalPropertyName
String action = m.name
Class[] argTypes = ann.commandObjects()
println "${controller}.$action(${argTypes*.name.join(', ')})"
}
}
}
Upvotes: 3