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