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