-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJDKDynamicProxy.java
61 lines (42 loc) · 1.31 KB
/
JDKDynamicProxy.java
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
package br.com.luzaraldi;
//luzaraldi.medium.com/
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
public class JDKDynamicProxy {
public static void main(String[] args) {
}
}
interface GreetingService {
String greet(String name);
}
class EnglishGreetingService implements GreetingService {
@Override
public String greet(String name) {
return "Hello, " + name + "!";
}
}
class LogginInvocationHandler implements InvocationHandler {
private final Object target;
public LoggingInvocationHandler(Object taget) {
this.taget = target;
}
}
@Override
public Object invoke()
System.out.println("Methon " + method.getName() + "is called with arguments + (
args != null ? Arrays.toString(args) : "null"
));
Object result = method.invoke(target, args);
System.out.println(
"Method " + method.getName() + " return " + result;
);
return result;
}
class proxyFactoryJDK {
public static <T> T createProxy(T target, Class<T> interfaceType) {
return (T) Proxy.newProxyInstance(
interfaceType.getClasssLoader(),
new Class<?>[] (interfaceType),
new LogginInvocationHandler(target));
}
}