dimanche 29 juillet 2018

Want to pass class.getmethod arguments dynamically in run time java

I want to invoke a method by giving methodName in runtime. I can do it in below way.

Method method = MyObject.class.getMethod("doSomething", String.class); Object returnValue = method.invoke(null, "parameter-value1");

But I want to list down all the methods with that method name and the different set of arguments and let the user choose the method with a particular set of arguments and dynamically pass those arguments in Method method = MyObject.class.getMethod("doSomething", String.class); instead of hardcoding String.class.

suppose I have two methods like methodName(String) and methodName(String,int)

I want to let user choose which one to pick in runtime and pass that information for getMethod function for that particular method.

How can I do this?





Aucun commentaire:

Enregistrer un commentaire