1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.apache.tapestry.enhance; |
16 | |
|
17 | |
import org.apache.hivemind.ErrorLog; |
18 | |
import org.apache.hivemind.Location; |
19 | |
import org.apache.hivemind.service.BodyBuilder; |
20 | |
import org.apache.hivemind.service.MethodSignature; |
21 | |
import org.apache.hivemind.util.Defense; |
22 | |
import org.apache.tapestry.IBinding; |
23 | |
import org.apache.tapestry.IComponent; |
24 | |
import org.apache.tapestry.binding.BindingSource; |
25 | |
import org.apache.tapestry.event.PageDetachListener; |
26 | |
import org.apache.tapestry.spec.IComponentSpecification; |
27 | |
import org.apache.tapestry.spec.IPropertySpecification; |
28 | |
|
29 | |
import java.lang.reflect.Modifier; |
30 | |
import java.util.Iterator; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 0 | public class SpecifiedPropertyWorker implements EnhancementWorker |
41 | |
{ |
42 | |
private ErrorLog _errorLog; |
43 | |
|
44 | |
private BindingSource _bindingSource; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public void performEnhancement(EnhancementOperation op, IComponentSpecification spec) |
54 | |
{ |
55 | 0 | Iterator i = spec.getPropertySpecificationNames().iterator(); |
56 | |
|
57 | 0 | while(i.hasNext()) |
58 | |
{ |
59 | 0 | String name = (String) i.next(); |
60 | 0 | IPropertySpecification ps = spec.getPropertySpecification(name); |
61 | |
|
62 | |
try |
63 | |
{ |
64 | 0 | performEnhancement(op, ps); |
65 | |
} |
66 | 0 | catch (RuntimeException ex) |
67 | |
{ |
68 | 0 | _errorLog.error(EnhanceMessages.errorAddingProperty(name, op |
69 | |
.getBaseClass(), ex), ps.getLocation(), ex); |
70 | 0 | } |
71 | 0 | } |
72 | 0 | } |
73 | |
|
74 | |
private void performEnhancement(EnhancementOperation op, |
75 | |
IPropertySpecification ps) |
76 | |
{ |
77 | 0 | Defense.notNull(ps, "ps"); |
78 | |
|
79 | 0 | String propertyName = ps.getName(); |
80 | 0 | String specifiedType = ps.getType(); |
81 | 0 | boolean persistent = ps.isPersistent(); |
82 | 0 | String initialValue = ps.getInitialValue(); |
83 | 0 | Location location = ps.getLocation(); |
84 | |
|
85 | 0 | addProperty(op, propertyName, specifiedType, persistent, initialValue, location, ps); |
86 | 0 | } |
87 | |
|
88 | |
public void addProperty(EnhancementOperation op, String propertyName, String specifiedType, |
89 | |
boolean persistent, String initialValue, Location location, IPropertySpecification ps) |
90 | |
{ |
91 | 0 | Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, specifiedType, ps.isGeneric()); |
92 | |
|
93 | 0 | op.claimProperty(propertyName); |
94 | |
|
95 | 0 | String field = "_$" + propertyName; |
96 | |
|
97 | 0 | op.addField(field, propertyType); |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | 0 | EnhanceUtils.createSimpleAccessor(op, field, propertyName, propertyType, location); |
105 | |
|
106 | 0 | addMutator(op, propertyName, propertyType, field, persistent, location); |
107 | |
|
108 | 0 | if (initialValue == null) |
109 | 0 | addReinitializer(op, propertyType, field); |
110 | |
else |
111 | 0 | addInitialValue(op, propertyName, propertyType, field, initialValue, persistent, location); |
112 | 0 | } |
113 | |
|
114 | |
private void addReinitializer(EnhancementOperation op, Class propertyType, String fieldName) |
115 | |
{ |
116 | 0 | String defaultFieldName = fieldName + "$default"; |
117 | |
|
118 | 0 | op.addField(defaultFieldName, propertyType); |
119 | |
|
120 | |
|
121 | |
|
122 | 0 | op.extendMethodImplementation(IComponent.class, EnhanceUtils.FINISH_LOAD_SIGNATURE, |
123 | |
defaultFieldName + " = " + fieldName + ";"); |
124 | |
|
125 | |
|
126 | |
|
127 | 0 | op.extendMethodImplementation(PageDetachListener.class, |
128 | |
EnhanceUtils.PAGE_DETACHED_SIGNATURE, fieldName + " = " + defaultFieldName + ";"); |
129 | 0 | } |
130 | |
|
131 | |
private void addInitialValue(EnhancementOperation op, String propertyName, Class propertyType, |
132 | |
String fieldName, String initialValue, boolean persistent, Location location) |
133 | |
{ |
134 | 0 | String description = EnhanceMessages.initialValueForProperty(propertyName); |
135 | |
|
136 | 0 | InitialValueBindingCreator creator = |
137 | |
new InitialValueBindingCreator(_bindingSource, description, initialValue, location); |
138 | |
|
139 | 0 | String creatorField = op.addInjectedField(fieldName + "$initialValueBindingCreator", InitialValueBindingCreator.class, creator); |
140 | |
|
141 | 0 | String bindingField = fieldName + "$initialValueBinding"; |
142 | 0 | op.addField(bindingField, IBinding.class); |
143 | |
|
144 | 0 | BodyBuilder builder = new BodyBuilder(); |
145 | |
|
146 | 0 | builder.addln("{0} = {1}.createBinding(this);", bindingField, creatorField); |
147 | |
|
148 | 0 | op.extendMethodImplementation(IComponent.class, EnhanceUtils.FINISH_LOAD_SIGNATURE, builder.toString()); |
149 | |
|
150 | 0 | builder.clear(); |
151 | |
|
152 | 0 | builder.addln("{0} = {1};", fieldName, EnhanceUtils.createUnwrapExpression(op, bindingField, propertyType)); |
153 | |
|
154 | 0 | String code = builder.toString(); |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | 0 | op.extendMethodImplementation(IComponent.class, EnhanceUtils.FINISH_LOAD_SIGNATURE, code); |
161 | |
|
162 | 0 | op.extendMethodImplementation(PageDetachListener.class, EnhanceUtils.PAGE_DETACHED_SIGNATURE, code); |
163 | 0 | } |
164 | |
|
165 | |
private void addMutator(EnhancementOperation op, String propertyName, |
166 | |
Class propertyType, String fieldName, boolean persistent, Location location) |
167 | |
{ |
168 | 0 | String methodName = EnhanceUtils.createMutatorMethodName(propertyName); |
169 | |
|
170 | 0 | BodyBuilder body = new BodyBuilder(); |
171 | |
|
172 | 0 | body.begin(); |
173 | |
|
174 | 0 | if (persistent) { |
175 | |
|
176 | 0 | body.add("org.apache.tapestry.Tapestry#fireObservedChange(this, "); |
177 | 0 | body.addQuoted(propertyName); |
178 | 0 | body.addln(", ($w) $1);"); |
179 | |
} |
180 | |
|
181 | 0 | body.addln(fieldName + " = $1;"); |
182 | 0 | body.end(); |
183 | |
|
184 | 0 | MethodSignature sig = new MethodSignature(void.class, methodName, new Class[] { propertyType }, null); |
185 | |
|
186 | 0 | op.addMethod(Modifier.PUBLIC, sig, body.toString(), location); |
187 | 0 | } |
188 | |
|
189 | |
public void setErrorLog(ErrorLog errorLog) |
190 | |
{ |
191 | 0 | _errorLog = errorLog; |
192 | 0 | } |
193 | |
|
194 | |
public void setBindingSource(BindingSource bindingSource) |
195 | |
{ |
196 | 0 | _bindingSource = bindingSource; |
197 | 0 | } |
198 | |
} |