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