KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > tracing > Trace


1 package samples.tracing;
2
3 import net.sf.cglib.proxy.MethodProxy;
4 import net.sf.cglib.proxy.MethodInterceptor;
5 import net.sf.cglib.proxy.Enhancer;
6
7 /**
8  * This class is taken from the regular cglib samples.
9  */

10 public class Trace implements MethodInterceptor {
11
12     public static class Target {
13         static int s_count = 0;
14
15         private int m_count;
16
17         public Target() {
18             m_count = s_count++;
19         }
20
21         public void step1() {
22             step2(new Target());
23         }
24
25         public void step2(Target arg) {
26             step3();
27         }
28
29         public void step3() {
30         }
31
32         public String JavaDoc toString() {
33             return "I am Target " + m_count;
34         }
35     }
36
37     int ident = 1;
38     static Trace callback = new Trace();
39
40     public static Object JavaDoc newInstance(Class JavaDoc clazz) {
41         try {
42             Enhancer e = new Enhancer();
43             e.setSuperclass(clazz);
44             e.setCallback(callback);
45             return e.create();
46         } catch (Throwable JavaDoc e) {
47             e.printStackTrace();
48             throw new Error JavaDoc(e.getMessage());
49         }
50
51     }
52
53     public static void main(String JavaDoc[] args) {
54         Target target = (Target) newInstance(Target.class);
55         target.step1();
56     }
57
58
59     public Object JavaDoc intercept(Object JavaDoc obj, java.lang.reflect.Method JavaDoc method, Object JavaDoc[] args, MethodProxy proxy)
60             throws Throwable JavaDoc {
61         printIdent(ident);
62         System.out.println(method);
63         for (int i = 0; i < args.length; i++) {
64             printIdent(ident);
65             System.out.print("arg" + (i + 1) + ": ");
66             if (obj == args[i]) {
67                 System.out.println("this");
68             } else {
69                 System.out.println(args[i]);
70             }
71         }
72         ident++;
73
74         Object JavaDoc retValFromSuper = null;
75         try {
76             retValFromSuper = proxy.invokeSuper(obj, args);
77             ident--;
78         } catch (Throwable JavaDoc t) {
79             ident--;
80             printIdent(ident);
81             System.out.println("throw " + t);
82             System.out.println();
83             throw t.fillInStackTrace();
84         }
85
86         printIdent(ident);
87         System.out.print("return ");
88         if (obj == retValFromSuper) {
89             System.out.println("this");
90         } else {
91             System.out.println(retValFromSuper);
92         }
93
94         if (ident == 1) {
95             System.out.println();
96         }
97
98         return retValFromSuper;
99     }
100
101     void printIdent(int ident) {
102         while (--ident > 0) {
103             System.out.print(".......");
104         }
105         System.out.print(" ");
106     }
107
108     private Trace() {
109     }
110 }
Popular Tags