KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > CopyMethodTest


1 package test;
2
3 import java.lang.reflect.Method JavaDoc;
4 import java.lang.reflect.Modifier JavaDoc;
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 /**
12  * Used to test coping of a method from one class to another.
13  */

14 public class CopyMethodTest {
15     private Class JavaDoc clazz;
16
17     public static void main(String JavaDoc [] args) throws Exception JavaDoc {
18         CopyMethodTest cmt = new CopyMethodTest();
19         cmt.run(args);
20     }
21
22     public CopyMethodTest() throws Exception JavaDoc {
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 JavaDoc[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 JavaDoc cl = InstrumentingClassLoader.createClassLoader(ctx);
45         this.clazz = cl.loadClass("test.Foo");
46     }
47
48     public void run(String JavaDoc[] args) throws Exception JavaDoc {
49         java.lang.reflect.Method JavaDoc m =
50             clazz.getMethod("bar", new Class JavaDoc [] {});
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