Any solution for Class.getMethod() reflection and autoboxing?

后端 未结 8 1093
一向
一向 2021-02-12 22:16

I want to use

Class.getMethod(String name, Class... parameterTypes)

to find the method I need to invoke with the given parameters, but apparent

8条回答
  •  眼角桃花
    2021-02-12 22:54

    You could add some additional logic around your reflection call which tries converting your Integer.class (or whatever) into the corresponding primitive class, and then repeatedly looking up the method until you get a match. If you have Apache Commons Lang, then the wrapperToPrimitive method will do this conversation for you, but writing it yourself is trivial.

    1. Perform getMethod as usual
    2. If nothing found, then look for any parameter types which have corresponding primitives
    3. For each combination of those, perform another lookup until something sticks.

    Not elegant, and for methods with a lot of primitive parameters, it might even be slow.

提交回复
热议问题