1 18 package org.apache.beehive.netui.util.internal.cache; 19 20 import java.lang.reflect.Method ; 21 import org.apache.beehive.netui.util.internal.concurrent.InternalConcurrentHashMap; 22 23 import org.apache.beehive.netui.util.logging.Logger; 24 25 28 public class MethodCache { 29 30 private static final Logger LOGGER = Logger.getInstance(MethodCache.class); 31 32 private final InternalConcurrentHashMap _methodCache; 33 34 37 public MethodCache() { 38 _methodCache = new InternalConcurrentHashMap(); 39 } 40 41 44 public final Method [] getMethods(Class type) { 45 if(LOGGER.isDebugEnabled()) LOGGER.debug("type: " + type + " hash code: " + type.hashCode()); 46 47 Object obj = _methodCache.get(type); 48 49 if(obj == null) { 50 obj = type.getMethods(); 51 _methodCache.put(type, obj); 52 } 53 54 return (Method [])obj; 55 } 56 57 60 public final Method getMethod(Class type, String methodName, int argCount) { 61 if(LOGGER.isDebugEnabled()) LOGGER.debug("Get method \"" + methodName + "\" from type \"" + type + "\" with " + argCount + " params"); 62 63 if(methodName == null) 64 return null; 65 66 Method [] methods = getMethods(type); 67 68 for(int i = 0; i < methods.length; i++) { 69 if(methods[i].getName().equals(methodName) && (argCount == methods[i].getParameterTypes().length)) 70 return methods[i]; 71 } 72 73 return null; 74 } 75 76 79 public final Method getMethod(Class type, String methodName, String [] argTypes) { 80 if(methodName == null) 81 return null; 82 83 Method [] methods = getMethods(type); 84 Class [] parameterTypes = null; 85 86 for(int i = 0; i < methods.length; i++) { 87 if(!methods[i].getName().equals(methodName)) 89 continue; 90 91 parameterTypes = methods[i].getParameterTypes(); 93 94 if((argTypes == null || argTypes.length == 0) && parameterTypes.length == 0) 96 return methods[i]; 97 else if((argTypes == null || argTypes.length == 0) && !(parameterTypes.length == 0)) 99 continue; 100 else if(parameterTypes != null && parameterTypes.length == argTypes.length) { 102 boolean match = true; 103 for(int j = 0; j < parameterTypes.length; j++) { 104 if(!parameterTypes[j].getName().equals(argTypes[j])) { 105 match = false; 106 break; 107 } 108 } 109 if(match) return methods[i]; 110 } 111 } 112 113 return null; 114 } 115 116 119 public final Method getMethod(Class type, String methodName, Class [] argTypes) { 120 if(argTypes == null) 121 return getMethod(type, methodName, (String [])null); 122 123 String [] typeStrs = new String [argTypes.length]; 124 for(int i = 0; i < argTypes.length; i++) { 125 typeStrs[i] = argTypes[i].getName(); 126 } 127 128 return getMethod(type, methodName, typeStrs); 129 } 130 } 131 | Popular Tags |