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.InvocationTargetException; |
19 |
|
import java.lang.reflect.Method; |
20 |
|
|
21 |
|
import org.qedeq.base.trace.Trace; |
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
@author |
28 |
|
|
|
|
| 0% |
Uncovered Elements: 56 (56) |
Complexity: 11 |
Complexity Density: 0.3 |
|
29 |
|
public final class DynamicGetter { |
30 |
|
|
31 |
|
|
32 |
|
private static final Class CLASS = DynamicGetter.class; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
37 |
0
|
private DynamicGetter() {... |
38 |
|
|
39 |
|
} |
40 |
|
|
|
|
| 0% |
Uncovered Elements: 31 (31) |
Complexity: 6 |
Complexity Density: 0.29 |
|
41 |
0
|
private static final Object getMethodResult(final Object obj, final String methodParam)... |
42 |
|
throws IllegalAccessException, InvocationTargetException { |
43 |
0
|
String methodName = methodParam; |
44 |
0
|
Object[] param; |
45 |
0
|
int pos = methodName.indexOf("("); |
46 |
0
|
if (pos >= 0) { |
47 |
0
|
methodName = methodName.substring(0, pos); |
48 |
0
|
if (')' == methodParam.charAt(pos + 1)) { |
49 |
0
|
param = new Object[0]; |
50 |
|
} else { |
51 |
0
|
param = new Object[1]; |
52 |
0
|
param[0] = new Integer(Integer.parseInt(methodParam.substring(pos + 1, |
53 |
|
methodParam.length() - 1))); |
54 |
|
} |
55 |
|
} else { |
56 |
0
|
param = new Object[0]; |
57 |
|
} |
58 |
|
|
59 |
0
|
if (obj == null) { |
60 |
0
|
Trace.trace(DynamicGetter.class, "getMethodResult(Object)", "obj = null!"); |
61 |
0
|
throw new RuntimeException("trying to execute method on null object: \"" + methodName + "\""); |
62 |
|
} |
63 |
0
|
Method[] method = obj.getClass().getMethods(); |
64 |
0
|
for (int i = 0; i < method.length; i++) { |
65 |
0
|
if (method[i].getName().equals(methodName)) { |
66 |
0
|
Trace.trace(CLASS, "getMethodResult(Object)", "invoking:", |
67 |
|
method[i].getName()); |
68 |
0
|
return method[i].invoke(obj, param); |
69 |
|
} |
70 |
|
} |
71 |
|
|
72 |
0
|
System.out.println(obj.getClass()); |
73 |
0
|
System.out.println(obj); |
74 |
0
|
throw new RuntimeException("method not found: " + methodName); |
75 |
|
} |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
@param |
81 |
|
@param |
82 |
|
@return |
83 |
|
@throws |
84 |
|
@throws |
85 |
|
|
|
|
| 0% |
Uncovered Elements: 22 (22) |
Complexity: 4 |
Complexity Density: 0.25 |
|
86 |
0
|
public static final Object get(final Object object, final String methodParam)... |
87 |
|
throws IllegalAccessException, InvocationTargetException { |
88 |
0
|
Object result = object; |
89 |
0
|
int posOld = 0; |
90 |
0
|
int pos = 0; |
91 |
|
|
92 |
0
|
if (methodParam.length() == 0) { |
93 |
0
|
return result; |
94 |
|
} |
95 |
0
|
Trace.param(CLASS, "get(Object, String)", "methodParam", methodParam); |
96 |
|
|
97 |
0
|
while (0 <= (pos = methodParam.indexOf(".", posOld))) { |
98 |
0
|
String method = methodParam.substring(posOld, pos); |
99 |
0
|
posOld = pos + 1; |
100 |
0
|
Trace.param(CLASS, "get(Object, String)", "method", method); |
101 |
|
|
102 |
0
|
Object result2 = getMethodResult(result, method); |
103 |
0
|
if (result2 == null) { |
104 |
0
|
Trace.trace(CLASS, "get(Object, String)", "result = null"); |
105 |
|
} |
106 |
0
|
result = result2; |
107 |
|
} |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
0
|
Trace.param(CLASS, "get(Object, String)", "method", |
113 |
|
methodParam.substring(posOld)); |
114 |
0
|
return getMethodResult(result, methodParam.substring(posOld)); |
115 |
|
} |
116 |
|
} |