|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
InjectObjectWorker.java | 100% | 100% | 100% | 100% |
|
1 | // Copyright 2004, 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
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 | * Implementation for injection type "object" (the default). Adds read-only properties to the | |
28 | * enhanced class that contain objects injected from HiveMind. | |
29 | * | |
30 | * @author Howard M. Lewis Ship | |
31 | * @since 4.0 | |
32 | */ | |
33 | public class InjectObjectWorker implements InjectEnhancementWorker | |
34 | { | |
35 | private InjectedValueProvider _provider; | |
36 | ||
37 | 376 | public void performEnhancement(EnhancementOperation op, InjectSpecification is) |
38 | { | |
39 | 376 | String name = is.getProperty(); |
40 | 376 | String objectReference = is.getObject(); |
41 | 376 | Location location = is.getLocation(); |
42 | ||
43 | 376 | injectObject(op, objectReference, name, location); |
44 | } | |
45 | ||
46 | 376 | public void injectObject(EnhancementOperation op, String objectReference, String propertyName, |
47 | Location location) | |
48 | { | |
49 | 376 | Defense.notNull(op, "op"); |
50 | 376 | Defense.notNull(propertyName, "propertyName"); |
51 | 376 | Defense.notNull(objectReference, "objectReference"); |
52 | ||
53 | 376 | Class propertyType = op.getPropertyType(propertyName); |
54 | 376 | if (propertyType == null) |
55 | 73 | propertyType = Object.class; |
56 | ||
57 | 376 | op.claimProperty(propertyName); |
58 | ||
59 | 376 | Object injectedValue = _provider.obtainValue(objectReference, location); |
60 | ||
61 | 376 | if (injectedValue == null) |
62 | 1 | throw new ApplicationRuntimeException(EnhanceMessages |
63 | .locatedValueIsNull(objectReference), location, null); | |
64 | ||
65 | 375 | if (!propertyType.isAssignableFrom(injectedValue.getClass())) |
66 | 1 | throw new ApplicationRuntimeException(EnhanceMessages.incompatibleInjectType( |
67 | objectReference, | |
68 | injectedValue, | |
69 | propertyType), location, null); | |
70 | ||
71 | 374 | String fieldName = op.addInjectedField("_$" + propertyName, propertyType, injectedValue); |
72 | ||
73 | 374 | String methodName = EnhanceUtils.createAccessorMethodName(propertyName); |
74 | ||
75 | 374 | op.addMethod( |
76 | Modifier.PUBLIC, | |
77 | new MethodSignature(propertyType, methodName, null, null), | |
78 | "return " + fieldName + ";"); | |
79 | } | |
80 | ||
81 | 43 | public void setProvider(InjectedValueProvider provider) |
82 | { | |
83 | 43 | _provider = provider; |
84 | } | |
85 | } |
|