Get methods' arguments

435 views Asked by At

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.

1

There are 1 answers

2
Burt Beckwith On

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(', ')})"
      }
   }
}