KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > MethodAddTest


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
8 /**
9  * Used for testing method calls. Given without arguments
10  * static calls are made. With arguments, dynamic calls are made.
11  * This class implements Hook interface and sets hook method to
12  * be called based on the above.
13  */

14 public class MethodAddTest implements Hook {
15     private Class JavaDoc clazz;
16     private static Object JavaDoc instance;
17     private boolean callStatic;
18
19     public static void main(String JavaDoc [] args) throws Exception JavaDoc {
20         MethodAddTest mct = new MethodAddTest(args.length == 0);
21         mct.run(args);
22     }
23
24     public MethodAddTest(boolean callStatic) throws Exception JavaDoc {
25         this.callStatic = callStatic;
26
27         String JavaDoc className = System.getProperty("class", "test.Foo");
28         System.out.println("Loading class " + className);
29         InstrumentationContext ctx = new InstrumentationContext();
30         Instrumentor i = new CreateMethodInstrumentor(Modifier.PUBLIC,
31                                                       "foo2");
32
33         InstrumentationDescriptor id = new InstrumentationDescriptor();
34         id.addInstrumentation(i, null);
35         id.addInclusionRule("test.*");
36
37         // Should we add
38
// addExclusionRule(this.getClass().getName());
39
// to implementation of InstrumentationDescriptor
40
id.addExclusionRule("test.MethodAddTest");
41
42         ctx.addInstrumentationDescriptor(id);
43         Loader loader = ctx.getLoader();
44         ClassLoader JavaDoc cl = InstrumentingClassLoader.createClassLoader(loader);
45
46         this.clazz = cl.loadClass(className);
47     }
48
49     public void callMeDynamic(String JavaDoc s1, String JavaDoc s2) {
50         System.out.println ("callMeDynamic : " + s1 + ", " + s2);
51     }
52
53     public static void callMeStatic(String JavaDoc s1, String JavaDoc s2) {
54         System.out.println ("callMeStatic : " + s1 + ", " + s2);
55     }
56
57
58     // -- Interface Hook --
59
public Method getHookMethod() {
60         Method hookMethod = null;
61         Class JavaDoc [] params = new Class JavaDoc [] {String JavaDoc.class, String JavaDoc.class};
62
63         if (callStatic) {
64             System.out.println("Instrumenting for static calls");
65             try {
66                 hookMethod =
67                     this.getClass().getMethod("callMeStatic", params);
68             }
69             catch(Exception JavaDoc e) {
70                 // not possible, but just in case
71
throw new RuntimeException JavaDoc (e.toString ());
72             }
73         }
74         else {
75             System.out.println("Instrumenting for dynamic calls");
76             try {
77                 hookMethod =
78                     this.getClass().getMethod("callMeDynamic", params);
79             }
80             catch(Exception JavaDoc e) {
81                 // not possible, but just in case
82
throw new RuntimeException JavaDoc(e.toString ());
83             }
84         }
85
86         return hookMethod;
87     }
88
89
90     public void run(String JavaDoc[] args) throws Exception JavaDoc {
91         java.lang.reflect.Method JavaDoc m =
92             clazz.getMethod("main", new Class JavaDoc [] {String JavaDoc[].class});
93         
94         m.invoke(clazz, new Object JavaDoc [] {args});
95     }
96
97     public Object JavaDoc getInstance() {
98         return this;
99     }
100 }
101
Popular Tags