-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimpleDynamicProxy.java
More file actions
68 lines (56 loc) · 1.8 KB
/
SimpleDynamicProxy.java
File metadata and controls
68 lines (56 loc) · 1.8 KB
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
package typeinfo;
import java.lang.reflect.*;
/**
* RUN:
* javac typeinfo/SimpleDynamicProxy.java && java typeinfo.SimpleDynamicProxy
* OUTPUT:
* doSomething
* somethingElse bonobo
*
* **** proxy: class typeinfo.$Proxy0,
* method: public abstract void typeinfo.Interface.doSomething(),
* args: null
* doSomething
*
* **** proxy: class typeinfo.$Proxy0,
* method: public abstract void typeinfo.Interface.somethingElse(java.lang.String),
* args: [Ljava.lang.Object;@15a3d6b
* bonobo
* somethingElse bonobo
*/
public class SimpleDynamicProxy {
public static void consumer(Interface iface) {
iface.doSomething();
iface.somethingElse("bonobo");
}
public static void main(String[] args) {
RealObject real = new RealObject();
consumer(real);
Interface proxy = (Interface)Proxy.newProxyInstance(
Interface.class.getClassLoader()
, new Class[]{ Interface.class }
, new DynamicProxyHandler(real)
);
consumer(proxy);
}
}
class DynamicProxyHandler implements InvocationHandler {
private Object proxied;
public DynamicProxyHandler(Object proxied) {
this.proxied = proxied;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.printf(
"\n**** proxy: " + proxy.getClass()
+ ",\n method: " + method
+ ",\n args: " + args
+ "\n"
);
if (args != null) {
for (Object arg : args) {
System.out.printf("%17s\n", arg);
}
}
return method.invoke(proxied, args);
}
}