1 package samples.interceptor; 2 3 import java.lang.reflect.InvocationHandler ; 4 5 import alt.jiapi.InstrumentationContext; 6 import alt.jiapi.InstrumentationDescriptor; 7 import alt.jiapi.reflect.Loader; 8 import alt.jiapi.reflect.JiapiClass; 9 import alt.jiapi.util.Bootstrapper; 10 import alt.jiapi.util.InstrumentingClassLoader; 11 12 import alt.jiapi.interceptor.*; 13 14 20 public class Sample1 implements InvocationHandler { 21 public static void main(String args[]) throws Exception { 22 String className = "samples.Foo"; 23 if (args.length > 0) { 24 className = args[0]; 25 } 26 27 new Sample1(className); 28 } 29 30 public Sample1(String className) throws Exception { 31 InstrumentationContext ctx = new InstrumentationContext(); 33 InstrumentationDescriptor id = new InstrumentationDescriptor(); 34 id.addInclusionRule("samples.*"); 35 ctx.addInstrumentationDescriptor(id); 36 37 InvocationInterceptor ii = new InvocationInterceptor(id, "samples*",this); 41 Bootstrapper.launch(className, null, ctx, 43 InstrumentingClassLoader.createClassLoader(ctx)); 44 } 45 46 47 56 public Object invoke(Object o, java.lang.reflect.Method m, Object [] args) throws Exception { 57 System.out.println(" Calling " + m.getName()); 58 59 for (int i = 0; i < args.length; i++) { 60 if (args[i] instanceof String ) { 63 args[i] = ((String )args[i]).toUpperCase(); 64 } 65 } 66 67 long l1 = System.currentTimeMillis(); 68 69 Object rv = m.invoke(o, args); 71 72 long l2 = System.currentTimeMillis(); 73 System.out.println(" " + m.getName() + " executed in " + 74 (l2 - l1) + " ms"); 75 76 if (rv instanceof Integer ) { 80 int i = ((Integer )rv).intValue(); 81 rv = new Integer (i + 1); 82 } 83 84 return rv; 85 } 86 } 87 | Popular Tags |