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 |
|
|
|
|
| 94% |
Uncovered Elements: 4 (67) |
Complexity: 21 |
Complexity Density: 0.5 |
|
53 |
|
public class MethodArgumentsUberspector extends AbstractChainableUberspector implements RuntimeServicesAware |
54 |
|
{ |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
private ConverterManager converterManager; |
59 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
60 |
13 |
@Override... |
61 |
|
public void setRuntimeServices(RuntimeServices runtimeServices) |
62 |
|
{ |
63 |
13 |
ComponentManager componentManager = |
64 |
|
(ComponentManager) runtimeServices.getApplicationAttribute(ComponentManager.class.getName()); |
65 |
13 |
try { |
66 |
13 |
this.converterManager = componentManager.getInstance(ConverterManager.class); |
67 |
|
} catch (ComponentLookupException e) { |
68 |
0 |
this.log.warn("Failed to initialize " + this.getClass().getSimpleName(), e); |
69 |
|
} |
70 |
|
} |
71 |
|
|
|
|
| 90% |
Uncovered Elements: 3 (30) |
Complexity: 7 |
Complexity Density: 0.39 |
|
72 |
13 |
@Override... |
73 |
|
public VelMethod getMethod(Object obj, String methodName, Object[] args, Info i) throws Exception |
74 |
|
{ |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
13 |
VelMethod initialVelMethod = super.getMethod(obj, methodName, args, i); |
88 |
13 |
VelMethod velMethod = initialVelMethod; |
89 |
|
|
90 |
13 |
boolean shouldConvert = false; |
91 |
13 |
if (this.converterManager != null) { |
92 |
13 |
if (velMethod == null) { |
93 |
6 |
shouldConvert = true; |
94 |
|
} else { |
95 |
7 |
Method method = getPrivateMethod(velMethod); |
96 |
7 |
boolean sameParameterNumbers = method.getParameterTypes().length == args.length; |
97 |
7 |
if (!sameParameterNumbers) { |
98 |
4 |
shouldConvert = true; |
99 |
|
} |
100 |
|
} |
101 |
|
} |
102 |
|
|
103 |
13 |
if (shouldConvert) { |
104 |
|
|
105 |
10 |
Object[] convertedArguments = this.convertArguments(obj, methodName, args); |
106 |
10 |
if (convertedArguments != null) { |
107 |
8 |
velMethod = super.getMethod(obj, methodName, convertedArguments, i); |
108 |
8 |
if (velMethod != null) { |
109 |
8 |
velMethod = new ConvertingVelMethod(velMethod); |
110 |
|
} else { |
111 |
0 |
velMethod = initialVelMethod; |
112 |
|
} |
113 |
|
} |
114 |
|
} |
115 |
|
|
116 |
13 |
return velMethod; |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
122 |
7 |
private Method getPrivateMethod(VelMethod velMethod) throws Exception... |
123 |
|
{ |
124 |
7 |
Field methodField = velMethod.getClass().getDeclaredField("method"); |
125 |
7 |
boolean isAccessible = methodField.isAccessible(); |
126 |
7 |
try { |
127 |
7 |
methodField.setAccessible(true); |
128 |
7 |
return (Method) methodField.get(velMethod); |
129 |
|
} finally { |
130 |
7 |
methodField.setAccessible(isAccessible); |
131 |
|
} |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
@param |
139 |
|
@param |
140 |
|
@param |
141 |
|
@return |
142 |
|
|
143 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 5 |
Complexity Density: 1 |
|
144 |
18 |
private Object[] convertArguments(Object obj, String methodName, Object[] args)... |
145 |
|
{ |
146 |
18 |
for (Method method : obj.getClass().getMethods()) { |
147 |
68 |
if (method.getName().equalsIgnoreCase(methodName) |
148 |
|
&& (method.getGenericParameterTypes().length == args.length || method.isVarArgs())) { |
149 |
16 |
try { |
150 |
16 |
return convertArguments(args, method.getGenericParameterTypes(), method.isVarArgs()); |
151 |
|
} catch (Exception e) { |
152 |
|
|
153 |
|
} |
154 |
|
} |
155 |
|
} |
156 |
|
|
157 |
2 |
return null; |
158 |
|
} |
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
@param |
166 |
|
@param |
167 |
|
@param |
168 |
|
@return |
169 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 6 |
Complexity Density: 0.67 |
|
170 |
16 |
private Object[] convertArguments(Object[] arguments, Type[] parameterTypes, boolean isVarArgs)... |
171 |
|
{ |
172 |
16 |
Object[] convertedArguments = Arrays.copyOf(arguments, arguments.length); |
173 |
46 |
for (int i = 0; i < arguments.length; i++) { |
174 |
|
|
175 |
|
|
176 |
30 |
Type expectedType; |
177 |
30 |
if (isVarArgs && i >= parameterTypes.length - 1) { |
178 |
14 |
expectedType = ((Class<?>) parameterTypes[parameterTypes.length - 1]).getComponentType(); |
179 |
|
} else { |
180 |
16 |
expectedType = parameterTypes[i]; |
181 |
|
} |
182 |
|
|
183 |
30 |
if (arguments[i] != null && !TypeUtils.isInstance(arguments[i], expectedType)) { |
184 |
14 |
convertedArguments[i] = this.converterManager.convert(expectedType, arguments[i]); |
185 |
|
} |
186 |
|
} |
187 |
|
|
188 |
16 |
return convertedArguments; |
189 |
|
} |
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
@version |
195 |
|
|
|
|
| 40% |
Uncovered Elements: 6 (10) |
Complexity: 5 |
Complexity Density: 1 |
|
196 |
|
private class ConvertingVelMethod implements VelMethod |
197 |
|
{ |
198 |
|
|
199 |
|
private VelMethod innerMethod; |
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
@param |
205 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
206 |
8 |
ConvertingVelMethod(VelMethod realMethod)... |
207 |
|
{ |
208 |
8 |
this.innerMethod = realMethod; |
209 |
|
} |
210 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
211 |
8 |
@Override... |
212 |
|
public Object invoke(Object o, Object[] params) throws Exception |
213 |
|
{ |
214 |
8 |
return this.innerMethod.invoke(o, convertArguments(o, this.innerMethod.getMethodName(), params)); |
215 |
|
} |
216 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
217 |
0 |
@Override... |
218 |
|
public boolean isCacheable() |
219 |
|
{ |
220 |
0 |
return this.innerMethod.isCacheable(); |
221 |
|
} |
222 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
223 |
0 |
@Override... |
224 |
|
public String getMethodName() |
225 |
|
{ |
226 |
0 |
return this.innerMethod.getMethodName(); |
227 |
|
} |
228 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
229 |
0 |
@Override... |
230 |
|
public Class<?> getReturnType() |
231 |
|
{ |
232 |
0 |
return this.innerMethod.getReturnType(); |
233 |
|
} |
234 |
|
} |
235 |
|
} |