}
/**
* 用对象反射调用它的某个方法(没有参数的方法)
*
* @param className
* 类名
* @param obj
* 对象
* @param methodName
* 方法名
* @return [返回类型说明]
*/
public static Object invokeMethodWithObj(String className, Object obj, String methodName) {
return invokeMethodWithObjHasParame(className, obj, methodName, new Object[0]);
}
/**
* 用对象反射调用它的某个方法(有参数的方法)
*
* @param className
* 类名
* @param obj
* 对象
* @param methodName
* 方法名
* @param parameter
* 参数数组
* @return Object
*/
public static Object invokeMethodWithObjHasParame(String className, Object obj, String methodName,
Object[] parameter) {
return invokeMethodWithObjHasSpecialParame(className, obj, methodName, parameter, getParameterClass(parameter));
}
/**
* 获取参数列表的class对象
*
* @param parameter
* 参数值数组
* @return Class[]
*/
@SuppressWarnings("rawtypes")
private static Class[] getParameterClass(Object[] parameter) {
Class[] methodParameters = null;
if (parameter != null && parameter.length > 0) {
methodParameters = new Class[parameter.length];
for (int i = 0; i < parameter.length; i++) {
methodParameters[i] = parameter[i].getClass();
}
}
return methodParameters;
}
/**
* 用对象反射调用它的某个方法(指定参数类型的方法)
*
* @param className
* 类名
* @param obj
* 对象
* @param methodName
* 方法名
* @param parameter
* 参数数组
* @param methodParameters
* 参数类型数组
* @return Object
*/
@SuppressWarnings("rawtypes")
public static Object invokeMethodWithObjHasSpecialParame(String className, Object obj, String methodName,
Object[] parameter, Class[] methodParameters) {
Object object = null;
try {
Method method = Class.forName(className).getMethod(methodName.trim(), methodParameters);
object = method.invoke(obj, parameter);
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
catch (SecurityException e) {
e.printStackTrace();
}
catch (NoSuchMethodException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
return object;
}
/**
* 反射获取一个类的方法信息 包括参数,方法名,返回类型 www.2cto.com
*
* @param className
* 类名
* @return List
*/
@SuppressWarnings("rawtypes")
public static List
List
try {
// 通过getMethods得到类中包含的方法
Class myClass = Class.forName(className);
Method m[] = myClass.getDeclaredMethods();
for (int i = 0; i < m.length; i++) {
String meth = m[i].toString();
// 截取出所有的参数,参