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