CGLIB 例子 Trace Class 方法信息

以下的示例代码将打印代理目标Class的方法信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.cglib;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Vector;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
/**
*
* @author gang
*
*/
public class Trace implements MethodInterceptor {

int ident = 1;
static Trace callback = new Trace();

/** Creates a new instance of Trace */
private Trace() {
}
/**
* 创建代理对象
* @param clazz
* @return
*/
public static Object newInstance( Class clazz ){
try{
Enhancer e = new Enhancer();
/**
* 设置代理的目标Class
*/
e.setSuperclass(clazz);
/**
* 设置拦截器
*/
e.setCallback(callback);
return e.create();
}catch( Throwable e ){
e.printStackTrace();
throw new Error(e.getMessage());
}

}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
List list = (List)newInstance(Vector.class);
Object value = "TEST";
list.add(value);
list.contains(value);
try{
list.set(2, "ArrayIndexOutOfBounds" );
}catch( ArrayIndexOutOfBoundsException ignore ){

}
list.add(value + "1");
list.add(value + "2");
list.toString();
list.equals(list);
list.set( 0, null );
list.toString();
list.add(list);
list.get(1);
list.toArray();
list.remove(list);
list.remove("");
list.containsAll(list);
list.lastIndexOf(value);
}
/**
*
*/
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
printIdent(ident);
System.out.println( method );
for( int i = 0; i < args.length; i++ ){
printIdent(ident);
System.out.print( "arg" + (i + 1) + ": ");
if( obj == args[i])
System.out.println("this");
else
System.out.println(args[i]);
}
ident++;

Object retValFromSuper = null;
try {
/**
* obj 为生成的代理对象,调用其父类的方法,CGLIB的是通过继承生成子类实现的,
* 所以需要调用其父类方法
*/
retValFromSuper = proxy.invokeSuper(obj, args);
ident--;
} catch (Throwable t) {
ident--;
printIdent(ident);
System.out.println("throw " + t );
System.out.println();
throw t.fillInStackTrace();
}

printIdent(ident);
System.out.print("return " );
if( obj == retValFromSuper)
System.out.println("this");
else System.out.println(retValFromSuper);

if(ident == 1)
System.out.println();

return retValFromSuper;
}

void printIdent( int ident ){


while( --ident > 0 ){
System.out.print(".......");
}
System.out.print(" ");
}

}

打印结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  public synchronized boolean java.util.Vector.add(java.lang.Object)
arg1: TEST
return true

public boolean java.util.Vector.contains(java.lang.Object)
arg1: TEST
....... public synchronized int java.util.Vector.indexOf(java.lang.Object,int)
....... arg1: TEST
....... arg2: 0
....... return 0
return true

public synchronized java.lang.Object java.util.Vector.set(int,java.lang.Object)
arg1: 2
arg2: ArrayIndexOutOfBounds
throw java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 2

public synchronized boolean java.util.Vector.add(java.lang.Object)
arg1: TEST1
return true

public synchronized boolean java.util.Vector.add(java.lang.Object)
arg1: TEST2
return true

public synchronized java.lang.String java.util.Vector.toString()
....... public synchronized java.util.Iterator java.util.Vector.iterator()
....... return java.util.Vector$Itr@deb6432
return [TEST, TEST1, TEST2]

public synchronized boolean java.util.Vector.equals(java.lang.Object)
arg1: this
return true

public synchronized java.lang.Object java.util.Vector.set(int,java.lang.Object)
arg1: 0
arg2: null
return TEST

public synchronized java.lang.String java.util.Vector.toString()
....... public synchronized java.util.Iterator java.util.Vector.iterator()
....... return java.util.Vector$Itr@28ba21f3
return [null, TEST1, TEST2]

public synchronized boolean java.util.Vector.add(java.lang.Object)
arg1: this
return true

public synchronized java.lang.Object java.util.Vector.get(int)
arg1: 1
return TEST1

public synchronized java.lang.Object[] java.util.Vector.toArray()
return [Ljava.lang.Object;@694f9431

public boolean java.util.Vector.remove(java.lang.Object)
arg1: this
....... public synchronized boolean java.util.Vector.removeElement(java.lang.Object)
....... arg1: this
.............. public int java.util.Vector.indexOf(java.lang.Object)
.............. arg1: this
..................... public synchronized int java.util.Vector.indexOf(java.lang.Object,int)
..................... arg1: this
..................... arg2: 0
............................ public synchronized boolean java.util.Vector.equals(java.lang.Object)
............................ arg1: null
............................ return false
............................ public synchronized boolean java.util.Vector.equals(java.lang.Object)
............................ arg1: TEST1
............................ return false
............................ public synchronized boolean java.util.Vector.equals(java.lang.Object)
............................ arg1: TEST2
............................ return false
............................ public synchronized boolean java.util.Vector.equals(java.lang.Object)
............................ arg1: this
............................ return true
..................... return 3
.............. return 3
.............. public synchronized void java.util.Vector.removeElementAt(int)
.............. arg1: 3
.............. return null
....... return true
return true

public boolean java.util.Vector.remove(java.lang.Object)
arg1:
....... public synchronized boolean java.util.Vector.removeElement(java.lang.Object)
....... arg1:
.............. public int java.util.Vector.indexOf(java.lang.Object)
.............. arg1:
..................... public synchronized int java.util.Vector.indexOf(java.lang.Object,int)
..................... arg1:
..................... arg2: 0
..................... return -1
.............. return -1
....... return false
return false

public synchronized boolean java.util.Vector.containsAll(java.util.Collection)
arg1: this
....... public synchronized java.util.Iterator java.util.Vector.iterator()
....... return java.util.Vector$Itr@f2a0b8e
....... public boolean java.util.Vector.contains(java.lang.Object)
....... arg1: null
.............. public synchronized int java.util.Vector.indexOf(java.lang.Object,int)
.............. arg1: null
.............. arg2: 0
.............. return 0
....... return true
....... public boolean java.util.Vector.contains(java.lang.Object)
....... arg1: TEST1
.............. public synchronized int java.util.Vector.indexOf(java.lang.Object,int)
.............. arg1: TEST1
.............. arg2: 0
.............. return 1
....... return true
....... public boolean java.util.Vector.contains(java.lang.Object)
....... arg1: TEST2
.............. public synchronized int java.util.Vector.indexOf(java.lang.Object,int)
.............. arg1: TEST2
.............. arg2: 0
.............. return 2
....... return true
return true

public synchronized int java.util.Vector.lastIndexOf(java.lang.Object)
arg1: TEST
....... public synchronized int java.util.Vector.lastIndexOf(java.lang.Object,int)
....... arg1: TEST
....... arg2: 2
....... return -1
return -1