2

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.

Alidad
  • 5,463
  • 1
  • 24
  • 47

1 Answers1

3

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(', ')})"
      }
   }
}
Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
  • Thanks. Excellent solution. Is it possible to have also the name of the arguments along with the type? Using the example to have: (username String, password String) ? – ncaramolegos Dec 08 '13 at 16:24
  • That information should be available if the classes are compiled with debug information. I tried using `org.springframework.core.LocalVariableTableParameterNameDiscoverer` but it didn't find the names. You should look at this: http://stackoverflow.com/questions/2729580/how-to-get-the-parameter-names-of-an-objects-constructors-reflection/2729907#2729907 – Burt Beckwith Dec 08 '13 at 21:50