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.ApplicationRuntimeException; |
18 |
|
import org.apache.hivemind.ErrorLog; |
19 |
|
import org.apache.hivemind.Location; |
20 |
|
import org.apache.hivemind.service.BodyBuilder; |
21 |
|
import org.apache.hivemind.service.ClassFabUtils; |
22 |
|
import org.apache.hivemind.service.MethodSignature; |
23 |
|
import org.apache.hivemind.util.Defense; |
24 |
|
import org.apache.tapestry.IBinding; |
25 |
|
import org.apache.tapestry.IComponent; |
26 |
|
import org.apache.tapestry.spec.IComponentSpecification; |
27 |
|
import org.apache.tapestry.spec.IParameterSpecification; |
28 |
|
|
29 |
|
import java.lang.reflect.Modifier; |
30 |
|
import java.util.Iterator; |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
6 |
public class ParameterPropertyWorker implements EnhancementWorker |
39 |
|
{ |
40 |
|
|
41 |
|
private ErrorLog _errorLog; |
42 |
|
|
43 |
|
public void performEnhancement(EnhancementOperation op, IComponentSpecification spec) |
44 |
|
{ |
45 |
4 |
Iterator i = spec.getParameterNames().iterator(); |
46 |
8 |
while(i.hasNext()) |
47 |
|
{ |
48 |
4 |
String name = (String) i.next(); |
49 |
|
|
50 |
4 |
IParameterSpecification ps = spec.getParameter(name); |
51 |
|
|
52 |
|
try |
53 |
|
{ |
54 |
4 |
performEnhancement(op, name, ps); |
55 |
|
} |
56 |
1 |
catch (RuntimeException ex) |
57 |
|
{ |
58 |
1 |
_errorLog.error(EnhanceMessages.errorAddingProperty(ps.getPropertyName(), op.getBaseClass(), ex), |
59 |
|
ps.getLocation(), ex); |
60 |
3 |
} |
61 |
4 |
} |
62 |
4 |
} |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
private void performEnhancement(EnhancementOperation op, String parameterName, IParameterSpecification ps) |
77 |
|
{ |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
4 |
if (!parameterName.equals(ps.getParameterName())) |
82 |
1 |
return; |
83 |
|
|
84 |
3 |
String propertyName = ps.getPropertyName(); |
85 |
3 |
String specifiedType = ps.getType(); |
86 |
3 |
boolean cache = ps.getCache(); |
87 |
|
|
88 |
3 |
addParameter(op, parameterName, propertyName, specifiedType, cache, ps.getLocation()); |
89 |
2 |
} |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
public void addParameter(EnhancementOperation op, String parameterName, |
113 |
|
String propertyName, String specifiedType, boolean cache, |
114 |
|
Location location) |
115 |
|
{ |
116 |
3 |
Defense.notNull(op, "op"); |
117 |
3 |
Defense.notNull(parameterName, "parameterName"); |
118 |
3 |
Defense.notNull(propertyName, "propertyName"); |
119 |
|
|
120 |
3 |
Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, specifiedType); |
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
2 |
op.claimProperty(propertyName); |
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
2 |
String fieldName = "_$" + propertyName; |
133 |
2 |
String defaultFieldName = fieldName + "$Default"; |
134 |
2 |
String cachedFieldName = fieldName + "$Cached"; |
135 |
|
|
136 |
2 |
op.addField(fieldName, propertyType); |
137 |
2 |
op.addField(defaultFieldName, propertyType); |
138 |
2 |
op.addField(cachedFieldName, boolean.class); |
139 |
|
|
140 |
2 |
String bindingFieldName = buildBindingAccessor(op, fieldName, parameterName, location); |
141 |
|
|
142 |
2 |
buildAccessor(op, propertyName, propertyType, fieldName, |
143 |
|
defaultFieldName, cachedFieldName, bindingFieldName, |
144 |
|
cache, location); |
145 |
|
|
146 |
2 |
buildMutator(op, parameterName, propertyName, propertyType, fieldName, |
147 |
|
defaultFieldName, cachedFieldName, bindingFieldName, location); |
148 |
|
|
149 |
2 |
extendCleanupAfterRender(op, bindingFieldName, fieldName, defaultFieldName, cachedFieldName); |
150 |
2 |
} |
151 |
|
|
152 |
|
String buildBindingAccessor(EnhancementOperation op, String fieldName, String parameterName, Location location) |
153 |
|
{ |
154 |
2 |
BodyBuilder body = new BodyBuilder(); |
155 |
2 |
body.begin(); |
156 |
|
|
157 |
2 |
String bindingFieldName = fieldName + "$Binding"; |
158 |
2 |
String bindingCheckedName = bindingFieldName + "Checked"; |
159 |
|
|
160 |
5 |
op.addField(bindingFieldName, IBinding.class); |
161 |
2 |
op.addField(bindingCheckedName, Boolean.TYPE); |
162 |
|
|
163 |
2 |
body.addln("if (!{0})", bindingCheckedName); |
164 |
2 |
body.begin(); |
165 |
2 |
body.addln("{0} = getBinding(\"{1}\");", bindingFieldName, parameterName); |
166 |
2 |
body.addln("{0} = true;", bindingCheckedName); |
167 |
2 |
body.end(); |
168 |
|
|
169 |
2 |
body.addln("return {0};", bindingFieldName); |
170 |
|
|
171 |
2 |
body.end(); |
172 |
|
|
173 |
2 |
String methodName = EnhanceUtils.createAccessorMethodName(bindingFieldName); |
174 |
|
|
175 |
2 |
op.addMethod(Modifier.PUBLIC, |
176 |
|
new MethodSignature(IBinding.class, methodName, new Class[0], null), |
177 |
|
body.toString(), location); |
178 |
|
|
179 |
2 |
return methodName + "()"; |
180 |
|
} |
181 |
|
|
182 |
|
void extendCleanupAfterRender(EnhancementOperation op, String bindingFieldName, |
183 |
|
String fieldName, String defaultFieldName, String cachedFieldName) |
184 |
|
{ |
185 |
2 |
BodyBuilder cleanupBody = new BodyBuilder(); |
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
2 |
cleanupBody.addln("if ({0} && ! {1}.isInvariant())", cachedFieldName, bindingFieldName); |
194 |
2 |
cleanupBody.begin(); |
195 |
2 |
cleanupBody.addln("{0} = false;", cachedFieldName); |
196 |
2 |
cleanupBody.addln("{0} = {1};", fieldName, defaultFieldName); |
197 |
2 |
cleanupBody.end(); |
198 |
|
|
199 |
2 |
op.extendMethodImplementation(IComponent.class, |
200 |
|
EnhanceUtils.CLEANUP_AFTER_RENDER_SIGNATURE, cleanupBody.toString()); |
201 |
2 |
} |
202 |
|
|
203 |
|
private void buildMutator(EnhancementOperation op, String parameterName, |
204 |
|
String propertyName, Class propertyType, String fieldName, |
205 |
|
String defaultFieldName, String cachedFieldName, |
206 |
|
String bindingFieldName, Location location) |
207 |
|
{ |
208 |
2 |
BodyBuilder builder = new BodyBuilder(); |
209 |
2 |
builder.begin(); |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
2 |
builder.addln("if (! isInActiveState())"); |
217 |
2 |
builder.begin(); |
218 |
2 |
builder.addln("{0} = $1;", defaultFieldName); |
219 |
2 |
builder.addln("return;"); |
220 |
2 |
builder.end(); |
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
225 |
2 |
builder.addln("if ({0} == null)", bindingFieldName); |
226 |
2 |
builder.addln(" throw new {0}(\"Parameter ''{1}'' is not bound and can not be updated.\");", |
227 |
|
ApplicationRuntimeException.class.getName(), parameterName); |
228 |
|
|
229 |
|
|
230 |
|
|
231 |
2 |
builder.addln("{0}.setObject(($w) $1);", bindingFieldName); |
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
|
236 |
|
|
237 |
2 |
builder.addln("if (isRendering())"); |
238 |
2 |
builder.begin(); |
239 |
2 |
builder.addln("{0} = $1;", fieldName); |
240 |
2 |
builder.addln("{0} = true;", cachedFieldName); |
241 |
2 |
builder.end(); |
242 |
|
|
243 |
2 |
builder.end(); |
244 |
|
|
245 |
2 |
String mutatorMethodName = EnhanceUtils.createMutatorMethodName(propertyName); |
246 |
|
|
247 |
2 |
op.addMethod(Modifier.PUBLIC, |
248 |
|
new MethodSignature(void.class, mutatorMethodName, new Class[] { propertyType }, null), |
249 |
|
builder.toString(), location); |
250 |
2 |
} |
251 |
|
|
252 |
|
|
253 |
|
|
254 |
|
void buildAccessor(EnhancementOperation op, String propertyName, Class propertyType, |
255 |
|
String fieldName, String defaultFieldName, String cachedFieldName, |
256 |
|
String bindingFieldName, boolean cache, Location location) |
257 |
|
{ |
258 |
4 |
BodyBuilder builder = new BodyBuilder(); |
259 |
4 |
builder.begin(); |
260 |
|
|
261 |
4 |
builder.addln("if ({0}) return {1};", cachedFieldName, fieldName); |
262 |
4 |
builder.addln("if ({0} == null) return {1};", bindingFieldName, defaultFieldName); |
263 |
|
|
264 |
4 |
String javaTypeName = ClassFabUtils.getJavaClassName(propertyType); |
265 |
|
|
266 |
4 |
builder.addln("{0} result = {1};", javaTypeName, EnhanceUtils.createUnwrapExpression(op, bindingFieldName, propertyType)); |
267 |
|
|
268 |
|
|
269 |
|
|
270 |
|
|
271 |
|
|
272 |
|
|
273 |
4 |
String expression = cache ? "isRendering() || {0}.isInvariant()" : "{0}.isInvariant()"; |
274 |
|
|
275 |
4 |
builder.addln("if (" + expression + ")", bindingFieldName); |
276 |
4 |
builder.begin(); |
277 |
4 |
builder.addln("{0} = result;", fieldName); |
278 |
4 |
builder.addln("{0} = true;", cachedFieldName); |
279 |
4 |
builder.end(); |
280 |
|
|
281 |
4 |
builder.addln("return result;"); |
282 |
|
|
283 |
4 |
builder.end(); |
284 |
|
|
285 |
4 |
String accessorMethodName = op.getAccessorMethodName(propertyName); |
286 |
|
|
287 |
4 |
op.addMethod(Modifier.PUBLIC, new MethodSignature(propertyType, |
288 |
|
accessorMethodName, null, null), builder.toString(), location); |
289 |
4 |
} |
290 |
|
|
291 |
|
public void setErrorLog(ErrorLog errorLog) |
292 |
|
{ |
293 |
1 |
_errorLog = errorLog; |
294 |
1 |
} |
295 |
|
} |