1 package samples.interceptor; 2 3 import java.lang.reflect.Field ; 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 Sample2 implements AccessAdvisor, FieldHandler { 21 public static void main(String args[]) throws Exception { 22 new Sample2(); 23 } 24 25 public Sample2() throws Exception { 26 InstrumentationContext ctx = new InstrumentationContext(); 28 InstrumentationDescriptor id = new InstrumentationDescriptor(); 29 id.addInclusionRule("samples.*"); 30 ctx.addInstrumentationDescriptor(id); 31 32 36 FieldInterceptor fi = new FieldInterceptor(id, "samples*",this); 38 42 Bootstrapper.launch("samples.Foo", null, ctx, 44 InstrumentingClassLoader.createClassLoader(ctx)); 45 } 46 47 48 57 public Object get(Object o, String name, Object value) { 58 System.out.println(" Getting field " + name + ": "); 59 60 if (value instanceof String ) { 61 value = ((String )value).toUpperCase(); 62 } 63 else if (value instanceof Integer ) { 64 value = new Integer (100); 65 } 66 67 return value; 68 } 69 70 71 80 public Object set(Object o, String name, Object value) { 81 System.out.println(" Setting field " + name + ": " + value); 82 return value; 83 } 84 85 86 public void setField(Object target, Field field, Object value) throws Exception { 88 field.set(target, value); 90 } 91 92 public Object getField(Object target, Field field) throws Exception { 93 System.out.println("getting"); 94 return field.get(target); 95 } 96 } 97 | Popular Tags |