java - Making a method that will call a second from a qualified name -
i need method call second 1 second class. e.g
class commandclass = class.forname("foo.class"); method method = commandclass.getmethod("method", string.class, string.class); method.invoke(method, "param1", "param2");
but have tried put in method harder thought. public method can called class parameters of type.
just pass arguments needed method , call code.
// pass "null" paridentifiers , parameters if it's method without public static void invokemethod(string classname, string methodname, class<?>[] paridentifiers, object[] parameters) { try { // code class<?> commandclass = class.forname(classname); method method = commandclass.getmethod(methodname, paridentifiers); method.invoke(method, parameters); } catch (nosuchmethodexception | securityexception | classnotfoundexception | illegalaccessexception | illegalargumentexception | invocationtargetexception e) { e.printstacktrace(); } } public static void main(string[] args) { class<?>[] types = new class<?>[]{string.class, string.class}; object[] params = new object[]{"param1", "param2"}; invokemethod("foo.class", "method", types, params); }
Comments
Post a Comment