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