|
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 |
9150
| public static String createMutatorMethodName(String propertyName)
|
|
53 |
| { |
|
54 |
9150
| return "set" + upcase(propertyName);
|
|
55 |
| } |
|
56 |
| |
|
57 |
1782
| public static String createAccessorMethodName(String propertyName)
|
|
58 |
| { |
|
59 |
1782
| return "get" + upcase(propertyName);
|
|
60 |
| } |
|
61 |
| |
|
62 |
10932
| private static String upcase(String name)
|
|
63 |
| { |
|
64 |
10932
| return name.substring(0, 1).toUpperCase() + name.substring(1);
|
|
65 |
| } |
|
66 |
| |
|
67 |
6918
| public static void createSimpleAccessor(EnhancementOperation op, String fieldName,
|
|
68 |
| String propertyName, Class propertyType, Location location) |
|
69 |
| { |
|
70 |
6918
| String methodName = op.getAccessorMethodName(propertyName);
|
|
71 |
| |
|
72 |
6918
| op.addMethod(
|
|
73 |
| Modifier.PUBLIC, |
|
74 |
| new MethodSignature(propertyType, methodName, null, null), |
|
75 |
| "return " + fieldName + ";", |
|
76 |
| location); |
|
77 |
| } |
|
78 |
| |
|
79 |
5355
| public static void createSimpleMutator(EnhancementOperation op, String fieldName,
|
|
80 |
| String propertyName, Class propertyType, Location location) |
|
81 |
| { |
|
82 |
5355
| String methodName = createMutatorMethodName(propertyName);
|
|
83 |
| |
|
84 |
5355
| 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 |
4860
| public static Class extractPropertyType(EnhancementOperation op, String propertyName,
|
|
106 |
| String definedTypeName) |
|
107 |
| { |
|
108 |
4860
| Defense.notNull(op, "op");
|
|
109 |
4860
| Defense.notNull(propertyName, "propertyName");
|
|
110 |
| |
|
111 |
4860
| if (definedTypeName != null)
|
|
112 |
| { |
|
113 |
276
| Class propertyType = op.convertTypeName(definedTypeName);
|
|
114 |
| |
|
115 |
270
| op.validateProperty(propertyName, propertyType);
|
|
116 |
| |
|
117 |
270
| return propertyType;
|
|
118 |
| } |
|
119 |
| |
|
120 |
4584
| Class propertyType = op.getPropertyType(propertyName);
|
|
121 |
| |
|
122 |
4584
| return propertyType == null ? Object.class : propertyType;
|
|
123 |
| } |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
1041
| public static boolean toBoolean(IBinding binding)
|
|
129 |
| { |
|
130 |
1041
| Boolean wrapped = (Boolean) binding.getObject(Boolean.class);
|
|
131 |
| |
|
132 |
1038
| return wrapped == null ? false : wrapped.booleanValue();
|
|
133 |
| } |
|
134 |
| |
|
135 |
6
| public static byte toByte(IBinding binding)
|
|
136 |
| { |
|
137 |
6
| Byte wrapped = (Byte) binding.getObject(Byte.class);
|
|
138 |
| |
|
139 |
6
| return wrapped == null ? 0 : wrapped.byteValue();
|
|
140 |
| } |
|
141 |
| |
|
142 |
6
| public static char toChar(IBinding binding)
|
|
143 |
| { |
|
144 |
6
| Character wrapped = (Character) binding.getObject(Character.class);
|
|
145 |
| |
|
146 |
6
| return wrapped == null ? 0 : wrapped.charValue();
|
|
147 |
| } |
|
148 |
| |
|
149 |
6
| public static short toShort(IBinding binding)
|
|
150 |
| { |
|
151 |
6
| Short wrapped = (Short) binding.getObject(Short.class);
|
|
152 |
| |
|
153 |
6
| return wrapped == null ? 0 : wrapped.shortValue();
|
|
154 |
| } |
|
155 |
| |
|
156 |
36
| public static int toInt(IBinding binding)
|
|
157 |
| { |
|
158 |
36
| Integer wrapped = (Integer) binding.getObject(Integer.class);
|
|
159 |
| |
|
160 |
36
| return wrapped == null ? 0 : wrapped.intValue();
|
|
161 |
| } |
|
162 |
| |
|
163 |
6
| public static long toLong(IBinding binding)
|
|
164 |
| { |
|
165 |
6
| Long wrapped = (Long) binding.getObject(Long.class);
|
|
166 |
| |
|
167 |
6
| return wrapped == null ? 0 : wrapped.longValue();
|
|
168 |
| } |
|
169 |
| |
|
170 |
6
| public static float toFloat(IBinding binding)
|
|
171 |
| { |
|
172 |
6
| Float wrapped = (Float) binding.getObject(Float.class);
|
|
173 |
| |
|
174 |
6
| return wrapped == null ? 0.0f : wrapped.floatValue();
|
|
175 |
| } |
|
176 |
| |
|
177 |
12
| public static double toDouble(IBinding binding)
|
|
178 |
| { |
|
179 |
12
| Double wrapped = (Double) binding.getObject(Double.class);
|
|
180 |
| |
|
181 |
12
| 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 |
3
| _unwrappers.put(boolean.class, "toBoolean");
|
|
195 |
3
| _unwrappers.put(byte.class, "toByte");
|
|
196 |
3
| _unwrappers.put(char.class, "toChar");
|
|
197 |
3
| _unwrappers.put(short.class, "toShort");
|
|
198 |
3
| _unwrappers.put(int.class, "toInt");
|
|
199 |
3
| _unwrappers.put(long.class, "toLong");
|
|
200 |
3
| _unwrappers.put(float.class, "toFloat");
|
|
201 |
3
| _unwrappers.put(double.class, "toDouble");
|
|
202 |
| } |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
3486
| public static String getUnwrapperMethodName(Class type)
|
|
210 |
| { |
|
211 |
3486
| Defense.notNull(type, "type");
|
|
212 |
| |
|
213 |
3486
| return (String) _unwrappers.get(type);
|
|
214 |
| } |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
3486
| public static String createUnwrapExpression(EnhancementOperation op, String bindingName,
|
|
230 |
| Class valueType) |
|
231 |
| { |
|
232 |
3486
| Defense.notNull(op, "op");
|
|
233 |
3486
| Defense.notNull(bindingName, "bindingName");
|
|
234 |
3486
| Defense.notNull(valueType, "valueType");
|
|
235 |
| |
|
236 |
3486
| StringBuffer buffer = new StringBuffer();
|
|
237 |
| |
|
238 |
3486
| String unwrapper = getUnwrapperMethodName(valueType);
|
|
239 |
| |
|
240 |
3486
| if (unwrapper == null)
|
|
241 |
| { |
|
242 |
2583
| String propertyTypeRef = op.getClassReference(valueType);
|
|
243 |
| |
|
244 |
2583
| buffer.append("(");
|
|
245 |
2583
| buffer.append(ClassFabUtils.getJavaClassName(valueType));
|
|
246 |
2583
| buffer.append(") ");
|
|
247 |
2583
| buffer.append(bindingName);
|
|
248 |
2583
| buffer.append(".getObject(");
|
|
249 |
2583
| buffer.append(propertyTypeRef);
|
|
250 |
2583
| buffer.append(")");
|
|
251 |
| } |
|
252 |
| else |
|
253 |
| { |
|
254 |
903
| buffer.append(EnhanceUtils.class.getName());
|
|
255 |
903
| buffer.append(".");
|
|
256 |
903
| buffer.append(unwrapper);
|
|
257 |
903
| buffer.append("(");
|
|
258 |
903
| buffer.append(bindingName);
|
|
259 |
903
| buffer.append(")");
|
|
260 |
| } |
|
261 |
| |
|
262 |
3486
| return buffer.toString();
|
|
263 |
| } |
|
264 |
| |
|
265 |
| |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
15
| public static Class verifyPropertyType(EnhancementOperation op, String propertyName,
|
|
277 |
| Class requiredType) |
|
278 |
| { |
|
279 |
15
| Defense.notNull(op, "op");
|
|
280 |
15
| Defense.notNull(propertyName, "propertyName");
|
|
281 |
15
| Defense.notNull(requiredType, "requiredType");
|
|
282 |
| |
|
283 |
15
| Class propertyType = op.getPropertyType(propertyName);
|
|
284 |
| |
|
285 |
| |
|
286 |
15
| if (propertyType == null)
|
|
287 |
3
| return Object.class;
|
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
12
| if (!propertyType.isAssignableFrom(requiredType))
|
|
293 |
3
| throw new ApplicationRuntimeException(EnhanceMessages.wrongTypeForProperty(
|
|
294 |
| propertyName, |
|
295 |
| propertyType, |
|
296 |
| requiredType)); |
|
297 |
| |
|
298 |
9
| return propertyType;
|
|
299 |
| } |
|
300 |
| } |