1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package triptracker.testing.compress;
21
22 import java.lang.reflect.*;
23 import java.util.*;
24
25 /***
26 * Generic debug proxy class that logs every method call.
27 *
28 * @author Jan Magne Tjensvold {@code <janmagne at broadpark dot no>}
29 */
30 public class DebugProxy implements InvocationHandler {
31 protected final Object obj;
32 protected final List<Method> methods;
33 protected final List<Method> history;
34
35 protected static boolean debugEnabled = true;
36
37 /***
38 * Creation of new proxies can only be made by the
39 * <code>proxyFor(Object)</code> method since the constructor's visibility
40 * is private.
41 *
42 * @param obj points to the object to create a proxy for.
43 */
44 protected DebugProxy(Object obj) {
45 this.obj = obj;
46 methods = new ArrayList<Method>();
47 history = Collections.unmodifiableList(methods);
48 }
49
50 /***
51 * Creates a logging proxy object from the specified object.
52 *
53 * @param obj points to the object to create a proxy for.
54 * @return proxy object of this class that logs method calls.
55 */
56 public static synchronized Object proxyFor(Object obj) {
57 Class objClass = obj.getClass();
58
59
60 Object proxyObj = Proxy.newProxyInstance(objClass.getClassLoader(),
61 objClass.getInterfaces(), new DebugProxy(obj));
62 return proxyObj;
63 }
64
65 /***
66 * This method is called everytime a call is made to any method on the proxy
67 * instance.
68 *
69 * @param proxy is a reference to the proxy object.
70 * @param method points to a method object describing the method being
71 * called.
72 * @param args is the arguments for the method call.
73 * @return the actual return value from the method call.
74 * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
75 * java.lang.reflect.Method, java.lang.Object[])
76 */
77 public Object invoke(Object proxy, Method method, Object[] args)
78 throws Throwable {
79 if (debugEnabled)
80 methods.add(method);
81
82
83 try {
84 return method.invoke(obj, args);
85 } catch (InvocationTargetException e) {
86 throw e.getTargetException();
87 }
88 }
89
90 /***
91 * Returns the method call history as a list of strings.
92 *
93 * @return method call history.
94 */
95 public List<Method> getHistory() {
96 return history;
97 }
98
99 /***
100 * Clear the method call history.
101 */
102 public void clearHistory() {
103 methods.clear();
104 }
105
106 /***
107 * Get the enabled state of the debug proxy class.
108 *
109 * <p>If a debug proxy class is disabled its debug specific states shall not
110 * be modified when method invocations pass through the
111 * <code>invoke(Object, Method, Object[])</code> method and it should not
112 * generate any debug specific output.
113 *
114 * @return enabled state of the debug proxy.
115 */
116 public static boolean isDebugEnabled() {
117 return debugEnabled;
118 }
119
120 /***
121 * Set the enabled state of the debug proxy class.
122 *
123 * <p>If a debug proxy class is disabled its debug specific states shall not
124 * be modified when method invocations pass through the
125 * <code>invoke(Object, Method, Object[])</code> method and it should not
126 * generate any debug specific output.
127 *
128 * @param state is the enabled state of the debug proxy.
129 */
130 public static void setDebugEnabled(boolean state) {
131 debugEnabled = state;
132 }
133 }