forked from JustinSDK/JavaSE6Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogHandler.java
More file actions
38 lines (34 loc) · 1.24 KB
/
LogHandler.java
File metadata and controls
38 lines (34 loc) · 1.24 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
package onlyfun.caterpillar;
import java.util.logging.*;
import java.lang.reflect.*;
public class LogHandler implements InvocationHandler {
private Logger logger =
Logger.getLogger(this.getClass().getName());
private Object delegate;
// 綁定要代理的物件
public Object bind(Object delegate) {
this.delegate = delegate;
// 建立並傳回代理物件
return Proxy.newProxyInstance(
delegate.getClass().getClassLoader(),
// 要被代理的介面
delegate.getClass().getInterfaces(),
this);
}
// 代理要呼叫的方法,並在其前後增加行為
public Object invoke(Object proxy,
Method method,
Object[] args) throws Throwable {
Object result = null;
try {
logger.log(Level.INFO,
"method starts..." + method.getName());
result = method.invoke(delegate, args);
logger.log(Level.INFO,
"method ends..." + method.getName());
} catch (Exception e){
logger.log(Level.INFO, e.toString());
}
return result;
}
}