62,614
社区成员
发帖
与我相关
我的任务
分享
public Object callObjectMethod(Object o, String methodName, Object...args)
public Object callObjectMethod(Object o, String methodName, Object...args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Class<?> class1 = o.getClass();
Class[] methodArgs = null;
if(args != null){
methodArgs = new Class[args.length];
for(int i = 0; i < args.length; ++i)
methodArgs[i] = args[i].getClass();
}
Method method = class1.getMethod(methodName, methodArgs);
return method.invoke(o, args);
}
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class Hello {
public String hello(String s){
return "String hello(String s):" + s;
}
public String hello(int i){
return "String hello(int i):" + i;
}
}
public class Test {
public static Object test(Object o, String methodName, Class<?>[] arguments, Object ...objects) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Method method = o.getClass().getMethod(methodName, arguments);
Object invoke = method.invoke(o, objects);
return invoke;
}
public static void main(String[] args) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Hello hello = new Hello();
Class<?>[] c = {String.class};
System.out.println(test(hello, "hello", c, "abc"));
System.out.println(test(hello, "hello", new Class<?>[] {int.class}, 100));
}
}
Class[] parameter = method.getParameterTypes();
Object[] args = new Object[parameter.length];
for (int j = 0; j < parameter.length; j++) {
try {
if (parameter[j] == Map.class) {
args[j] = (Map) paraList.get(j);
}else if (parameter[j] == HashMap.class) {
args[j] = (HashMap) paraList.get(j);
}else if (parameter[j] == List.class) {
args[j] = (List) paraList.get(j);
}else if (parameter[j] == ArrayList.class) {
args[j] = (ArrayList) paraList.get(j);
}else if (parameter[j] == HttpServletRequest.class) {
args[j] = (HttpServletRequest) request;
} else if (parameter[j] == HttpServletResponse.class) {
args[j] = (HttpServletResponse) response;
}
} catch (Exception e) {
//返回错误,数据类型错误=========================================
throw new AppException(ajaxBean + "的参数和传入的参数类型不符。参数序号:" + ((Integer) j).toString() + ",参数类型:" + parameter[j].toString() + ",传入类型:" + paraList.get(j).getClass());
}
if (parameter[j] == String.class || parameter[j] == Boolean.class || parameter[j] == boolean.class || parameter[j] == Integer.class || parameter[j] == int.class || parameter[j] == float.class || parameter[j] == Float.class || parameter[j] == double.class || parameter[j] == Double.class || parameter[j] == Date.class) {
Object value = paraList.get(j);
if (parameter[j] == Integer.class || parameter[j] == int.class) {
if (value instanceof Integer) {
args[j] = value;
} else if (value instanceof Float) {
args[j] = ((Float) value).intValue();
} else if (value instanceof Double) {
args[j] = ((Double) value).intValue();
} else if (value instanceof String) {
args[j] = Integer.valueOf((String) value).intValue();
} else {
//返回错误,数据类型错误=========================================
throw new AppException(ajaxBean + "的参数和传入的参数类型不符。参数序号:" + ((Integer) j).toString() + ",参数类型:Integer,传入类型:" + value.getClass());
}
} else if (parameter[j] == float.class || parameter[j] == Float.class) {
if (value instanceof Float) {
args[j] = value;
} else if (value instanceof Integer) {
args[j] = (Float) value;
} else if (value instanceof Double) {
args[j] = ((Double) value).floatValue();
} else if (value instanceof String) {
args[j] = Float.valueOf((String) value).floatValue();
} else {
throw new AppException(ajaxBean + "的参数和传入的参数类型不符。参数序号:" + ((Integer) j).toString() + ",参数类型:Float,传入类型:" + value.getClass());
}
} else if (parameter[j] == double.class || parameter[j] == Double.class) {
if (value instanceof Double) {
args[j] = value;
} else if (value instanceof Float) {
args[j] = ((Float) value).doubleValue();
} else if (value instanceof Integer) {
args[j] = ((Integer) value).doubleValue();
} else if (value instanceof String) {
args[j] = Double.valueOf((String) value).doubleValue();
} else {
throw new AppException(ajaxBean + "的参数和传入的参数类型不符。参数序号:" + ((Integer) j).toString() + ",参数类型:Double,传入类型:" + value.getClass());
}
} else if (parameter[j] == Date.class) {
if (value instanceof Date) {
args[j] = Date.valueOf(value.toString());
} else if (value instanceof String) {
args[j] = Date.valueOf(value.toString());
} else {
//返回错误,数据类型错误=========================================
throw new AppException(ajaxBean + "的参数和传入的参数类型不符。参数序号:" + ((Integer) j).toString() + ",参数类型:Date,传入类型:" + value.getClass());
}
}
}
}