KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > CallTrace


1 package test;
2
3 import java.util.StringTokenizer JavaDoc;
4
5 import alt.jiapi.event.MethodEventProducer;
6 import alt.jiapi.event.MethodListener;
7 import alt.jiapi.event.MethodEvent;
8 import alt.jiapi.*;
9 import alt.jiapi.util.Bootstrapper;
10
11 /**
12  * This class is mainly used to test Jiapi event model.
13  * In addition to that, it servers as an example of using
14  * Jiapi event model.<p>
15  * There is a not so obvious bug in this example/test.
16  *
17  * While this example traces method entries and exits, it failes
18  * on exceptions. That is, Exception is not considered method exit,
19  * and indentation of output is not correct if method leaves through
20  * an Exception.<p>
21  * This will be corrected as soon as we have a suitable instrumentor.
22  */

23 public class CallTrace implements MethodListener {
24     private String JavaDoc prefix = System.getProperty("prefix", "");
25     private static String JavaDoc resolution = System.getProperty("resolution", "*");
26     /**
27      * Entry point.<br>
28      * Following System properties may be used to tune this utility.
29      * <ul>
30      * <li>include - ':' separated list of inclusion rules. Mandatory.
31      * <li>exclude - ':' separated list of exclusion rules. Optional.
32      * <li>resolution - Method resolution, like 'get*'
33      * <li>prefix - Prefix that gets printed on each event. Optional.
34      * This is useful for tracing software, that prints into
35      * System.out. One may use tools like grep, to filter console
36      * to show only events.
37      * </ul>
38      */

39     public static void main(String JavaDoc [] args) throws Exception JavaDoc {
40         // Create context using provider of our choice
41
InstrumentationContext ctx =
42             new InstrumentationContext();
43
44         // Create Descriptor. No need to to add Instrumentors or
45
// Patches, since MethodEventProducer below will do it
46
// for our convenience. Behind the scenes.
47
InstrumentationDescriptor id = new InstrumentationDescriptor();
48
49         String JavaDoc inclusions = System.getProperty("include");
50         if (inclusions != null) {
51             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(inclusions, ":");
52             while (st.hasMoreTokens()) {
53                 id.addInclusionRule(st.nextToken());
54             }
55         }
56         else {
57             usage();
58             System.exit(1);
59         }
60
61         String JavaDoc exclusions = System.getProperty("exclude");
62         if (exclusions != null) {
63             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(exclusions, ":");
64             while (st.hasMoreTokens()) {
65                 id.addExclusionRule (st.nextToken());
66             }
67         }
68
69         // Store Descriptor to Context
70
ctx.addInstrumentationDescriptor(id);
71
72         // This will provide Instrumentors and Patches needed in this
73
// test/sample
74
MethodEventProducer mep = new MethodEventProducer(id, resolution);
75
76         // Add MethodEventListner ----
77
CallTrace et = new CallTrace();
78         mep.addMethodListener(et);
79
80         // The first argument is class name, the rest are String args.
81
String JavaDoc [] _args = new String JavaDoc[args.length - 1];
82         System.arraycopy(args, 1, _args, 0, _args.length);
83         Bootstrapper.launch(args[0], _args, ctx, CallTrace.class.getClassLoader());
84     }
85
86
87     int eventCount = 0;
88     public void methodEntered (MethodEvent event) {
89         System.out.println(prefix + eventCount++ + ":" + indentation +
90                            event.getClassName() + "." + event.getMethodName());
91         indentation = indentMore();
92     }
93
94     public void methodExited (MethodEvent event) {
95         indentation = indentLess();
96     }
97
98      String JavaDoc indentation = "";
99     private String JavaDoc indentMore() {
100         return indentation + " ";
101     }
102
103     private String JavaDoc indentLess() {
104         if (indentation.length() < 3) {
105             return "";
106         }
107         return indentation.substring(0, indentation.length() - 3);
108     }
109
110     private static void usage() {
111         System.out.println("usage : ");
112         System.out.println(" java -Dinclude=foo.*:bar.* [-Dexclude=foobar.*] test.CallTrace test.Foo");
113     }
114 }
115
Popular Tags