1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
package org.qedeq.base.test; |
17 |
|
|
18 |
|
import java.lang.reflect.InvocationHandler; |
19 |
|
import java.lang.reflect.InvocationTargetException; |
20 |
|
import java.lang.reflect.Method; |
21 |
|
import java.lang.reflect.Proxy; |
22 |
|
|
23 |
|
import org.qedeq.base.trace.Trace; |
24 |
|
import org.qedeq.base.utility.StringUtility; |
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
@author |
30 |
|
|
|
|
| 90.2% |
Uncovered Elements: 6 (61) |
Complexity: 16 |
Complexity Density: 0.39 |
|
31 |
|
public final class ObjectProxy implements InvocationHandler { |
32 |
|
|
33 |
|
|
34 |
|
private static final Class CLASS = ObjectProxy.class; |
35 |
|
|
36 |
|
private Object target; |
37 |
|
|
38 |
|
private static int level; |
39 |
|
|
40 |
|
private String history = ""; |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
@param |
46 |
|
@return |
47 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
48 |
18
|
public static Object createProxy(final Object obj) {... |
49 |
18
|
return createProxy(obj, null); |
50 |
|
} |
51 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
52 |
41492
|
private static Object createProxy(final Object obj, final Object parent) {... |
53 |
41492
|
if (obj == null) { |
54 |
278
|
return null; |
55 |
|
} |
56 |
41214
|
if (obj instanceof Proxy) { |
57 |
6
|
Trace.trace(CLASS, "createProxy", "object is already proxy!"); |
58 |
6
|
return obj; |
59 |
|
} |
60 |
41208
|
Trace.trace(CLASS, "createProxy", "instanciating"); |
61 |
41208
|
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass() |
62 |
|
.getInterfaces(), new ObjectProxy(obj, parent)); |
63 |
|
} |
64 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
65 |
41208
|
private ObjectProxy(final Object obj, final Object parent) {... |
66 |
41208
|
target = obj; |
67 |
41208
|
if (parent != null) { |
68 |
41196
|
Trace.trace(CLASS, "QedeqProxy(Object, Object)", parent.getClass().getName()); |
69 |
41196
|
history = ((ObjectProxy) Proxy.getInvocationHandler(parent)).history + "/" |
70 |
|
+ StringUtility.getClassName(obj.getClass()); |
71 |
|
} else { |
72 |
12
|
history = StringUtility.getClassName(obj.getClass()); |
73 |
|
} |
74 |
|
} |
75 |
|
|
|
|
| 84.2% |
Uncovered Elements: 6 (38) |
Complexity: 10 |
Complexity Density: 0.36 |
|
76 |
60786
|
public Object invoke(final Object proxy, final Method method, final Object[] args)... |
77 |
|
throws Throwable { |
78 |
60786
|
level++; |
79 |
60786
|
final StringBuffer buffer = new StringBuffer(); |
80 |
121572
|
for (int i = 0; i < level; i++) { |
81 |
60786
|
buffer.append("-"); |
82 |
|
} |
83 |
60786
|
Trace.trace(CLASS, "invoke", buffer.toString() + "> " + method.getName()); |
84 |
60786
|
Trace.trace(CLASS, "invoke", "> " + history); |
85 |
60786
|
Object result = null; |
86 |
60786
|
try { |
87 |
60786
|
Object[] proxyArgs = null; |
88 |
60786
|
if (args != null) { |
89 |
13881
|
proxyArgs = new Object[args.length]; |
90 |
27762
|
for (int i = 0; i < args.length; i++) { |
91 |
13881
|
if (method.getParameterTypes()[i].isPrimitive()) { |
92 |
13881
|
proxyArgs[i] = args[i]; |
93 |
|
} else { |
94 |
|
|
95 |
0
|
proxyArgs[i] = createProxy(args[i], proxy); |
96 |
|
} |
97 |
|
} |
98 |
|
} |
99 |
60786
|
result = method.invoke(target, proxyArgs); |
100 |
|
} catch (InvocationTargetException e) { |
101 |
0
|
Trace.trace(CLASS, "invoke", e); |
102 |
0
|
throw e.getCause(); |
103 |
|
} catch (Throwable e) { |
104 |
0
|
e.printStackTrace(); |
105 |
0
|
throw e; |
106 |
|
} finally { |
107 |
60786
|
Trace.trace(CLASS, "invoke", buffer.toString() + "< " + method.getName()); |
108 |
60786
|
level--; |
109 |
|
} |
110 |
60786
|
if (method.getReturnType().getName().startsWith("java.") |
111 |
|
|| method.getReturnType().isPrimitive() |
112 |
|
|| method.getReturnType().equals(String.class)) { |
113 |
19312
|
Trace.trace(CLASS, "invoke", "creating no proxy for " + method.getReturnType()); |
114 |
19312
|
Trace.trace(CLASS, "invoke", "result is: >" + result + "<"); |
115 |
19312
|
return result; |
116 |
|
|
117 |
|
} |
118 |
41474
|
Trace.trace(CLASS, "invoke", "creating proxy for " + method.getReturnType()); |
119 |
41474
|
return createProxy(result, proxy); |
120 |
|
} |
121 |
|
|
122 |
|
} |