反射方法
/**
* @param methodObject 方法所在的对象
* @param methodName 方法名
* @param args 方法名参数数组
*/
public Object invokeMethod(Object methodObject, String methodName, Object[] args)
throws Exception {
Class ownerClass = methodObject.getClass();
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName, argsClass);
return method.invoke(methodObject, args);
}
调用方式:
String[] args={,,,};
invokeMethod(new MyObject(),getName,args);