1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.enhance; |
16 |
|
|
17 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
18 |
|
import org.apache.hivemind.Location; |
19 |
|
import org.apache.hivemind.service.ClassFabUtils; |
20 |
|
import org.apache.hivemind.service.MethodSignature; |
21 |
|
import org.apache.hivemind.util.Defense; |
22 |
|
import org.apache.tapestry.IBinding; |
23 |
|
import org.apache.tapestry.IRequestCycle; |
24 |
|
import org.apache.tapestry.engine.IPageLoader; |
25 |
|
import org.apache.tapestry.event.PageEvent; |
26 |
|
import org.apache.tapestry.spec.IComponentSpecification; |
27 |
|
|
28 |
|
import java.lang.reflect.Modifier; |
29 |
|
import java.util.HashMap; |
30 |
|
import java.util.Map; |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
public final class EnhanceUtils |
39 |
|
{ |
40 |
1 |
public static final MethodSignature FINISH_LOAD_SIGNATURE = new MethodSignature(void.class, |
41 |
|
"finishLoad", new Class[] |
42 |
14 |
{ IRequestCycle.class, IPageLoader.class, IComponentSpecification.class }, null); |
43 |
|
|
44 |
1 |
public static final MethodSignature PAGE_DETACHED_SIGNATURE = new MethodSignature(void.class, |
45 |
|
"pageDetached", new Class[] |
46 |
|
{ PageEvent.class }, null); |
47 |
|
|
48 |
1 |
public static final MethodSignature CLEANUP_AFTER_RENDER_SIGNATURE = new MethodSignature( |
49 |
|
void.class, "cleanupAfterRender", new Class[] |
50 |
|
{ IRequestCycle.class }, null); |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
1 |
private static Map _unwrappers = new HashMap(); |
59 |
|
|
60 |
|
static |
61 |
|
{ |
62 |
1 |
_unwrappers.put(boolean.class, "toBoolean"); |
63 |
1 |
_unwrappers.put(byte.class, "toByte"); |
64 |
1 |
_unwrappers.put(char.class, "toChar"); |
65 |
1 |
_unwrappers.put(short.class, "toShort"); |
66 |
1 |
_unwrappers.put(int.class, "toInt"); |
67 |
1 |
_unwrappers.put(long.class, "toLong"); |
68 |
1 |
_unwrappers.put(float.class, "toFloat"); |
69 |
1 |
_unwrappers.put(double.class, "toDouble"); |
70 |
1 |
} |
71 |
|
|
72 |
|
|
73 |
0 |
private EnhanceUtils() { } |
74 |
|
|
75 |
|
public static String createMutatorMethodName(String propertyName) |
76 |
|
{ |
77 |
1141 |
return "set" + upcase(propertyName); |
78 |
|
} |
79 |
|
|
80 |
|
public static String createAccessorMethodName(String propertyName) |
81 |
|
{ |
82 |
69 |
return "get" + upcase(propertyName); |
83 |
|
} |
84 |
|
|
85 |
|
private static String upcase(String name) |
86 |
|
{ |
87 |
1210 |
return name.substring(0, 1).toUpperCase() + name.substring(1); |
88 |
|
} |
89 |
|
|
90 |
|
public static void createSimpleAccessor(EnhancementOperation op, String fieldName, |
91 |
|
String propertyName, Class propertyType, Location location) |
92 |
|
{ |
93 |
1253 |
String methodName = op.getAccessorMethodName(propertyName); |
94 |
|
|
95 |
1253 |
op.addMethod( Modifier.PUBLIC, new MethodSignature(propertyType, methodName, null, null), |
96 |
|
"return " + fieldName + ";", location); |
97 |
1253 |
} |
98 |
|
|
99 |
|
public static void createSimpleMutator(EnhancementOperation op, String fieldName, |
100 |
|
String propertyName, Class propertyType, Location location) |
101 |
|
{ |
102 |
1135 |
String methodName = createMutatorMethodName(propertyName); |
103 |
|
|
104 |
1135 |
op.addMethod(Modifier.PUBLIC, new MethodSignature(void.class, methodName, |
105 |
|
new Class[] { propertyType }, null), fieldName + " = $1;", location); |
106 |
1135 |
} |
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
public static Class extractPropertyType(EnhancementOperation op, String propertyName, String definedTypeName) |
126 |
|
{ |
127 |
193 |
return extractPropertyType(op, propertyName, definedTypeName, false); |
128 |
|
} |
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
public static Class extractPropertyType(EnhancementOperation op, String propertyName, |
148 |
|
String definedTypeName, boolean isGeneric) |
149 |
|
{ |
150 |
197 |
Defense.notNull(op, "op"); |
151 |
197 |
Defense.notNull(propertyName, "propertyName"); |
152 |
|
|
153 |
197 |
if (definedTypeName != null) |
154 |
|
{ |
155 |
6 |
Class propertyType = op.convertTypeName(definedTypeName); |
156 |
|
|
157 |
4 |
if (!isGeneric) |
158 |
4 |
op.validateProperty(propertyName, propertyType); |
159 |
|
|
160 |
4 |
return propertyType; |
161 |
|
} |
162 |
|
|
163 |
191 |
Class propertyType = op.getPropertyType(propertyName); |
164 |
|
|
165 |
191 |
return propertyType == null ? Object.class : propertyType; |
166 |
|
} |
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
public static boolean toBoolean(IBinding binding) |
172 |
|
{ |
173 |
2 |
Boolean wrapped = (Boolean) binding.getObject(Boolean.class); |
174 |
|
|
175 |
2 |
return wrapped == null ? false : wrapped.booleanValue(); |
176 |
|
} |
177 |
|
|
178 |
|
public static byte toByte(IBinding binding) |
179 |
|
{ |
180 |
2 |
Byte wrapped = (Byte) binding.getObject(Byte.class); |
181 |
|
|
182 |
2 |
return wrapped == null ? 0 : wrapped.byteValue(); |
183 |
|
} |
184 |
|
|
185 |
|
public static char toChar(IBinding binding) |
186 |
|
{ |
187 |
2 |
Character wrapped = (Character) binding.getObject(Character.class); |
188 |
|
|
189 |
2 |
return wrapped == null ? 0 : wrapped.charValue(); |
190 |
|
} |
191 |
|
|
192 |
|
public static short toShort(IBinding binding) |
193 |
|
{ |
194 |
2 |
Short wrapped = (Short) binding.getObject(Short.class); |
195 |
|
|
196 |
2 |
return wrapped == null ? 0 : wrapped.shortValue(); |
197 |
|
} |
198 |
|
|
199 |
|
public static int toInt(IBinding binding) |
200 |
|
{ |
201 |
2 |
Integer wrapped = (Integer) binding.getObject(Integer.class); |
202 |
|
|
203 |
2 |
return wrapped == null ? 0 : wrapped.intValue(); |
204 |
|
} |
205 |
|
|
206 |
|
public static long toLong(IBinding binding) |
207 |
|
{ |
208 |
2 |
Long wrapped = (Long) binding.getObject(Long.class); |
209 |
|
|
210 |
2 |
return wrapped == null ? 0 : wrapped.longValue(); |
211 |
|
} |
212 |
|
|
213 |
|
public static float toFloat(IBinding binding) |
214 |
|
{ |
215 |
2 |
Float wrapped = (Float) binding.getObject(Float.class); |
216 |
|
|
217 |
2 |
return wrapped == null ? 0.0f : wrapped.floatValue(); |
218 |
|
} |
219 |
|
|
220 |
|
public static double toDouble(IBinding binding) |
221 |
|
{ |
222 |
2 |
Double wrapped = (Double) binding.getObject(Double.class); |
223 |
|
|
224 |
2 |
return wrapped == null ? 0.0d : wrapped.doubleValue(); |
225 |
|
} |
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
|
231 |
|
|
232 |
|
public static String getUnwrapperMethodName(Class type) |
233 |
|
{ |
234 |
7 |
Defense.notNull(type, "type"); |
235 |
|
|
236 |
7 |
return (String) _unwrappers.get(type); |
237 |
|
} |
238 |
|
|
239 |
|
|
240 |
|
|
241 |
|
|
242 |
|
|
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
public static String createUnwrapExpression(EnhancementOperation op, String bindingName, Class valueType) |
253 |
|
{ |
254 |
7 |
Defense.notNull(op, "op"); |
255 |
7 |
Defense.notNull(bindingName, "bindingName"); |
256 |
7 |
Defense.notNull(valueType, "valueType"); |
257 |
|
|
258 |
7 |
StringBuffer buffer = new StringBuffer(); |
259 |
|
|
260 |
7 |
String unwrapper = getUnwrapperMethodName(valueType); |
261 |
|
|
262 |
7 |
if (unwrapper == null) |
263 |
|
{ |
264 |
4 |
String propertyTypeRef = op.getClassReference(valueType); |
265 |
|
|
266 |
4 |
buffer.append("("); |
267 |
4 |
buffer.append(ClassFabUtils.getJavaClassName(valueType)); |
268 |
4 |
buffer.append(") "); |
269 |
4 |
buffer.append(bindingName); |
270 |
4 |
buffer.append(".getObject("); |
271 |
4 |
buffer.append(propertyTypeRef); |
272 |
4 |
buffer.append(")"); |
273 |
4 |
} |
274 |
|
else |
275 |
|
{ |
276 |
3 |
buffer.append(EnhanceUtils.class.getName()); |
277 |
3 |
buffer.append("."); |
278 |
3 |
buffer.append(unwrapper); |
279 |
3 |
buffer.append("("); |
280 |
3 |
buffer.append(bindingName); |
281 |
3 |
buffer.append(")"); |
282 |
|
} |
283 |
|
|
284 |
7 |
return buffer.toString(); |
285 |
|
} |
286 |
|
|
287 |
|
|
288 |
|
|
289 |
|
|
290 |
|
|
291 |
|
|
292 |
|
|
293 |
|
|
294 |
|
|
295 |
|
|
296 |
|
|
297 |
|
|
298 |
|
public static Class verifyPropertyType(EnhancementOperation op, String propertyName, |
299 |
|
Class requiredType) |
300 |
|
{ |
301 |
5 |
Defense.notNull(op, "op"); |
302 |
5 |
Defense.notNull(propertyName, "propertyName"); |
303 |
5 |
Defense.notNull(requiredType, "requiredType"); |
304 |
|
|
305 |
5 |
Class propertyType = op.getPropertyType(propertyName); |
306 |
|
|
307 |
|
|
308 |
5 |
if (propertyType == null) |
309 |
1 |
return Object.class; |
310 |
|
|
311 |
|
|
312 |
|
|
313 |
|
|
314 |
4 |
if (!propertyType.isAssignableFrom(requiredType)) |
315 |
1 |
throw new ApplicationRuntimeException(EnhanceMessages.wrongTypeForProperty( |
316 |
|
propertyName, |
317 |
|
propertyType, |
318 |
|
requiredType)); |
319 |
|
|
320 |
3 |
return propertyType; |
321 |
|
} |
322 |
|
|
323 |
|
|
324 |
|
|
325 |
|
|
326 |
|
|
327 |
|
|
328 |
|
|
329 |
|
|
330 |
|
|
331 |
|
public static boolean canProxyPropertyType(Class type) |
332 |
|
{ |
333 |
|
|
334 |
|
|
335 |
2 |
if (type.isInterface()) |
336 |
1 |
return true; |
337 |
|
|
338 |
1 |
if (!hasEmptyConstructor(type)) |
339 |
1 |
return false; |
340 |
|
|
341 |
0 |
if (type.isArray() || type.isPrimitive() || Modifier.isFinal(type.getModifiers()) || Object.class == type) |
342 |
0 |
return false; |
343 |
|
|
344 |
0 |
return true; |
345 |
|
} |
346 |
|
|
347 |
|
|
348 |
|
|
349 |
|
|
350 |
|
|
351 |
|
|
352 |
|
|
353 |
|
|
354 |
|
|
355 |
|
public static boolean hasEmptyConstructor(Class type) |
356 |
|
{ |
357 |
1 |
Defense.notNull(type, "type"); |
358 |
|
|
359 |
|
try { |
360 |
|
|
361 |
1 |
return type.getConstructor(null) != null; |
362 |
1 |
} catch (Throwable t) { |
363 |
1 |
return false; |
364 |
|
} |
365 |
|
} |
366 |
|
} |