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

后端 未结 8 1076
一向
一向 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:52

    Try using Class.isPrimitive() to determine if it a primitive type, then if it is, use reflections to retrieve the TYPE field and check if that is equal. So, in very liberal pseudocode:

    for(Method m:getDeclaredMethods())
      for(Class c:m.getParameterTypes() && Class desired:desiredMethodArgTypes)
        if(c.isAssignableFrom(desired))
          //matches
        if(c.isPrimitive() && c==desired.getDeclaredField("TYPE").get(desiredObject))
          //matches
    

提交回复
热议问题