简体   繁体   中英

Why is getParameterTypes empty when called on a controller Method?

If I have an instance of a controller method, obtained by calling getMethod on the Class object for a controller, why does the method, which is defined as taking 1 parameter, have an empty array for "getParameterTypes"?

Is there any way to actually get the parameter types it accepts?

If you define a controller action which accepts any parameters the Grails compiler generates a corresponding no-argument method.

class MyController {
    // you write an action like this...
    def someAction(String name, Integer age) {
        // your code here
    }

    // the Grails compiler generates this additional method...
    def someAction() {
        // do some stuff needed by the framework

        // ...

        // initialize parameters

        def name = ...
        def age = ...

        // call the original method
        someAction(name, age)
    }
}

I expect that you are calling getParameterTypes on the method that the Grails compiler generated and not the original method.

Is your action a closure or a method? If it's a closure, I wouldn't expect it to work with the Method class from the reflection API. I tested the following method-action from one of my own controllers

class MyController {

  def someAction(MyCommand command) {

  }
}

and it behaves as expected

Method action = MyController.methods.find( it.name == 'someAction' }
assert [MyCommand] == action.parameterTypes as List

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM