1 3 package jodd.introspector; 4 5 import jodd.util.ReflectUtil; 6 7 import java.lang.reflect.Method ; 8 import java.util.ArrayList ; 9 import java.util.HashMap ; 10 import java.util.List ; 11 import java.util.Iterator ; 12 13 16 class Methods { 17 18 HashMap mMap = new HashMap (); 19 Method [] allMethods; 20 boolean locked = false; 21 22 int count; 23 void addMethod(String name, Method method) { 24 if (locked == true) { 25 throw new IllegalStateException ("Methods introspection is already finished."); 26 } 27 count++; 28 List paramList = (List ) mMap.get(name); 29 if (paramList == null) { 30 paramList = new ArrayList (); 31 mMap.put(name, paramList); 32 } 33 paramList.add(new MethodDescriptor(method)); 34 } 35 36 void lock() { 37 HashMap newMap = new HashMap (mMap.size()); 38 locked = true; 39 allMethods = new Method [count]; 40 Iterator it = mMap.keySet().iterator(); 41 int k = 0; 42 while (it.hasNext()) { 43 String name = (String ) it.next(); 44 List list = (List ) mMap.get(name); 45 MethodEntry entry = new MethodEntry(); 46 entry.size = list.size(); 47 entry.methodsList = new Method [entry.size]; 48 entry.paramterTypes = new Class [entry.size][]; 49 for (int i = 0; i < entry.size; i++) { 50 MethodDescriptor md = (MethodDescriptor) list.get(i); 51 allMethods[k] = md.method; 52 k++; 53 entry.methodsList[i] = md.method; 54 entry.paramterTypes[i] = md.parameterTypes; 55 } 56 newMap.put(name, entry); 57 } 58 mMap = newMap; 59 } 60 61 63 Method getMethod(String name, Class [] paramTypes) { 64 MethodEntry entry = (MethodEntry) mMap.get(name); 65 if (entry == null) { 66 return null; 67 } 68 for (int i = 0; i < entry.size; i++) { 69 if (ReflectUtil.compareParameteres(entry.paramterTypes[i], paramTypes) == true) { 70 return entry.methodsList[i]; 71 } 72 } 73 return null; 74 } 75 76 Method getMethod(String name) { 77 MethodEntry entry = (MethodEntry) mMap.get(name); 78 if (entry == null) { 79 return null; 80 } 81 if (entry.size != 1) { 82 throw new IllegalArgumentException ("Method '" + name + "' is not unique!"); 83 } 84 return entry.methodsList[0]; 85 } 86 87 Method [] getAllMethods(String name) { 88 MethodEntry entry = (MethodEntry) mMap.get(name); 89 if (entry == null) { 90 return null; 91 } 92 return entry.methodsList; 93 } 94 95 Method [] getAllMethods() { 96 return allMethods; 97 } 98 } 99 100 101 102 class MethodDescriptor { 103 final Method method; 104 final Class [] parameterTypes; 105 static final Class [] NOPARAMS = new Class [0]; 106 MethodDescriptor(Method method) { 107 Class [] params = method.getParameterTypes(); 108 if (params == null) { 109 this.parameterTypes = NOPARAMS; 110 } else { 111 this.parameterTypes = params; 112 } 113 this.method = method; 114 } 115 } 116 117 class MethodEntry { 118 Method [] methodsList; 119 Class [][] paramterTypes; 120 int size; 121 } | Popular Tags |