突然写了一个,不知道有用不,希望指正。 /** * 这个类主要是掉用参数传进来的对象中的方法, * 属于单列的模式, * 方法一旦被调用则会缓存在内存中,减少下此方法调用时反射的时间 */ package com.jw.dispense; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import com.jw.dispense.test.Test; public class DispenseMethod { public static final DispenseMethod dispense = new DispenseMethod(); private Map<String ,Method> methodMap ; private DispenseMethod(){ methodMap = new ConcurrentHashMap<String, Method>(); } public static DispenseMethod getInstance(){ return dispense; } /** * <p>Title: distribution</p> * <p>Description: 分发器,,服务端接收到了message,通过了服务端的业务后返回了一个MainMessage</p> @author JiangWei * @param session * @param message * @return * @throws ClassNotFoundException * @throws NoSuchMethodException * @throws SecurityException * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws NoSuchFieldException */ public Object distributionMethod(Object object,String methodName , Object[] parameters) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchFieldException{ Class<?> objClass = object.getClass(); Class<?>[] pclass = new Class[parameters.length]; Class<?> parClass = null; for (int i = 0; i < parameters.length; i++) { parClass = parameters[i].getClass(); if(Integer.class == parClass){ pclass[i] = Integer.TYPE; }else if(Double.class == parClass){ pclass[i] = Double.TYPE; }else if(Double.class == parClass){ pclass[i] = Double.TYPE; }else if(Short.class == parClass){ pclass[i] = Short.TYPE; }else if(Long.class == parClass){ pclass[i] = Long.TYPE; }else if(Float.class == parClass){ pclass[i] = Float.TYPE; }else if(Boolean.class == parClass){ pclass[i] = Boolean.TYPE; }else if(Character.class == parClass){ pclass[i] = Character.TYPE; }else if(Byte.class == parClass){ pclass[i] = Byte.TYPE; }else{ pclass[i] = parClass; } } // int double short long float,boolean char byte Method method = getMethodQ(objClass,methodName,pclass); return method.invoke(object, parameters); } private Method getMethodQ(Class<?> objClass,String methodName,Class<?>[] pclass) throws SecurityException, NoSuchMethodException{ StringBuffer keysb = new StringBuffer(); keysb.append(objClass.getName()+"."+methodName+"("); boolean isHasP = false; for(Class<?> pclas : pclass){ isHasP = true; keysb.append(pclas.getName()+","); } if(isHasP){ keysb.replace(keysb.lastIndexOf(","), keysb.lastIndexOf(",")+1, ""); } keysb.append(")"); String key = new String(keysb.toString()); Method method = methodMap.get(key); if(method == null){ System.out.println(key); method = objClass.getMethod(methodName,pclass); methodMap.put(key, method); } return method; } public static void main(String[] args) { DispenseMethod dispense = DispenseMethod.getInstance(); Test test = new Test(); try { dispense.distributionMethod(test,"test", new Object[]{1,"a"}); dispense.distributionMethod(test,"test", new Object[]{}); dispense.distributionMethod(test,"test", new Object[]{3}); dispense.distributionMethod(test,"test", new Object[]{4}); dispense.distributionMethod(test,"test", new Object[]{5}); } catch (Exception e) { e.printStackTrace(); } } } 测试需要用到的类 package com.jw.dispense.test; public class Test { public void test(int a){ System.out.println("OK,its worked:"+a); } public void test(){ System.out.println("OK,its worked:"+"wu"); } public void test(int a,String aa){ System.out.println("OK,its worked:"+a+aa); } } 执行结果 com.jw.dispense.test.Test.test(int,java.lang.String) OK,its worked:1a com.jw.dispense.test.Test.test() OK,its worked:wu com.jw.dispense.test.Test.test(int) OK,its worked:3 OK,its worked:4 OK,its worked:5