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 |
339
| public void performEnhancement(EnhancementOperation op, InjectSpecification is)
|
38 |
| { |
39 |
339
| String name = is.getProperty();
|
40 |
339
| String objectReference = is.getObject();
|
41 |
339
| Location location = is.getLocation();
|
42 |
| |
43 |
339
| injectObject(op, objectReference, name, location);
|
44 |
| } |
45 |
| |
46 |
339
| public void injectObject(EnhancementOperation op, String objectReference, String propertyName,
|
47 |
| Location location) |
48 |
| { |
49 |
339
| Defense.notNull(op, "op");
|
50 |
339
| Defense.notNull(propertyName, "propertyName");
|
51 |
339
| Defense.notNull(objectReference, "objectReference");
|
52 |
| |
53 |
339
| Class propertyType = op.getPropertyType(propertyName);
|
54 |
339
| if (propertyType == null)
|
55 |
58
| propertyType = Object.class;
|
56 |
| |
57 |
339
| String fieldName = "_$" + propertyName;
|
58 |
| |
59 |
339
| op.claimProperty(propertyName);
|
60 |
| |
61 |
339
| Object injectedValue = _provider.obtainValue(objectReference, location);
|
62 |
| |
63 |
339
| if (injectedValue == null)
|
64 |
1
| throw new ApplicationRuntimeException(EnhanceMessages
|
65 |
| .locatedValueIsNull(objectReference), location, null); |
66 |
| |
67 |
338
| if (!propertyType.isAssignableFrom(injectedValue.getClass()))
|
68 |
1
| throw new ApplicationRuntimeException(EnhanceMessages.incompatibleInjectType(
|
69 |
| objectReference, |
70 |
| injectedValue, |
71 |
| propertyType), location, null); |
72 |
| |
73 |
337
| op.addInjectedField(fieldName, propertyType, injectedValue);
|
74 |
| |
75 |
337
| String methodName = EnhanceUtils.createAccessorMethodName(propertyName);
|
76 |
| |
77 |
337
| op.addMethod(
|
78 |
| Modifier.PUBLIC, |
79 |
| new MethodSignature(propertyType, methodName, null, null), |
80 |
| "return " + fieldName + ";"); |
81 |
| } |
82 |
| |
83 |
42
| public void setProvider(InjectedValueProvider provider)
|
84 |
| { |
85 |
42
| _provider = provider;
|
86 |
| } |
87 |
| } |