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