1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package triptracker.testing;
21
22 import java.lang.reflect.InvocationHandler;
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25 import java.lang.reflect.Proxy;
26
27 public class DebugProxy implements InvocationHandler {
28
29 private Object obj;
30
31 public static Object newInstance(Object obj) {
32 return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj
33 .getClass().getInterfaces(), new DebugProxy(obj));
34 }
35
36 private DebugProxy(Object obj) {
37 this.obj = obj;
38 }
39
40 public Object invoke(Object proxy, Method m, Object[] args)
41 throws Throwable {
42 Object result;
43 try {
44 System.out.println("before method " + m.getName());
45 result = m.invoke(obj, args);
46 } catch (InvocationTargetException e) {
47 throw e.getTargetException();
48 } catch (Exception e) {
49 throw new RuntimeException("unexpected invocation exception: "
50 + e.getMessage());
51 } finally {
52 System.out.println("after method " + m.getName());
53 }
54 return result;
55 }
56 }