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