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