1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.enhance; |
16 |
| |
17 |
| import java.beans.BeanInfo; |
18 |
| import java.beans.IntrospectionException; |
19 |
| import java.beans.Introspector; |
20 |
| import java.beans.PropertyDescriptor; |
21 |
| import java.lang.reflect.Constructor; |
22 |
| import java.lang.reflect.Method; |
23 |
| import java.lang.reflect.Modifier; |
24 |
| import java.util.ArrayList; |
25 |
| import java.util.HashMap; |
26 |
| import java.util.HashSet; |
27 |
| import java.util.Iterator; |
28 |
| import java.util.List; |
29 |
| import java.util.Map; |
30 |
| import java.util.Set; |
31 |
| |
32 |
| import org.apache.commons.logging.Log; |
33 |
| import org.apache.hivemind.ApplicationRuntimeException; |
34 |
| import org.apache.hivemind.ClassResolver; |
35 |
| import org.apache.hivemind.HiveMind; |
36 |
| import org.apache.hivemind.Location; |
37 |
| import org.apache.hivemind.service.BodyBuilder; |
38 |
| import org.apache.hivemind.service.ClassFab; |
39 |
| import org.apache.hivemind.service.ClassFactory; |
40 |
| import org.apache.hivemind.service.MethodSignature; |
41 |
| import org.apache.hivemind.util.Defense; |
42 |
| import org.apache.hivemind.util.ToStringBuilder; |
43 |
| import org.apache.tapestry.services.ComponentConstructor; |
44 |
| import org.apache.tapestry.spec.IComponentSpecification; |
45 |
| import org.apache.tapestry.util.IdAllocator; |
46 |
| import org.apache.tapestry.util.ObjectIdentityMap; |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| public class EnhancementOperationImpl implements EnhancementOperation |
57 |
| { |
58 |
| private ClassResolver _resolver; |
59 |
| |
60 |
| private IComponentSpecification _specification; |
61 |
| |
62 |
| private Class _baseClass; |
63 |
| |
64 |
| private ClassFab _classFab; |
65 |
| |
66 |
| private final Set _claimedProperties = new HashSet(); |
67 |
| |
68 |
| private final JavaClassMapping _javaClassMapping = new JavaClassMapping(); |
69 |
| |
70 |
| private final List _constructorTypes = new ArrayList(); |
71 |
| |
72 |
| private final List _constructorArguments = new ArrayList(); |
73 |
| |
74 |
| private final ObjectIdentityMap _finalFields = new ObjectIdentityMap(); |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
| private Set _addedInterfaces = new HashSet(); |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| private Map _incompleteMethods = new HashMap(); |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| private Map _properties = new HashMap(); |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| private BodyBuilder _constructorBuilder; |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| private final IdAllocator _idAllocator = new IdAllocator(); |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| private final Map _methods = new HashMap(); |
112 |
| |
113 |
| |
114 |
| |
115 |
| private final Log _log; |
116 |
| |
117 |
601
| public EnhancementOperationImpl(ClassResolver classResolver,
|
118 |
| IComponentSpecification specification, Class baseClass, ClassFactory classFactory, |
119 |
| Log log) |
120 |
| { |
121 |
601
| Defense.notNull(classResolver, "classResolver");
|
122 |
601
| Defense.notNull(specification, "specification");
|
123 |
601
| Defense.notNull(baseClass, "baseClass");
|
124 |
601
| Defense.notNull(classFactory, "classFactory");
|
125 |
| |
126 |
601
| _resolver = classResolver;
|
127 |
601
| _specification = specification;
|
128 |
601
| _baseClass = baseClass;
|
129 |
| |
130 |
601
| introspectBaseClass();
|
131 |
| |
132 |
601
| String name = newClassName();
|
133 |
| |
134 |
601
| _classFab = classFactory.newClass(name, _baseClass);
|
135 |
601
| _log = log;
|
136 |
| } |
137 |
| |
138 |
0
| public String toString()
|
139 |
| { |
140 |
0
| ToStringBuilder builder = new ToStringBuilder(this);
|
141 |
| |
142 |
0
| builder.append("baseClass", _baseClass.getName());
|
143 |
0
| builder.append("claimedProperties", _claimedProperties);
|
144 |
0
| builder.append("classFab", _classFab);
|
145 |
| |
146 |
0
| return builder.toString();
|
147 |
| } |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
601
| private void introspectBaseClass()
|
159 |
| { |
160 |
601
| try
|
161 |
| { |
162 |
601
| synchronized (HiveMind.INTROSPECTOR_MUTEX)
|
163 |
| { |
164 |
601
| addPropertiesDeclaredInBaseClass();
|
165 |
| } |
166 |
| } |
167 |
| catch (IntrospectionException ex) |
168 |
| { |
169 |
0
| throw new ApplicationRuntimeException(EnhanceMessages.unabelToIntrospectClass(
|
170 |
| _baseClass, |
171 |
| ex), ex); |
172 |
| } |
173 |
| |
174 |
| } |
175 |
| |
176 |
601
| private void addPropertiesDeclaredInBaseClass() throws IntrospectionException
|
177 |
| { |
178 |
601
| Class introspectClass = _baseClass;
|
179 |
| |
180 |
601
| addPropertiesDeclaredInClass(introspectClass);
|
181 |
| |
182 |
601
| List interfaceQueue = new ArrayList();
|
183 |
| |
184 |
601
| while (introspectClass != null)
|
185 |
| { |
186 |
2927
| addInterfacesToQueue(introspectClass, interfaceQueue);
|
187 |
| |
188 |
2927
| introspectClass = introspectClass.getSuperclass();
|
189 |
| } |
190 |
| |
191 |
601
| while (!interfaceQueue.isEmpty())
|
192 |
| { |
193 |
6483
| Class interfaceClass = (Class) interfaceQueue.remove(0);
|
194 |
| |
195 |
6483
| addPropertiesDeclaredInClass(interfaceClass);
|
196 |
| |
197 |
6483
| addInterfacesToQueue(interfaceClass, interfaceQueue);
|
198 |
| } |
199 |
| } |
200 |
| |
201 |
9410
| private void addInterfacesToQueue(Class introspectClass, List interfaceQueue)
|
202 |
| { |
203 |
9410
| Class[] interfaces = introspectClass.getInterfaces();
|
204 |
| |
205 |
9410
| for (int i = 0; i < interfaces.length; i++)
|
206 |
6483
| interfaceQueue.add(interfaces[i]);
|
207 |
| } |
208 |
| |
209 |
7084
| private void addPropertiesDeclaredInClass(Class introspectClass) throws IntrospectionException
|
210 |
| { |
211 |
7084
| BeanInfo bi = Introspector.getBeanInfo(introspectClass);
|
212 |
| |
213 |
7084
| PropertyDescriptor[] pds = bi.getPropertyDescriptors();
|
214 |
| |
215 |
7084
| for (int i = 0; i < pds.length; i++)
|
216 |
| { |
217 |
40084
| PropertyDescriptor pd = pds[i];
|
218 |
| |
219 |
40084
| String name = pd.getName();
|
220 |
| |
221 |
40084
| if (!_properties.containsKey(name))
|
222 |
16033
| _properties.put(name, pd);
|
223 |
| } |
224 |
| } |
225 |
| |
226 |
| |
227 |
| |
228 |
| |
229 |
| |
230 |
| |
231 |
1
| EnhancementOperationImpl()
|
232 |
| { |
233 |
1
| _log = null;
|
234 |
| } |
235 |
| |
236 |
4117
| public void claimProperty(String propertyName)
|
237 |
| { |
238 |
4117
| Defense.notNull(propertyName, "propertyName");
|
239 |
| |
240 |
4117
| if (_claimedProperties.contains(propertyName))
|
241 |
2
| throw new ApplicationRuntimeException(EnhanceMessages.claimedProperty(propertyName));
|
242 |
| |
243 |
4115
| _claimedProperties.add(propertyName);
|
244 |
| } |
245 |
| |
246 |
1176
| public void claimReadonlyProperty(String propertyName)
|
247 |
| { |
248 |
1176
| claimProperty(propertyName);
|
249 |
| |
250 |
1176
| PropertyDescriptor pd = getPropertyDescriptor(propertyName);
|
251 |
| |
252 |
1176
| if (pd != null && pd.getWriteMethod() != null)
|
253 |
1
| throw new ApplicationRuntimeException(EnhanceMessages.readonlyProperty(propertyName, pd
|
254 |
| .getWriteMethod())); |
255 |
| } |
256 |
| |
257 |
7011
| public void addField(String name, Class type)
|
258 |
| { |
259 |
7011
| _classFab.addField(name, type);
|
260 |
| } |
261 |
| |
262 |
1754
| public String addInjectedField(String fieldName, Class fieldType, Object value)
|
263 |
| { |
264 |
1754
| Defense.notNull(fieldName, "fieldName");
|
265 |
1754
| Defense.notNull(fieldType, "fieldType");
|
266 |
1754
| Defense.notNull(value, "value");
|
267 |
| |
268 |
1754
| String existing = (String) _finalFields.get(value);
|
269 |
| |
270 |
| |
271 |
| |
272 |
1754
| if (existing != null)
|
273 |
0
| return existing;
|
274 |
| |
275 |
| |
276 |
| |
277 |
| |
278 |
| |
279 |
| |
280 |
1754
| String uniqueName = _idAllocator.allocateId(fieldName);
|
281 |
| |
282 |
| |
283 |
| |
284 |
| |
285 |
1754
| _classFab.addField(uniqueName, fieldType);
|
286 |
| |
287 |
1754
| int parameterIndex = addConstructorParameter(fieldType, value);
|
288 |
| |
289 |
1754
| constructorBuilder().addln("{0} = ${1};", uniqueName, Integer.toString(parameterIndex));
|
290 |
| |
291 |
| |
292 |
| |
293 |
1754
| _finalFields.put(value, uniqueName);
|
294 |
| |
295 |
1754
| return uniqueName;
|
296 |
| } |
297 |
| |
298 |
92
| public Class convertTypeName(String type)
|
299 |
| { |
300 |
92
| Defense.notNull(type, "type");
|
301 |
| |
302 |
92
| Class result = _javaClassMapping.getType(type);
|
303 |
| |
304 |
92
| if (result == null)
|
305 |
| { |
306 |
22
| result = _resolver.findClass(type);
|
307 |
| |
308 |
21
| _javaClassMapping.recordType(type, result);
|
309 |
| } |
310 |
| |
311 |
91
| return result;
|
312 |
| } |
313 |
| |
314 |
3211
| public Class getPropertyType(String name)
|
315 |
| { |
316 |
3211
| Defense.notNull(name, "name");
|
317 |
| |
318 |
3211
| PropertyDescriptor pd = getPropertyDescriptor(name);
|
319 |
| |
320 |
3211
| return pd == null ? null : pd.getPropertyType();
|
321 |
| } |
322 |
| |
323 |
89
| public void validateProperty(String name, Class expectedType)
|
324 |
| { |
325 |
89
| Defense.notNull(name, "name");
|
326 |
89
| Defense.notNull(expectedType, "expectedType");
|
327 |
| |
328 |
89
| PropertyDescriptor pd = getPropertyDescriptor(name);
|
329 |
| |
330 |
89
| if (pd == null)
|
331 |
25
| return;
|
332 |
| |
333 |
64
| Class propertyType = pd.getPropertyType();
|
334 |
| |
335 |
64
| if (propertyType.equals(expectedType))
|
336 |
63
| return;
|
337 |
| |
338 |
1
| throw new ApplicationRuntimeException(EnhanceMessages.propertyTypeMismatch(
|
339 |
| _baseClass, |
340 |
| name, |
341 |
| propertyType, |
342 |
| expectedType)); |
343 |
| } |
344 |
| |
345 |
7826
| private PropertyDescriptor getPropertyDescriptor(String name)
|
346 |
| { |
347 |
7826
| return (PropertyDescriptor) _properties.get(name);
|
348 |
| } |
349 |
| |
350 |
3350
| public String getAccessorMethodName(String propertyName)
|
351 |
| { |
352 |
3350
| Defense.notNull(propertyName, "propertyName");
|
353 |
| |
354 |
3350
| PropertyDescriptor pd = getPropertyDescriptor(propertyName);
|
355 |
| |
356 |
3350
| if (pd != null && pd.getReadMethod() != null)
|
357 |
3145
| return pd.getReadMethod().getName();
|
358 |
| |
359 |
205
| return EnhanceUtils.createAccessorMethodName(propertyName);
|
360 |
| } |
361 |
| |
362 |
7046
| public void addMethod(int modifier, MethodSignature sig, String methodBody, Location location)
|
363 |
| { |
364 |
7046
| Defense.notNull(sig, "sig");
|
365 |
7046
| Defense.notNull(methodBody, "methodBody");
|
366 |
7046
| Defense.notNull(location, "location");
|
367 |
| |
368 |
7046
| Location existing = (Location) _methods.get(sig);
|
369 |
7046
| if (existing != null)
|
370 |
1
| throw new ApplicationRuntimeException(EnhanceMessages.methodConflict(sig, existing),
|
371 |
| location, null); |
372 |
| |
373 |
7045
| _methods.put(sig, location);
|
374 |
| |
375 |
7045
| _classFab.addMethod(modifier, sig, methodBody);
|
376 |
| } |
377 |
| |
378 |
1
| public Class getBaseClass()
|
379 |
| { |
380 |
1
| return _baseClass;
|
381 |
| } |
382 |
| |
383 |
810
| public String getClassReference(Class clazz)
|
384 |
| { |
385 |
810
| Defense.notNull(clazz, "clazz");
|
386 |
| |
387 |
810
| String result = (String) _finalFields.get(clazz);
|
388 |
| |
389 |
810
| if (result == null)
|
390 |
579
| result = addClassReference(clazz);
|
391 |
| |
392 |
810
| return result;
|
393 |
| } |
394 |
| |
395 |
579
| private String addClassReference(Class clazz)
|
396 |
| { |
397 |
579
| StringBuffer buffer = new StringBuffer("_class$");
|
398 |
| |
399 |
579
| Class c = clazz;
|
400 |
| |
401 |
579
| while (c.isArray())
|
402 |
| { |
403 |
13
| buffer.append("array$");
|
404 |
13
| c = c.getComponentType();
|
405 |
| } |
406 |
| |
407 |
579
| buffer.append(c.getName().replace('.', '$'));
|
408 |
| |
409 |
579
| String fieldName = buffer.toString();
|
410 |
| |
411 |
579
| return addInjectedField(fieldName, Class.class, clazz);
|
412 |
| } |
413 |
| |
414 |
| |
415 |
| |
416 |
| |
417 |
| |
418 |
| |
419 |
1754
| private int addConstructorParameter(Class type, Object value)
|
420 |
| { |
421 |
1754
| _constructorTypes.add(type);
|
422 |
1754
| _constructorArguments.add(value);
|
423 |
| |
424 |
1754
| return _constructorArguments.size();
|
425 |
| } |
426 |
| |
427 |
1754
| private BodyBuilder constructorBuilder()
|
428 |
| { |
429 |
1754
| if (_constructorBuilder == null)
|
430 |
| { |
431 |
410
| _constructorBuilder = new BodyBuilder();
|
432 |
410
| _constructorBuilder.begin();
|
433 |
| } |
434 |
| |
435 |
1754
| return _constructorBuilder;
|
436 |
| } |
437 |
| |
438 |
| |
439 |
| |
440 |
| |
441 |
| |
442 |
| |
443 |
580
| public ComponentConstructor getConstructor()
|
444 |
| { |
445 |
580
| try
|
446 |
| { |
447 |
580
| finalizeEnhancedClass();
|
448 |
| |
449 |
580
| Constructor c = findConstructor();
|
450 |
| |
451 |
579
| Object[] params = _constructorArguments.toArray();
|
452 |
| |
453 |
579
| return new ComponentConstructorImpl(c, params, _classFab.toString(), _specification
|
454 |
| .getLocation()); |
455 |
| } |
456 |
| catch (Throwable t) |
457 |
| { |
458 |
1
| throw new ApplicationRuntimeException(EnhanceMessages.classEnhancementFailure(
|
459 |
| _baseClass, |
460 |
| t), _classFab, null, t); |
461 |
| } |
462 |
| } |
463 |
| |
464 |
581
| void finalizeEnhancedClass()
|
465 |
| { |
466 |
581
| finalizeIncompleteMethods();
|
467 |
| |
468 |
581
| if (_constructorBuilder != null)
|
469 |
| { |
470 |
409
| _constructorBuilder.end();
|
471 |
| |
472 |
409
| Class[] types = (Class[]) _constructorTypes
|
473 |
| .toArray(new Class[_constructorTypes.size()]); |
474 |
| |
475 |
409
| _classFab.addConstructor(types, null, _constructorBuilder.toString());
|
476 |
| } |
477 |
| |
478 |
581
| if (_log != null)
|
479 |
389
| _log.debug("Creating class:\n\n" + _classFab);
|
480 |
| } |
481 |
| |
482 |
581
| private void finalizeIncompleteMethods()
|
483 |
| { |
484 |
581
| Iterator i = _incompleteMethods.entrySet().iterator();
|
485 |
581
| while (i.hasNext())
|
486 |
| { |
487 |
738
| Map.Entry e = (Map.Entry) i.next();
|
488 |
738
| MethodSignature sig = (MethodSignature) e.getKey();
|
489 |
738
| BodyBuilder builder = (BodyBuilder) e.getValue();
|
490 |
| |
491 |
| |
492 |
| |
493 |
| |
494 |
738
| builder.end();
|
495 |
| |
496 |
738
| _classFab.addMethod(Modifier.PUBLIC, sig, builder.toString());
|
497 |
| } |
498 |
| } |
499 |
| |
500 |
580
| private Constructor findConstructor()
|
501 |
| { |
502 |
580
| Class componentClass = _classFab.createClass();
|
503 |
| |
504 |
| |
505 |
| |
506 |
579
| return componentClass.getConstructors()[0];
|
507 |
| } |
508 |
| |
509 |
| static int _uid = 0; |
510 |
| |
511 |
601
| private String newClassName()
|
512 |
| { |
513 |
601
| String baseName = _baseClass.getName();
|
514 |
601
| int dotx = baseName.lastIndexOf('.');
|
515 |
| |
516 |
601
| return "$" + baseName.substring(dotx + 1) + "_" + _uid++;
|
517 |
| } |
518 |
| |
519 |
4227
| public void extendMethodImplementation(Class interfaceClass, MethodSignature methodSignature,
|
520 |
| String code) |
521 |
| { |
522 |
4227
| addInterfaceIfNeeded(interfaceClass);
|
523 |
| |
524 |
4227
| BodyBuilder builder = (BodyBuilder) _incompleteMethods.get(methodSignature);
|
525 |
| |
526 |
4227
| if (builder == null)
|
527 |
| { |
528 |
740
| builder = createIncompleteMethod(methodSignature);
|
529 |
| |
530 |
740
| _incompleteMethods.put(methodSignature, builder);
|
531 |
| } |
532 |
| |
533 |
4227
| builder.addln(code);
|
534 |
| } |
535 |
| |
536 |
4227
| private void addInterfaceIfNeeded(Class interfaceClass)
|
537 |
| { |
538 |
4227
| if (implementsInterface(interfaceClass))
|
539 |
3994
| return;
|
540 |
| |
541 |
233
| _classFab.addInterface(interfaceClass);
|
542 |
233
| _addedInterfaces.add(interfaceClass);
|
543 |
| } |
544 |
| |
545 |
6176
| public boolean implementsInterface(Class interfaceClass)
|
546 |
| { |
547 |
6176
| if (interfaceClass.isAssignableFrom(_baseClass))
|
548 |
2715
| return true;
|
549 |
| |
550 |
3461
| Iterator i = _addedInterfaces.iterator();
|
551 |
3461
| while (i.hasNext())
|
552 |
| { |
553 |
1665
| Class addedInterface = (Class) i.next();
|
554 |
| |
555 |
1665
| if (interfaceClass.isAssignableFrom(addedInterface))
|
556 |
1368
| return true;
|
557 |
| } |
558 |
| |
559 |
2093
| return false;
|
560 |
| } |
561 |
| |
562 |
740
| private BodyBuilder createIncompleteMethod(MethodSignature sig)
|
563 |
| { |
564 |
740
| BodyBuilder result = new BodyBuilder();
|
565 |
| |
566 |
| |
567 |
| |
568 |
740
| result.begin();
|
569 |
| |
570 |
740
| if (existingImplementation(sig))
|
571 |
495
| result.addln("super.{0}($$);", sig.getName());
|
572 |
| |
573 |
740
| return result;
|
574 |
| } |
575 |
| |
576 |
| |
577 |
| |
578 |
| |
579 |
| |
580 |
| |
581 |
740
| private boolean existingImplementation(MethodSignature sig)
|
582 |
| { |
583 |
740
| Method m = findMethod(sig);
|
584 |
| |
585 |
740
| return m != null && !Modifier.isAbstract(m.getModifiers());
|
586 |
| } |
587 |
| |
588 |
| |
589 |
| |
590 |
| |
591 |
740
| private Method findMethod(MethodSignature sig)
|
592 |
| { |
593 |
| |
594 |
| |
595 |
740
| try
|
596 |
| { |
597 |
740
| return _baseClass.getMethod(sig.getName(), sig.getParameterTypes());
|
598 |
| |
599 |
| } |
600 |
| catch (NoSuchMethodException ex) |
601 |
| { |
602 |
| |
603 |
| } |
604 |
| |
605 |
495
| Class c = _baseClass;
|
606 |
| |
607 |
495
| while (c != Object.class)
|
608 |
| { |
609 |
1462
| try
|
610 |
| { |
611 |
1462
| return c.getDeclaredMethod(sig.getName(), sig.getParameterTypes());
|
612 |
| } |
613 |
| catch (NoSuchMethodException ex) |
614 |
| { |
615 |
| |
616 |
| } |
617 |
| |
618 |
1200
| c = c.getSuperclass();
|
619 |
| } |
620 |
| |
621 |
233
| return null;
|
622 |
| } |
623 |
| |
624 |
560
| public List findUnclaimedAbstractProperties()
|
625 |
| { |
626 |
560
| List result = new ArrayList();
|
627 |
| |
628 |
560
| Iterator i = _properties.values().iterator();
|
629 |
| |
630 |
560
| while (i.hasNext())
|
631 |
| { |
632 |
15067
| PropertyDescriptor pd = (PropertyDescriptor) i.next();
|
633 |
| |
634 |
15067
| String name = pd.getName();
|
635 |
| |
636 |
15067
| if (_claimedProperties.contains(name))
|
637 |
2490
| continue;
|
638 |
| |
639 |
12577
| if (isAbstractProperty(pd))
|
640 |
1424
| result.add(name);
|
641 |
| } |
642 |
| |
643 |
560
| return result;
|
644 |
| } |
645 |
| |
646 |
| |
647 |
| |
648 |
| |
649 |
| |
650 |
| |
651 |
12577
| private boolean isAbstractProperty(PropertyDescriptor pd)
|
652 |
| { |
653 |
12577
| return isExistingAbstractMethod(pd.getReadMethod())
|
654 |
| || isExistingAbstractMethod(pd.getWriteMethod()); |
655 |
| } |
656 |
| |
657 |
23793
| private boolean isExistingAbstractMethod(Method m)
|
658 |
| { |
659 |
23793
| return m != null && Modifier.isAbstract(m.getModifiers());
|
660 |
| } |
661 |
| } |