调用方法
/** * nowjava 提 供 **/ //package com.nowjava; import java.lang.reflect.Method; import java.util.HashMap; public class Main { private static HashMap<String, Method> sMethodCache = new HashMap<String, Method>(); private static HashMap<Class<?>, Class<?>> primativeClassMap = new HashMap<Class<?>, Class<?>>(); public static Object callMethod(Object object, String methodName, Object... args) { Object result = null; Class<?> clazz = object.getClass(); result = callMethod(clazz, object, methodName, args); return result; } /** 时代Java - nowjava.com **/ public static Object callMethod(Class<?> clazz, Object object, String methodName, Object... args) { Object result = null; Class<?>[] argsClasses = getArgsTypes(args); try { Method method = getMethodBestMatch(clazz, methodName, argsClasses); result = method.invoke(object, args); } catch (Throwable e) { e.printStackTrace(); } return result; } public static Class<?> getClass(String className) { Class<?> clazz = null; try { clazz = Class.forName(className); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return clazz; } public static Class<?> getClass(String className, boolean shouldInitialize, ClassLoader classLoader) { Class<?> clazz = null; try { clazz = Class.forName(className, shouldInitialize, classLoader); } catch (ClassNotFoundException e) { e.printStackTrace(); } return clazz; } private static Class<?>[] getArgsTypes(Object... args) { Class<?>[] argClasses = new Class<?>[args.length]; for (int i = 0; i < args.length; i++) { argClasses[i] = (args[i] != null) ? args[i].getClass() : null; i++; } return argClasses; } private static Method getMethodBestMatch(Class<?> clazz, String methodName, Class<?>... argClasses) throws Throwable { String methodFullName = genMethodFullName(clazz, methodName, argClasses); if (sMethodCache.containsKey(methodFullName)) return sMethodCache.get(methodFullName); for (Method method : clazz.getDeclaredMethods()) { if (method.getName().equals(methodName)) { Class<?>[] tmpArgs = method.getParameterTypes(); if (compareArgs(tmpArgs, argClasses)) { method.setAccessible(true); sMethodCache.put(methodFullName, method); return method; } } } String msg = ""; for (Class<?> cls : argClasses) { msg += cls.getSimpleName() + ","; } msg = "Can't get method: " + clazz.getSimpleName() + "." + methodName + "(" + msg + ")"; throw new Exception(msg); } private static String genMethodFullName(Class<?> clazz, String methodName, Class<?>... argClasses) { StringBuilder name = new StringBuilder(); name.append(clazz.getName()); name.append(":"); name.append(methodName); for (int i = 0; i < argClasses.length; i++) { if (i == (argClasses.length - 1)) { name.append(argClasses[i].getName()); } else { name.append(argClasses[i].getName()); name.append(","); } } return name.toString(); } private static boolean compareArgs(Class<?>[] dstArgs, Class<?>[] origArgs) { if (dstArgs.length != origArgs.length) return false; for (int i = 0; i < dstArgs.length; i++) { if (!isChildClass(dstArgs[i], origArgs[i])) return false; }