KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > InstrumentorTest


1 package test;
2
3 import java.lang.reflect.InvocationTargetException JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.util.StringTokenizer JavaDoc;
6
7 import alt.jiapi.*;
8 import alt.jiapi.instrumentor.*;
9 import alt.jiapi.util.Bootstrapper;
10 import alt.jiapi.reflect.Loader;
11
12 /**
13  * Class description. InstrumentorTest
14  *
15  * System properties used :
16  * <dl>
17  * <li>include=rule1:rule2:rule3:.... inclusion rules
18  * <li>exclude=rule1:rule2:rule3:.... exclusion rules
19  * <li>class=foo.Bar main-class, defaults to test.Foo
20  * </dl>
21  * Jiapi System properties
22  * <dl>
23  * <li>jiapi.debug Lots of debugging
24  * <li>jiapi.trace tracing
25  * <li>dump Dumps generated bytecode
26  * </dl>
27  *
28  * @author Mika Riekkinen
29  */

30 public class InstrumentorTest implements Hook {
31     protected InstrumentationContext ctx;
32     protected String JavaDoc className;
33
34     {
35         className = System.getProperty("class");
36         if (className == null) {
37             className = defaultClassName();
38         }
39     }
40
41     protected InstrumentorTest() {
42         try {
43             ctx = new InstrumentationContext();
44 // System.out.println("Context: " + ctx.getClass().getClassLoader());
45
}
46         catch (Exception JavaDoc e) {
47             e.printStackTrace();
48         }
49     }
50
51     protected void run(String JavaDoc [] args) {
52         Bootstrapper.launch(className, args, ctx, getClass().getClassLoader());
53     }
54
55     /**
56      * Handy method for adding an array of rules to descriptor
57      */

58     protected void addInclusionRules(InstrumentationDescriptor id,
59                                      String JavaDoc [] rules) throws JiapiException {
60         for (int i = 0; i < rules.length; i++) {
61             id.addInclusionRule(rules[i]);
62         }
63     }
64     /**
65      * Handy method for adding an array of rules to descriptor
66      */

67     protected void addExclusionRules(InstrumentationDescriptor id,
68                                      String JavaDoc [] rules) throws JiapiException {
69         for (int i = 0; i < rules.length; i++) {
70             id.addExclusionRule(rules[i]);
71         }
72     }
73
74     protected String JavaDoc defaultClassName() {
75         return "test.Foo";
76     }
77
78     /**
79      * Returns inclusion rules from system property 'include'
80      */

81     public String JavaDoc [] getInclusionRules() {
82         String JavaDoc inclusions = System.getProperty("include");
83         return tokenizeRules(inclusions);
84     }
85     /**
86      * Returns exclusion rules from system property 'exclude'
87      */

88     public String JavaDoc [] getExclusionRules() {
89         String JavaDoc exclusions = System.getProperty("exclude");
90         exclusions += ":" + this.getClass().getName() + ":test.InstrumentorTest";
91         return tokenizeRules(exclusions);
92     }
93
94     /**
95      * Tokenizes rules. Rules are separated with ':' character.
96      * For example : 'test.*:com.foo.*'
97      * @return An array of tokenized rules. If input 'rules' String is
98      * null, an ampty array(length 0) is returned.
99      */

100     protected String JavaDoc[] tokenizeRules(String JavaDoc rules) {
101         String JavaDoc[] __rules = null;
102         if (rules != null) {
103             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(rules, ":");
104             __rules = new String JavaDoc [st.countTokens()];
105             
106             for (int i = 0; i < __rules.length; i++) {
107                 __rules[i] = st.nextToken();
108             }
109         }
110
111         if (__rules == null) {
112             __rules = new String JavaDoc[0];
113         }
114
115         return __rules;
116     }
117
118     /**
119      * Default implementation of hook method used. Override this,
120      * if you do not like the output.
121      */

122     public void hookMethod(Object JavaDoc _this, String JavaDoc s2) {
123         System.out.println ("Hook called : " + _this + ", " + s2);
124     }
125
126
127     // -- Interface Hook --
128
/**
129      * Default implementation of getHookMethod.
130      * Returns <code>void hookMethod(Object, String)</code>
131      */

132     public Method getHookMethod() {
133         Method hookMethod = null;
134         Class JavaDoc [] params = new Class JavaDoc [] {Object JavaDoc.class, String JavaDoc.class};
135
136         try {
137             hookMethod = this.getClass().getMethod("hookMethod", params);
138         }
139         catch(Exception JavaDoc e) {
140             // not possible, but just in case
141
throw new RuntimeException JavaDoc (e.toString ());
142         }
143
144         return hookMethod;
145     }
146
147
148     public Object JavaDoc getInstance() {
149         return this;
150     }
151 }
152
Popular Tags