1 package test; 2 3 import java.util.StringTokenizer ; 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 23 public class CallTrace implements MethodListener { 24 private String prefix = System.getProperty("prefix", ""); 25 private static String resolution = System.getProperty("resolution", "*"); 26 39 public static void main(String [] args) throws Exception { 40 InstrumentationContext ctx = 42 new InstrumentationContext(); 43 44 InstrumentationDescriptor id = new InstrumentationDescriptor(); 48 49 String inclusions = System.getProperty("include"); 50 if (inclusions != null) { 51 StringTokenizer st = new StringTokenizer (inclusions, ":"); 52 while (st.hasMoreTokens()) { 53 id.addInclusionRule(st.nextToken()); 54 } 55 } 56 else { 57 usage(); 58 System.exit(1); 59 } 60 61 String exclusions = System.getProperty("exclude"); 62 if (exclusions != null) { 63 StringTokenizer st = new StringTokenizer (exclusions, ":"); 64 while (st.hasMoreTokens()) { 65 id.addExclusionRule (st.nextToken()); 66 } 67 } 68 69 ctx.addInstrumentationDescriptor(id); 71 72 MethodEventProducer mep = new MethodEventProducer(id, resolution); 75 76 CallTrace et = new CallTrace(); 78 mep.addMethodListener(et); 79 80 String [] _args = new String [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 indentation = ""; 99 private String indentMore() { 100 return indentation + " "; 101 } 102 103 private String 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 |