1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.apache.tapestry.enhance; |
16 | |
|
17 | |
import java.util.Iterator; |
18 | |
|
19 | |
import org.apache.hivemind.ErrorLog; |
20 | |
import org.apache.hivemind.Location; |
21 | |
import org.apache.hivemind.service.BodyBuilder; |
22 | |
import org.apache.hivemind.service.ClassFabUtils; |
23 | |
import org.apache.hivemind.util.Defense; |
24 | |
import org.apache.tapestry.IComponent; |
25 | |
import org.apache.tapestry.TapestryUtils; |
26 | |
import org.apache.tapestry.spec.IComponentSpecification; |
27 | |
import org.apache.tapestry.spec.IContainedComponent; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 0 | public class InjectComponentWorker implements EnhancementWorker |
38 | |
{ |
39 | |
|
40 | |
private ErrorLog _errorLog; |
41 | |
|
42 | |
public void performEnhancement(EnhancementOperation op, |
43 | |
IComponentSpecification spec) |
44 | |
{ |
45 | 0 | Iterator i = spec.getComponentIds().iterator(); |
46 | |
|
47 | 0 | while(i.hasNext()) |
48 | |
{ |
49 | 0 | String id = (String) i.next(); |
50 | |
|
51 | 0 | IContainedComponent cc = spec.getComponent(id); |
52 | |
|
53 | 0 | String propertyName = cc.getPropertyName(); |
54 | |
|
55 | 0 | if (propertyName != null) |
56 | |
{ |
57 | |
try |
58 | |
{ |
59 | 0 | injectComponent(op, id, propertyName, cc.getLocation()); |
60 | |
} |
61 | 0 | catch (Exception ex) |
62 | |
{ |
63 | 0 | _errorLog.error(EnhanceMessages.errorAddingProperty( |
64 | |
propertyName, op.getBaseClass(), ex), cc |
65 | |
.getLocation(), ex); |
66 | 0 | } |
67 | |
} |
68 | 0 | } |
69 | 0 | } |
70 | |
|
71 | |
public void injectComponent(EnhancementOperation op, String componentId, |
72 | |
String propertyName, Location location) |
73 | |
{ |
74 | 0 | Defense.notNull(op, "op"); |
75 | 0 | Defense.notNull(componentId, "componentId"); |
76 | 0 | Defense.notNull(propertyName, "propertyName"); |
77 | |
|
78 | 0 | Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, |
79 | |
null); |
80 | |
|
81 | 0 | op.claimReadonlyProperty(propertyName); |
82 | |
|
83 | 0 | String fieldName = "_$" + propertyName; |
84 | 0 | String classField = op.getClassReference(propertyType); |
85 | 0 | String locationField = op.addInjectedField(fieldName + "$location", |
86 | 0 | Location.class, location); |
87 | |
|
88 | 0 | op.addField(fieldName, propertyType); |
89 | |
|
90 | 0 | EnhanceUtils.createSimpleAccessor(op, fieldName, propertyName, |
91 | |
propertyType, location); |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | 0 | BodyBuilder builder = new BodyBuilder(); |
98 | |
|
99 | 0 | builder.add("{0} = ({1}) ", fieldName, ClassFabUtils |
100 | |
.getJavaClassName(propertyType)); |
101 | 0 | builder.add("{0}#getComponent(this, ", TapestryUtils.class.getName()); |
102 | 0 | builder.addQuoted(componentId); |
103 | 0 | builder.add(", {0}, {1});", classField, locationField); |
104 | |
|
105 | 0 | op.extendMethodImplementation(IComponent.class, |
106 | |
EnhanceUtils.FINISH_LOAD_SIGNATURE, builder.toString()); |
107 | 0 | } |
108 | |
|
109 | |
public void setErrorLog(ErrorLog errorLog) |
110 | |
{ |
111 | 0 | _errorLog = errorLog; |
112 | 0 | } |
113 | |
} |