1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.velocity.introspection; |
21 |
|
|
22 |
|
import java.lang.reflect.Field; |
23 |
|
import java.lang.reflect.Method; |
24 |
|
import java.lang.reflect.Type; |
25 |
|
import java.util.Arrays; |
26 |
|
|
27 |
|
import org.apache.commons.lang3.reflect.TypeUtils; |
28 |
|
import org.apache.velocity.runtime.RuntimeServices; |
29 |
|
import org.apache.velocity.util.RuntimeServicesAware; |
30 |
|
import org.apache.velocity.util.introspection.AbstractChainableUberspector; |
31 |
|
import org.apache.velocity.util.introspection.Info; |
32 |
|
import org.apache.velocity.util.introspection.VelMethod; |
33 |
|
import org.xwiki.component.manager.ComponentLookupException; |
34 |
|
import org.xwiki.component.manager.ComponentManager; |
35 |
|
import org.xwiki.properties.ConverterManager; |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
@since |
51 |
|
@version |
52 |
|
@see |
53 |
|
|
|
|
| 97% |
Uncovered Elements: 2 (67) |
Complexity: 21 |
Complexity Density: 0.5 |
|
54 |
|
public class MethodArgumentsUberspector extends AbstractChainableUberspector implements RuntimeServicesAware |
55 |
|
{ |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
private ConverterManager converterManager; |
60 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
61 |
85 |
@Override... |
62 |
|
public void setRuntimeServices(RuntimeServices runtimeServices) |
63 |
|
{ |
64 |
85 |
ComponentManager componentManager = |
65 |
|
(ComponentManager) runtimeServices.getApplicationAttribute(ComponentManager.class.getName()); |
66 |
85 |
try { |
67 |
85 |
this.converterManager = componentManager.getInstance(ConverterManager.class); |
68 |
|
} catch (ComponentLookupException e) { |
69 |
0 |
this.log.warn("Failed to initialize " + this.getClass().getSimpleName(), e); |
70 |
|
} |
71 |
|
} |
72 |
|
|
|
|
| 96.7% |
Uncovered Elements: 1 (30) |
Complexity: 7 |
Complexity Density: 0.39 |
|
73 |
442896 |
@Override... |
74 |
|
public VelMethod getMethod(Object obj, String methodName, Object[] args, Info i) throws Exception |
75 |
|
{ |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
442893 |
VelMethod initialVelMethod = super.getMethod(obj, methodName, args, i); |
89 |
442892 |
VelMethod velMethod = initialVelMethod; |
90 |
|
|
91 |
442892 |
boolean shouldConvert = false; |
92 |
442895 |
if (this.converterManager != null) { |
93 |
442889 |
if (velMethod == null) { |
94 |
54082 |
shouldConvert = true; |
95 |
|
} else { |
96 |
388807 |
Method method = getPrivateMethod(velMethod); |
97 |
388796 |
boolean sameParameterNumbers = method.getParameterTypes().length == args.length; |
98 |
388801 |
if (!sameParameterNumbers) { |
99 |
21563 |
shouldConvert = true; |
100 |
|
} |
101 |
|
} |
102 |
|
} |
103 |
|
|
104 |
442874 |
if (shouldConvert) { |
105 |
|
|
106 |
75644 |
Object[] convertedArguments = this.convertArguments(obj, methodName, args); |
107 |
75648 |
if (convertedArguments != null) { |
108 |
67775 |
velMethod = super.getMethod(obj, methodName, convertedArguments, i); |
109 |
67776 |
if (velMethod != null) { |
110 |
67766 |
velMethod = new ConvertingVelMethod(velMethod); |
111 |
|
} else { |
112 |
11 |
velMethod = initialVelMethod; |
113 |
|
} |
114 |
|
} |
115 |
|
} |
116 |
|
|
117 |
442876 |
return velMethod; |
118 |
|
} |
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
123 |
388799 |
private Method getPrivateMethod(VelMethod velMethod) throws Exception... |
124 |
|
{ |
125 |
388801 |
Field methodField = velMethod.getClass().getDeclaredField("method"); |
126 |
388791 |
boolean isAccessible = methodField.isAccessible(); |
127 |
388792 |
try { |
128 |
388792 |
methodField.setAccessible(true); |
129 |
388792 |
return (Method) methodField.get(velMethod); |
130 |
|
} finally { |
131 |
388797 |
methodField.setAccessible(isAccessible); |
132 |
|
} |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
@param |
140 |
|
@param |
141 |
|
@param |
142 |
|
@return |
143 |
|
|
144 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 5 |
Complexity Density: 1 |
|
145 |
202174 |
private Object[] convertArguments(Object obj, String methodName, Object[] args)... |
146 |
|
{ |
147 |
202174 |
for (Method method : obj.getClass().getMethods()) { |
148 |
1898784 |
if (method.getName().equalsIgnoreCase(methodName) |
149 |
|
&& (method.getGenericParameterTypes().length == args.length || method.isVarArgs())) { |
150 |
194306 |
try { |
151 |
194309 |
return convertArguments(args, method.getGenericParameterTypes(), method.isVarArgs()); |
152 |
|
} catch (Exception e) { |
153 |
|
|
154 |
|
} |
155 |
|
} |
156 |
|
} |
157 |
|
|
158 |
7872 |
return null; |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
@param |
167 |
|
@param |
168 |
|
@param |
169 |
|
@return |
170 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 6 |
Complexity Density: 0.67 |
|
171 |
194301 |
private Object[] convertArguments(Object[] arguments, Type[] parameterTypes, boolean isVarArgs)... |
172 |
|
{ |
173 |
194300 |
Object[] convertedArguments = Arrays.copyOf(arguments, arguments.length); |
174 |
483847 |
for (int i = 0; i < arguments.length; i++) { |
175 |
|
|
176 |
|
|
177 |
289539 |
Type expectedType; |
178 |
289537 |
if (isVarArgs && i >= parameterTypes.length - 1) { |
179 |
19884 |
expectedType = ((Class<?>) parameterTypes[parameterTypes.length - 1]).getComponentType(); |
180 |
|
} else { |
181 |
269653 |
expectedType = parameterTypes[i]; |
182 |
|
} |
183 |
|
|
184 |
289534 |
if (arguments[i] != null && !TypeUtils.isInstance(arguments[i], expectedType)) { |
185 |
138515 |
convertedArguments[i] = this.converterManager.convert(expectedType, arguments[i]); |
186 |
|
} |
187 |
|
} |
188 |
|
|
189 |
194307 |
return convertedArguments; |
190 |
|
} |
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
@version |
196 |
|
|
|
|
| 60% |
Uncovered Elements: 4 (10) |
Complexity: 5 |
Complexity Density: 1 |
|
197 |
|
private class ConvertingVelMethod implements VelMethod |
198 |
|
{ |
199 |
|
|
200 |
|
private VelMethod innerMethod; |
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
@param |
206 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
207 |
67763 |
ConvertingVelMethod(VelMethod realMethod)... |
208 |
|
{ |
209 |
67765 |
this.innerMethod = realMethod; |
210 |
|
} |
211 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
212 |
126530 |
@Override... |
213 |
|
public Object invoke(Object o, Object[] params) throws Exception |
214 |
|
{ |
215 |
126533 |
return this.innerMethod.invoke(o, convertArguments(o, this.innerMethod.getMethodName(), params)); |
216 |
|
} |
217 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
218 |
0 |
@Override... |
219 |
|
public boolean isCacheable() |
220 |
|
{ |
221 |
0 |
return this.innerMethod.isCacheable(); |
222 |
|
} |
223 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
224 |
0 |
@Override... |
225 |
|
public String getMethodName() |
226 |
|
{ |
227 |
0 |
return this.innerMethod.getMethodName(); |
228 |
|
} |
229 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
230 |
18 |
@Override... |
231 |
|
public Class<?> getReturnType() |
232 |
|
{ |
233 |
18 |
return this.innerMethod.getReturnType(); |
234 |
|
} |
235 |
|
} |
236 |
|
} |