How to distinguish a class overloaded method using Reflection e.g. :
method()
and method(int arg)
void invoke(Object object, String methodName, int id) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
object.getClass().getDeclaredMethod(methodName).invoke(object, int.class);
}
And call to this function :
getV(new Object(), "method", 33);
And it returns error java.lang.IllegalArgumentException: wrong number of arguments
, That mean the method with no arguments is choosed by default. If i invoke the method without the int parameter it will work. I ask how to distinguish the overloaded method's ? I have read the docs for Class.getDeclaredMethod
https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html Also there is an old post on SO but both sources aren't answering the same question How to find an overloaded method in Java?. Ofcourse Class.Object dosen't have method with name method i just used it for the example.
Aucun commentaire:
Enregistrer un commentaire