|
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.ApplicationRuntimeException; |
|
20 |
| import org.apache.hivemind.Location; |
|
21 |
| import org.apache.hivemind.service.MethodSignature; |
|
22 |
| import org.apache.hivemind.util.Defense; |
|
23 |
| import org.apache.tapestry.services.InjectedValueProvider; |
|
24 |
| import org.apache.tapestry.spec.InjectSpecification; |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| public class InjectObjectWorker implements InjectEnhancementWorker |
|
34 |
| { |
|
35 |
| private InjectedValueProvider _provider; |
|
36 |
| |
|
37 |
1173
| public void performEnhancement(EnhancementOperation op, InjectSpecification is)
|
|
38 |
| { |
|
39 |
1173
| String name = is.getProperty();
|
|
40 |
1173
| String objectReference = is.getObject();
|
|
41 |
1173
| Location location = is.getLocation();
|
|
42 |
| |
|
43 |
1173
| injectObject(op, objectReference, name, location);
|
|
44 |
| } |
|
45 |
| |
|
46 |
1173
| public void injectObject(EnhancementOperation op, String objectReference, String propertyName,
|
|
47 |
| Location location) |
|
48 |
| { |
|
49 |
1173
| Defense.notNull(op, "op");
|
|
50 |
1173
| Defense.notNull(propertyName, "propertyName");
|
|
51 |
1173
| Defense.notNull(objectReference, "objectReference");
|
|
52 |
| |
|
53 |
1173
| Class propertyType = op.getPropertyType(propertyName);
|
|
54 |
1173
| if (propertyType == null)
|
|
55 |
189
| propertyType = Object.class;
|
|
56 |
| |
|
57 |
1173
| op.claimReadonlyProperty(propertyName);
|
|
58 |
| |
|
59 |
1173
| Object injectedValue = _provider.obtainValue(objectReference, location);
|
|
60 |
| |
|
61 |
1173
| if (injectedValue == null)
|
|
62 |
3
| throw new ApplicationRuntimeException(EnhanceMessages
|
|
63 |
| .locatedValueIsNull(objectReference), location, null); |
|
64 |
| |
|
65 |
1170
| if (!propertyType.isAssignableFrom(injectedValue.getClass()))
|
|
66 |
3
| throw new ApplicationRuntimeException(EnhanceMessages.incompatibleInjectType(
|
|
67 |
| objectReference, |
|
68 |
| injectedValue, |
|
69 |
| propertyType), location, null); |
|
70 |
| |
|
71 |
1167
| String fieldName = op.addInjectedField("_$" + propertyName, propertyType, injectedValue);
|
|
72 |
| |
|
73 |
1167
| String methodName = EnhanceUtils.createAccessorMethodName(propertyName);
|
|
74 |
| |
|
75 |
1167
| op.addMethod(
|
|
76 |
| Modifier.PUBLIC, |
|
77 |
| new MethodSignature(propertyType, methodName, null, null), |
|
78 |
| "return " + fieldName + ";", location); |
|
79 |
| } |
|
80 |
| |
|
81 |
123
| public void setProvider(InjectedValueProvider provider)
|
|
82 |
| { |
|
83 |
123
| _provider = provider;
|
|
84 |
| } |
|
85 |
| } |