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.Location; |
18 |
|
import org.apache.hivemind.service.BodyBuilder; |
19 |
|
import org.apache.hivemind.service.ClassFabUtils; |
20 |
|
import org.apache.hivemind.service.MethodSignature; |
21 |
|
import org.apache.hivemind.util.Defense; |
22 |
|
import org.apache.tapestry.engine.state.ApplicationStateManager; |
23 |
|
import org.apache.tapestry.event.PageDetachListener; |
24 |
|
import org.apache.tapestry.spec.InjectSpecification; |
25 |
|
|
26 |
|
import java.lang.reflect.Modifier; |
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
1 |
public class InjectStateWorker implements InjectEnhancementWorker |
41 |
|
{ |
42 |
|
|
43 |
|
private ApplicationStateManager _applicationStateManager; |
44 |
|
|
45 |
|
public void performEnhancement(EnhancementOperation op, |
46 |
|
InjectSpecification spec) |
47 |
|
{ |
48 |
1 |
injectState(op, spec.getObject(), spec.getProperty(), spec.getLocation()); |
49 |
1 |
} |
50 |
|
|
51 |
|
void injectState(EnhancementOperation op, String objectName, |
52 |
|
String propertyName, Location location) |
53 |
|
{ |
54 |
1 |
Defense.notNull(op, "op"); |
55 |
1 |
Defense.notNull(objectName, "objectName"); |
56 |
1 |
Defense.notNull(propertyName, "propertyName"); |
57 |
|
|
58 |
1 |
Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, null); |
59 |
1 |
String fieldName = "_$" + propertyName; |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
1 |
op.claimProperty(propertyName); |
64 |
|
|
65 |
1 |
op.addField(fieldName, propertyType); |
66 |
|
|
67 |
3 |
String managerField = op.addInjectedField("_$applicationStateManager", ApplicationStateManager.class, _applicationStateManager); |
68 |
|
|
69 |
1 |
BodyBuilder builder = new BodyBuilder(); |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
1 |
builder.begin(); |
74 |
1 |
builder.addln("if ({0} == null)", fieldName); |
75 |
1 |
builder.addln(" {0} = ({1}) {2}.get(\"{3}\");", new Object[] { |
76 |
|
fieldName, ClassFabUtils.getJavaClassName(propertyType), |
77 |
|
managerField, objectName }); |
78 |
1 |
builder.addln("return {0};", fieldName); |
79 |
1 |
builder.end(); |
80 |
|
|
81 |
1 |
String methodName = op.getAccessorMethodName(propertyName); |
82 |
|
|
83 |
1 |
MethodSignature sig = new MethodSignature(propertyType, methodName, |
84 |
|
null, null); |
85 |
|
|
86 |
1 |
op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location); |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
1 |
builder.clear(); |
91 |
1 |
builder.begin(); |
92 |
1 |
builder.addln("{0}.store(\"{1}\", $1);", managerField, objectName); |
93 |
1 |
builder.addln("{0} = $1;", fieldName); |
94 |
1 |
builder.end(); |
95 |
|
|
96 |
1 |
sig = new MethodSignature(void.class, EnhanceUtils |
97 |
|
.createMutatorMethodName(propertyName), |
98 |
|
new Class[] { propertyType }, null); |
99 |
|
|
100 |
1 |
op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location); |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
1 |
op.extendMethodImplementation(PageDetachListener.class, |
105 |
|
EnhanceUtils.PAGE_DETACHED_SIGNATURE, fieldName + " = null;"); |
106 |
1 |
} |
107 |
|
|
108 |
|
public void setApplicationStateManager(ApplicationStateManager applicationStateManager) |
109 |
|
{ |
110 |
1 |
_applicationStateManager = applicationStateManager; |
111 |
1 |
} |
112 |
|
} |