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