1 18 19 package alt.jiapi.interceptor; 20 21 import java.lang.reflect.InvocationHandler ; 22 import java.lang.reflect.Method ; 23 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 import org.apache.log4j.Category; 30 31 import alt.jiapi.InstrumentationContext; 32 import alt.jiapi.InstrumentationDescriptor; 33 import alt.jiapi.reflect.Signature; 34 35 import alt.jiapi.event.EventProducer; 36 37 55 public class InvocationInterceptor extends EventProducer { 56 private InvocationHandler handler; 57 private HashMap rmCache = new HashMap (); 58 59 66 public InvocationInterceptor(InstrumentationDescriptor id, InvocationHandler handler) { 67 this(id, "*", handler); 68 } 69 70 71 81 public InvocationInterceptor(InstrumentationDescriptor id, 82 String resolution, InvocationHandler handler){ 83 this(id, new String [] { resolution }, handler); 84 } 85 86 87 96 public InvocationInterceptor(InstrumentationDescriptor id, 97 String [] resolutions, 98 InvocationHandler handler) { 99 super(resolutions); 100 this.handler = handler; 101 102 id.addInstrumentor(new InvocationInstrumentor(this, handler)); 103 } 104 105 106 107 116 public Object invokeMethod(Object o, String name, Object [] args, 117 String signature) throws Throwable { 118 try { 119 Method m = null; 120 Class c = null; 121 if (o instanceof Class ) { c = (Class )o; 123 } 124 else { c = o.getClass(); 126 } 127 128 130 m = getReflectionMethod(c, name, args, signature); 132 135 Object r = handler.invoke(o, m, args); 136 return r; 137 } 138 catch(java.lang.reflect.InvocationTargetException ite) { 139 throw ite.getTargetException(); 140 } 141 catch(Throwable t) { 142 t.printStackTrace(); 143 throw t; 144 } 145 } 146 147 148 private Method getReflectionMethod(Class c, String name, Object [] args, 149 String signature) { 150 Method m = null; 151 152 m = (Method )rmCache.get(signature); 155 if (m != null) { 156 return m; 157 } 158 159 String sign = signature.substring(signature.lastIndexOf('(')); 160 Signature s = new Signature(sign); 161 162 String [] paramTypes = s.getParameters(); 163 164 try { 165 Class [] params = params = new Class [args.length]; 166 167 for (int i = 0; i < args.length; i++) { 168 params[i] = args[i].getClass(); 169 170 if ("int".equals(paramTypes[i])) { 174 params[i] = Integer.TYPE; 175 } 176 else if ("long".equals(paramTypes[i])) { 177 params[i] = Long.TYPE; 178 } 179 else if ("char".equals(paramTypes[i])) { 180 params[i] = Character.TYPE; 181 } 182 else if ("boolean".equals(paramTypes[i])) { 183 params[i] = Boolean.TYPE; 184 } 185 else if ("byte".equals(paramTypes[i])) { 186 params[i] = Byte.TYPE; 187 } 188 else if ("float".equals(paramTypes[i])) { 189 params[i] = Float.TYPE; 190 } 191 else if ("double".equals(paramTypes[i])) { 192 params[i] = Double.TYPE; 193 } 194 } 195 196 m = c.getMethod(name, params); 197 } 198 catch(Exception e) { 199 e.printStackTrace(); 200 } 201 202 rmCache.put(signature, m); 203 204 return m; 205 } 206 } 207 208 | Popular Tags |