001: package samples;
002:
003: import net.sf.cglib.proxy.*;
004: import java.util.*;
005:
006: /**
007: *
008: * @author baliuka
009: */
010: public class Trace implements MethodInterceptor {
011:
012: int ident = 1;
013: static Trace callback = new Trace();
014:
015: /** Creates a new instance of Trace */
016: private Trace() {
017: }
018:
019: public static Object newInstance(Class clazz) {
020: try {
021: Enhancer e = new Enhancer();
022: e.setSuperclass(clazz);
023: e.setCallback(callback);
024: return e.create();
025: } catch (Throwable e) {
026: e.printStackTrace();
027: throw new Error(e.getMessage());
028: }
029:
030: }
031:
032: /**
033: * @param args the command line arguments
034: */
035: public static void main(String[] args) {
036: List list = (List) newInstance(Vector.class);
037: Object value = "TEST";
038: list.add(value);
039: list.contains(value);
040: try {
041: list.set(2, "ArrayIndexOutOfBounds");
042: } catch (ArrayIndexOutOfBoundsException ignore) {
043:
044: }
045: list.add(value + "1");
046: list.add(value + "2");
047: list.toString();
048: list.equals(list);
049: list.set(0, null);
050: list.toString();
051: list.add(list);
052: list.get(1);
053: list.toArray();
054: list.remove(list);
055: list.remove("");
056: list.containsAll(list);
057: list.lastIndexOf(value);
058: }
059:
060: public Object intercept(Object obj,
061: java.lang.reflect.Method method, Object[] args,
062: MethodProxy proxy) throws Throwable {
063: printIdent(ident);
064: System.out.println(method);
065: for (int i = 0; i < args.length; i++) {
066: printIdent(ident);
067: System.out.print("arg" + (i + 1) + ": ");
068: if (obj == args[i])
069: System.out.println("this");
070: else
071: System.out.println(args[i]);
072: }
073: ident++;
074:
075: Object retValFromSuper = null;
076: try {
077: retValFromSuper = proxy.invokeSuper(obj, args);
078: ident--;
079: } catch (Throwable t) {
080: ident--;
081: printIdent(ident);
082: System.out.println("throw " + t);
083: System.out.println();
084: throw t.fillInStackTrace();
085: }
086:
087: printIdent(ident);
088: System.out.print("return ");
089: if (obj == retValFromSuper)
090: System.out.println("this");
091: else
092: System.out.println(retValFromSuper);
093:
094: if (ident == 1)
095: System.out.println();
096:
097: return retValFromSuper;
098: }
099:
100: void printIdent(int ident) {
101:
102: while (--ident > 0) {
103: System.out.print(".......");
104: }
105: System.out.print(" ");
106: }
107:
108: }
|