KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > Trace


1 package samples;
2 import net.sf.cglib.proxy.*;
3 import java.util.*;
4 /**
5  *
6  * @author baliuka
7  */

8 public class Trace implements MethodInterceptor {
9     
10     int ident = 1;
11     static Trace callback = new Trace();
12     
13     /** Creates a new instance of Trace */
14     private Trace() {
15     }
16     
17     public static Object JavaDoc newInstance( Class JavaDoc clazz ){
18       try{
19             Enhancer e = new Enhancer();
20             e.setSuperclass(clazz);
21             e.setCallback(callback);
22             return e.create();
23       }catch( Throwable JavaDoc e ){
24          e.printStackTrace();
25          throw new Error JavaDoc(e.getMessage());
26       }
27     
28     }
29     /**
30      * @param args the command line arguments
31      */

32     public static void main(String JavaDoc[] args) {
33         List list = (List)newInstance(Vector.class);
34         Object JavaDoc value = "TEST";
35         list.add(value);
36         list.contains(value);
37         try{
38          list.set(2, "ArrayIndexOutOfBounds" );
39         }catch( ArrayIndexOutOfBoundsException JavaDoc ignore ){
40         
41         }
42        list.add(value + "1");
43        list.add(value + "2");
44        list.toString();
45        list.equals(list);
46        list.set( 0, null );
47        list.toString();
48        list.add(list);
49        list.get(1);
50        list.toArray();
51        list.remove(list);
52        list.remove("");
53        list.containsAll(list);
54        list.lastIndexOf(value);
55     }
56
57
58     public Object JavaDoc intercept(Object JavaDoc obj, java.lang.reflect.Method JavaDoc method, Object JavaDoc[] args,
59                             MethodProxy proxy) throws Throwable JavaDoc {
60         printIdent(ident);
61         System.out.println( method );
62         for( int i = 0; i < args.length; i++ ){
63           printIdent(ident);
64           System.out.print( "arg" + (i + 1) + ": ");
65           if( obj == args[i])
66               System.out.println("this");
67           else
68               System.out.println(args[i]);
69         }
70         ident++;
71
72         Object JavaDoc retValFromSuper = null;
73         try {
74             retValFromSuper = proxy.invokeSuper(obj, args);
75             ident--;
76         } catch (Throwable JavaDoc t) {
77             ident--;
78             printIdent(ident);
79             System.out.println("throw " + t );
80             System.out.println();
81             throw t.fillInStackTrace();
82         }
83         
84         printIdent(ident);
85         System.out.print("return " );
86         if( obj == retValFromSuper)
87             System.out.println("this");
88         else System.out.println(retValFromSuper);
89         
90         if(ident == 1)
91              System.out.println();
92         
93         return retValFromSuper;
94     }
95     
96    void printIdent( int ident ){
97        
98     
99        while( --ident > 0 ){
100          System.out.print(".......");
101        }
102       System.out.print(" ");
103    }
104     
105 }
106
Popular Tags