1 package test; 2 3 import java.lang.reflect.Method ; 4 import java.lang.reflect.Modifier ; 5 import alt.jiapi.*; 6 import alt.jiapi.instrumentor.*; 7 import alt.jiapi.reflect.Loader; 8 import alt.jiapi.reflect.*; 9 import alt.jiapi.util.InstrumentingClassLoader; 10 11 14 public class CopyMethodTest { 15 private Class clazz; 16 17 public static void main(String [] args) throws Exception { 18 CopyMethodTest cmt = new CopyMethodTest(); 19 cmt.run(args); 20 } 21 22 public CopyMethodTest() throws Exception { 23 InstrumentationContext ctx = new InstrumentationContext(); 24 Instrumentor createMethod = 25 new CreateMethodInstrumentor(Modifier.PUBLIC + Modifier.STATIC, 26 "bar"); 27 28 Loader loader = ctx.getLoader(); 29 JiapiClass sourceClass = loader.loadClass("test.Bar"); 30 JiapiMethod sourceMethod = 31 sourceClass.getDeclaredMethod("bar", new String [0]); 32 33 Instrumentor copyMethod 34 = new CopyInstrumentor(sourceMethod.getInstructionList()); 35 36 InstrumentationDescriptor id = new InstrumentationDescriptor(); 37 InstrumentorChain chain = new InstrumentorChain(); 38 chain.add(createMethod); 39 chain.add(copyMethod); 40 41 id.addChain(chain); 42 id.addInclusionRule("test.Foo"); 43 ctx.addInstrumentationDescriptor(id); 44 ClassLoader cl = InstrumentingClassLoader.createClassLoader(ctx); 45 this.clazz = cl.loadClass("test.Foo"); 46 } 47 48 public void run(String [] args) throws Exception { 49 java.lang.reflect.Method m = 50 clazz.getMethod("bar", new Class [] {}); 51 52 System.out.println("trying to invoke the added method 'bar'..."); 53 m.invoke(clazz, null); 54 System.out.println("OK."); 55 } 56 } 57 | Popular Tags |