|
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 |
| public class InjectComponentWorker implements EnhancementWorker |
|
37 |
| { |
|
38 |
| private ErrorLog _errorLog; |
|
39 |
| |
|
40 |
1176
| public void performEnhancement(EnhancementOperation op, IComponentSpecification spec)
|
|
41 |
| { |
|
42 |
1176
| Iterator i = spec.getComponentIds().iterator();
|
|
43 |
| |
|
44 |
1176
| while (i.hasNext())
|
|
45 |
| { |
|
46 |
702
| String id = (String) i.next();
|
|
47 |
| |
|
48 |
702
| IContainedComponent cc = spec.getComponent(id);
|
|
49 |
| |
|
50 |
702
| String propertyName = cc.getPropertyName();
|
|
51 |
| |
|
52 |
702
| if (propertyName != null)
|
|
53 |
| { |
|
54 |
6
| try
|
|
55 |
| { |
|
56 |
6
| injectComponent(op, id, propertyName, cc.getLocation());
|
|
57 |
| } |
|
58 |
| catch (Exception ex) |
|
59 |
| { |
|
60 |
3
| _errorLog.error(EnhanceMessages.errorAddingProperty(propertyName, op
|
|
61 |
| .getBaseClass(), ex), cc.getLocation(), ex); |
|
62 |
| } |
|
63 |
| } |
|
64 |
| } |
|
65 |
| } |
|
66 |
| |
|
67 |
6
| public void injectComponent(EnhancementOperation op, String componentId, String propertyName,
|
|
68 |
| Location location) |
|
69 |
| { |
|
70 |
6
| Defense.notNull(op, "op");
|
|
71 |
6
| Defense.notNull(componentId, "componentId");
|
|
72 |
6
| Defense.notNull(propertyName, "propertyName");
|
|
73 |
| |
|
74 |
6
| Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, null);
|
|
75 |
| |
|
76 |
6
| op.claimReadonlyProperty(propertyName);
|
|
77 |
| |
|
78 |
3
| String fieldName = "_$" + propertyName;
|
|
79 |
3
| String classField = op.getClassReference(propertyType);
|
|
80 |
3
| String locationField = op.addInjectedField(
|
|
81 |
| fieldName + "$location", |
|
82 |
| Location.class, |
|
83 |
| location); |
|
84 |
| |
|
85 |
3
| op.addField(fieldName, propertyType);
|
|
86 |
| |
|
87 |
3
| EnhanceUtils.createSimpleAccessor(op, fieldName, propertyName, propertyType, location);
|
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
3
| BodyBuilder builder = new BodyBuilder();
|
|
93 |
| |
|
94 |
3
| builder.add("{0} = ({1}) ", fieldName, ClassFabUtils.getJavaClassName(propertyType));
|
|
95 |
3
| builder.add("{0}#getComponent(this, ", TapestryUtils.class.getName());
|
|
96 |
3
| builder.addQuoted(componentId);
|
|
97 |
3
| builder.add(", {0}, {1});", classField, locationField);
|
|
98 |
| |
|
99 |
3
| op.extendMethodImplementation(IComponent.class, EnhanceUtils.FINISH_LOAD_SIGNATURE, builder
|
|
100 |
| .toString()); |
|
101 |
| } |
|
102 |
| |
|
103 |
120
| public void setErrorLog(ErrorLog errorLog)
|
|
104 |
| { |
|
105 |
120
| _errorLog = errorLog;
|
|
106 |
| } |
|
107 |
| } |