KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > agent > HotSpotTransformer


1 package alt.jiapi.agent;
2
3 import java.util.Properties JavaDoc;
4
5 import alt.jiapi.InstrumentationContext;
6 import alt.jiapi.InstrumentationDescriptor;
7 import alt.jiapi.reflect.JiapiClass;
8 import alt.jiapi.util.HotSpotAdvice;
9 import alt.jiapi.util.HotSpotAdvisor;
10
11 /**
12  * Transforms classes. Currently, only method invocations are
13  * subject to instrumentation. Following is a list of properties,
14  * that can be given as an argument to <i>-javaagent</i> command
15  * line switch
16  * <ul>
17  * <li><b>advice=fq_name_of_advice_class</b>: Names a class that
18  * extends <i>alt.jiapi.util.HotSpotAdvice</i>
19  * <li><b>inclusion-rule=rule_string</b>: A regular expression indicating
20  * which classes are to be instrumented.
21  * <li><b>ir=rule_string</b>: same as above
22  * <li><b>exclusion-rule=rule_string</b>: A regular expression indicating
23  * which classes are not to be instrumented.
24  * <li><b>er=rule_string</b>: same as above
25  * <li><b>resolution=rule_string</b>: A regular expression indicating
26  * which method invocations should be the subject of instrumentation.
27  * <li><b>dump</b>: If given, dumps instrumented class into
28  * working directory
29  * </ul>
30  *
31  * @see alt.jiapi.util.HotSpotAdvice
32  */

33 public class HotSpotTransformer extends Transformer {
34     private InstrumentationContext context;
35     private Properties JavaDoc properties;
36
37     public HotSpotTransformer() {
38     }
39     public void init(Properties JavaDoc p) {
40     this.properties = p;
41     String JavaDoc aName = p.getProperty("advice","alt.jiapi.agent.DefaultAdvice");
42     String JavaDoc iRule = p.getProperty("inclusion-rule");
43     String JavaDoc eRule = p.getProperty("exclusion-rule");
44     String JavaDoc resolution = p.getProperty("resolution", "*");
45     
46     if (iRule == null) {
47         iRule = p.getProperty("ir");
48     }
49     if (eRule == null) {
50         eRule = p.getProperty("er");
51     }
52
53     if (iRule != null) {
54         try {
55         Class JavaDoc c = Class.forName(aName);
56         Object JavaDoc o = c.newInstance();
57         HotSpotAdvice hsa = (HotSpotAdvice)o;
58
59         InstrumentationContext ctx = new InstrumentationContext();
60         InstrumentationDescriptor id = new InstrumentationDescriptor();
61         id.addInclusionRule(iRule);
62         if (eRule != null) {
63             id.addExclusionRule(eRule);
64         }
65
66         ctx.addInstrumentationDescriptor(id);
67         
68         HotSpotAdvisor hsi =
69             new HotSpotAdvisor(id, hsa, HotSpotAdvisor.INVOCATIONS,
70                        resolution);
71
72         this.context = ctx;
73         }
74         catch(Exception JavaDoc e) {
75         e.printStackTrace();
76         }
77     }
78     else {
79         System.out.println("Warning: No inclusion rule given. This Transformer will do nothing.");
80     }
81     }
82
83     /**
84      * Instrument given JiapiClass.
85      */

86     public boolean transform(JiapiClass clazz) {
87     if (context != null) {
88         context.instrument(clazz);
89
90         if (properties.getProperty("dump") != null) {
91         try {
92             java.io.FileOutputStream JavaDoc fos =
93             new java.io.FileOutputStream JavaDoc(clazz.getName() + ".clazz");
94             clazz.dump(fos);
95         }
96         catch(Exception JavaDoc e) {
97             System.out.println("Failed to dump " + clazz.getName() +e);
98         }
99         }
100
101         return true;
102     }
103
104     return false;
105     }
106
107     // restransforming is not supported
108
// public boolean retransform(JiapiClass clazz) {
109
// }
110
}
111
Popular Tags