I am able invoke a method with arguments by a variable name if number of arguments and argument types are known, But how to get the declared method if no if arguments and argument type are known only at the time of search[search for method ].
public static void invokeMethod (String myClass,
String myMethod,
Class[] params, Object[] args)
throws Exception {
Class c = Class.forName(myClass);
Method m = c.getDeclaredMethod(myMethod, params);
Object i = c.newInstance();
Object r = m.invoke(i, args);
}
invokeMethod("myLib", "sampleMethod", new Class[] {String.class, String.class},
new Object[]
{new String("Hello"), new String("World")});
What if I am not aware of the count and type of Class[]
? How to manage this dynamically? I will get the arguments and method through the command line or a socket. So I am not aware that which method will be receiving.
Aucun commentaire:
Enregistrer un commentaire