public static void doSome(String className, String methodName,String[]methodArgs){
Class c = Class.forName(className);
Object target = c.newInstance();
if(methodArgs == null) {
Method m = c.getMethod(methodName);
m.invoke(target);
} else if(methodArgs.length == 1) {
Method m = c.getMethod(methodName,methodArgs[0].getClass());
m.invoke(target,methodArgs[0]);
} else if(methodArgs.length == 2) {
Method m = c.getMethod(methodName,methodArgs[0].getClass(),methodArgs[1].getClass());
m.invoke(target,methodArgs[0],methodArgs[1]);
} else if(methodArgs.length == 3) {
Method m = c.getMethod(methodName,methodArgs[0].getClass(),methodArgs[1].getClass(),methodArgs[2].getClass());
m.invoke(target,methodArgs[0],methodArgs[1],methodArgs[2]);
} else if(methodArgs.length == 4) {
Method m = c.getMethod(methodName,methodArgs[0].getClass(),methodArgs[1].getClass(),methodArgs[2].getClass(),methodArgs[3].getClass());
m.invoke(target,methodArgs[0],methodArgs[1],methodArgs[2],methodArgs[3]);
} else if(methodArgs.length == 5) {
Method m = c.getMethod(methodName,methodArgs[0].getClass(),methodArgs[1].getClass(),methodArgs[2].getClass(),methodArgs[3].getClass(),methodArgs[4].getClass());
m.invoke(target,methodArgs[0],methodArgs[1],methodArgs[2],methodArgs[3],methodArgs[4]);
}
}
The code is like this, which is to implement a dynamic proxy through reflection. The parameter is the class name of the proxy class, the name of the method to be executed by the proxy class, and the parameter of the method executed by the proxy class. How can it be written better?
(that is, the parameter problem of the proxied class. When there are several parameters invoke, you need to add several parameters, and when you get method, you need to add several parameter class names)