1. [代码][Java]代码 import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class InvokeUtil {
/*
* @param actionClass , methodName
* @description 传值class和string执行 类的方法
*/
public static Object execute(Class<?> actionClass,String methodName){
Method m ;
Object action ;
Object result = null ;
try {
m = actionClass.getMethod(methodName, null);
action = actionClass.newInstance();
result = m.invoke(action, null);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return result;
}
/*
* @param className , methodName
* @description 传值class名和string执行 类的方法
*/
public static Object execute(String className,String methodName){
Method m ;
Object action ;
Object result = null ;
try {
Class<?> actionClass = Class.forName(className);
execute(actionClass, methodName);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return result;
}
} |