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