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