1 16 package org.apache.axis.utils.cache; 17 18 import java.lang.reflect.Method ; 19 import java.util.Arrays ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import org.apache.axis.utils.ClassUtils; 24 25 34 public class MethodCache { 35 38 transient private static MethodCache instance; 39 40 44 transient private static ThreadLocal cache; 45 46 50 private MethodCache() { 51 cache = new ThreadLocal (); 52 } 53 54 58 public static MethodCache getInstance() { 59 if (instance == null) { 60 instance = new MethodCache(); 61 } 62 return instance; 63 } 64 65 68 private Map getMethodCache() { 69 Map map = (Map ) cache.get(); 70 if (map == null) { 71 map = new HashMap (); 72 cache.set(map); 73 } 74 return map; 75 } 76 77 81 static class MethodKey { 82 83 private final String methodName; 84 85 private final Class [] parameterTypes; 86 87 93 MethodKey(String methodName, Class [] parameterTypes) { 94 this.methodName = methodName; 95 this.parameterTypes = parameterTypes; 96 } 97 98 public boolean equals(Object other) { 99 MethodKey that = (MethodKey) other; 100 return this.methodName.equals(that.methodName) 101 && Arrays.equals(this.parameterTypes, 102 that.parameterTypes); 103 } 104 105 public int hashCode() { 106 return methodName.hashCode(); 111 } 112 } 113 114 115 private static final Object NULL_OBJECT = new Object (); 116 117 127 public Method getMethod(Class clazz, 128 String methodName, 129 Class [] parameterTypes) 130 throws NoSuchMethodException { 131 String className = clazz.getName(); 132 Map cache = getMethodCache(); 133 Method method = null; 134 Map methods = null; 135 136 159 MethodKey key = new MethodKey(methodName, parameterTypes); 161 methods = (Map ) cache.get(clazz); 162 if (methods != null) { 163 Object o = methods.get(key); 164 if (o != null) { if (o instanceof Method ) { return (Method ) o; 167 } else { return null; 173 } 174 } else { 175 } 177 } else { 178 } 180 181 try { 182 method = clazz.getMethod(methodName, parameterTypes); 183 } catch (NoSuchMethodException e1) { 184 if (!clazz.isPrimitive() && !className.startsWith("java.") && !className.startsWith("javax.")) { 185 try { 186 Class helper = ClassUtils.forName(className + "_Helper"); 187 method = helper.getMethod(methodName, parameterTypes); 188 } catch (ClassNotFoundException e2) { 189 } 190 } 191 } 192 193 if (methods == null) { 195 methods = new HashMap (); 196 cache.put(clazz, methods); 197 } 198 199 203 if (null == method) { 204 methods.put(key, NULL_OBJECT); 205 } else { 206 methods.put(key, method); 207 } 208 return method; 209 } 210 } 211 | Popular Tags |