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 | 0 | public static final MethodSignature FINISH_LOAD_SIGNATURE = new MethodSignature(void.class, |
41 | |
"finishLoad", new Class[] |
42 | 0 | { IRequestCycle.class, IPageLoader.class, IComponentSpecification.class }, null); |
43 | |
|
44 | 0 | public static final MethodSignature PAGE_DETACHED_SIGNATURE = new MethodSignature(void.class, |
45 | |
"pageDetached", new Class[] |
46 | |
{ PageEvent.class }, null); |
47 | |
|
48 | 0 | 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 | 0 | private static Map _unwrappers = new HashMap(); |
59 | |
|
60 | |
static |
61 | |
{ |
62 | 0 | _unwrappers.put(boolean.class, "toBoolean"); |
63 | 0 | _unwrappers.put(byte.class, "toByte"); |
64 | 0 | _unwrappers.put(char.class, "toChar"); |
65 | 0 | _unwrappers.put(short.class, "toShort"); |
66 | 0 | _unwrappers.put(int.class, "toInt"); |
67 | 0 | _unwrappers.put(long.class, "toLong"); |
68 | 0 | _unwrappers.put(float.class, "toFloat"); |
69 | 0 | _unwrappers.put(double.class, "toDouble"); |
70 | 0 | } |
71 | |
|
72 | |
|
73 | 0 | private EnhanceUtils() { } |
74 | |
|
75 | |
public static String createMutatorMethodName(String propertyName) |
76 | |
{ |
77 | 0 | return "set" + upcase(propertyName); |
78 | |
} |
79 | |
|
80 | |
public static String createAccessorMethodName(String propertyName) |
81 | |
{ |
82 | 0 | return "get" + upcase(propertyName); |
83 | |
} |
84 | |
|
85 | |
private static String upcase(String name) |
86 | |
{ |
87 | 0 | 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 | 0 | String methodName = op.getAccessorMethodName(propertyName); |
94 | |
|
95 | 0 | op.addMethod( Modifier.PUBLIC, new MethodSignature(propertyType, methodName, null, null), |
96 | |
"return " + fieldName + ";", location); |
97 | 0 | } |
98 | |
|
99 | |
public static void createSimpleMutator(EnhancementOperation op, String fieldName, |
100 | |
String propertyName, Class propertyType, Location location) |
101 | |
{ |
102 | 0 | String methodName = createMutatorMethodName(propertyName); |
103 | |
|
104 | 0 | op.addMethod(Modifier.PUBLIC, new MethodSignature(void.class, methodName, |
105 | |
new Class[] { propertyType }, null), fieldName + " = $1;", location); |
106 | 0 | } |
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 | 0 | 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 | 0 | Defense.notNull(op, "op"); |
151 | 0 | Defense.notNull(propertyName, "propertyName"); |
152 | |
|
153 | 0 | if (definedTypeName != null) |
154 | |
{ |
155 | 0 | Class propertyType = op.convertTypeName(definedTypeName); |
156 | |
|
157 | 0 | if (!isGeneric) |
158 | 0 | op.validateProperty(propertyName, propertyType); |
159 | |
|
160 | 0 | return propertyType; |
161 | |
} |
162 | |
|
163 | 0 | Class propertyType = op.getPropertyType(propertyName); |
164 | |
|
165 | 0 | return propertyType == null ? Object.class : propertyType; |
166 | |
} |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
public static boolean toBoolean(IBinding binding) |
172 | |
{ |
173 | 0 | Boolean wrapped = (Boolean) binding.getObject(Boolean.class); |
174 | |
|
175 | 0 | return wrapped == null ? false : wrapped.booleanValue(); |
176 | |
} |
177 | |
|
178 | |
public static byte toByte(IBinding binding) |
179 | |
{ |
180 | 0 | Byte wrapped = (Byte) binding.getObject(Byte.class); |
181 | |
|
182 | 0 | return wrapped == null ? 0 : wrapped.byteValue(); |
183 | |
} |
184 | |
|
185 | |
public static char toChar(IBinding binding) |
186 | |
{ |
187 | 0 | Character wrapped = (Character) binding.getObject(Character.class); |
188 | |
|
189 | 0 | return wrapped == null ? 0 : wrapped.charValue(); |
190 | |
} |
191 | |
|
192 | |
public static short toShort(IBinding binding) |
193 | |
{ |
194 | 0 | Short wrapped = (Short) binding.getObject(Short.class); |
195 | |
|
196 | 0 | return wrapped == null ? 0 : wrapped.shortValue(); |
197 | |
} |
198 | |
|
199 | |
public static int toInt(IBinding binding) |
200 | |
{ |
201 | 0 | Integer wrapped = (Integer) binding.getObject(Integer.class); |
202 | |
|
203 | 0 | return wrapped == null ? 0 : wrapped.intValue(); |
204 | |
} |
205 | |
|
206 | |
public static long toLong(IBinding binding) |
207 | |
{ |
208 | 0 | Long wrapped = (Long) binding.getObject(Long.class); |
209 | |
|
210 | 0 | return wrapped == null ? 0 : wrapped.longValue(); |
211 | |
} |
212 | |
|
213 | |
public static float toFloat(IBinding binding) |
214 | |
{ |
215 | 0 | Float wrapped = (Float) binding.getObject(Float.class); |
216 | |
|
217 | 0 | return wrapped == null ? 0.0f : wrapped.floatValue(); |
218 | |
} |
219 | |
|
220 | |
public static double toDouble(IBinding binding) |
221 | |
{ |
222 | 0 | Double wrapped = (Double) binding.getObject(Double.class); |
223 | |
|
224 | 0 | return wrapped == null ? 0.0d : wrapped.doubleValue(); |
225 | |
} |
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
public static String getUnwrapperMethodName(Class type) |
233 | |
{ |
234 | 0 | Defense.notNull(type, "type"); |
235 | |
|
236 | 0 | 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 | 0 | Defense.notNull(op, "op"); |
255 | 0 | Defense.notNull(bindingName, "bindingName"); |
256 | 0 | Defense.notNull(valueType, "valueType"); |
257 | |
|
258 | 0 | StringBuffer buffer = new StringBuffer(); |
259 | |
|
260 | 0 | String unwrapper = getUnwrapperMethodName(valueType); |
261 | |
|
262 | 0 | if (unwrapper == null) |
263 | |
{ |
264 | 0 | String propertyTypeRef = op.getClassReference(valueType); |
265 | |
|
266 | 0 | buffer.append("("); |
267 | 0 | buffer.append(ClassFabUtils.getJavaClassName(valueType)); |
268 | 0 | buffer.append(") "); |
269 | 0 | buffer.append(bindingName); |
270 | 0 | buffer.append(".getObject("); |
271 | 0 | buffer.append(propertyTypeRef); |
272 | 0 | buffer.append(")"); |
273 | 0 | } |
274 | |
else |
275 | |
{ |
276 | 0 | buffer.append(EnhanceUtils.class.getName()); |
277 | 0 | buffer.append("."); |
278 | 0 | buffer.append(unwrapper); |
279 | 0 | buffer.append("("); |
280 | 0 | buffer.append(bindingName); |
281 | 0 | buffer.append(")"); |
282 | |
} |
283 | |
|
284 | 0 | 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 | 0 | Defense.notNull(op, "op"); |
302 | 0 | Defense.notNull(propertyName, "propertyName"); |
303 | 0 | Defense.notNull(requiredType, "requiredType"); |
304 | |
|
305 | 0 | Class propertyType = op.getPropertyType(propertyName); |
306 | |
|
307 | |
|
308 | 0 | if (propertyType == null) |
309 | 0 | return Object.class; |
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | 0 | if (!propertyType.isAssignableFrom(requiredType)) |
315 | 0 | throw new ApplicationRuntimeException(EnhanceMessages.wrongTypeForProperty( |
316 | |
propertyName, |
317 | |
propertyType, |
318 | |
requiredType)); |
319 | |
|
320 | 0 | return propertyType; |
321 | |
} |
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
public static boolean canProxyPropertyType(Class type) |
332 | |
{ |
333 | |
|
334 | |
|
335 | 0 | if (type.isInterface()) |
336 | 0 | return true; |
337 | |
|
338 | 0 | if (!hasEmptyConstructor(type)) |
339 | 0 | 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 | 0 | Defense.notNull(type, "type"); |
358 | |
|
359 | |
try { |
360 | |
|
361 | 0 | return type.getConstructor(null) != null; |
362 | 0 | } catch (Throwable t) { |
363 | 0 | return false; |
364 | |
} |
365 | |
} |
366 | |
} |