|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover 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.service.MethodSignature;
|
|
21 |
import org.apache.tapestry.services.InjectedValueProvider;
|
|
22 |
import org.apache.tapestry.spec.InjectSpecification;
|
|
23 |
|
|
24 |
/**
|
|
25 |
* Implementation for injection type "object" (the default). Adds read-only properties to the
|
|
26 |
* enhanced class that contain objects injected from HiveMind.
|
|
27 |
*
|
|
28 |
* @author Howard M. Lewis Ship
|
|
29 |
* @since 4.0
|
|
30 |
*/
|
|
31 |
public class InjectObjectWorker implements InjectEnhancementWorker |
|
32 |
{ |
|
33 |
private InjectedValueProvider _provider;
|
|
34 |
|
|
35 | 383 |
public void performEnhancement(EnhancementOperation op, InjectSpecification is) |
36 |
{ |
|
37 | 383 |
String name = is.getProperty(); |
38 | 383 |
String objectReference = is.getObject(); |
39 |
|
|
40 | 383 |
Class propertyType = op.getPropertyType(name); |
41 | 383 |
if (propertyType == null) |
42 | 62 |
propertyType = Object.class;
|
43 |
|
|
44 | 383 |
String fieldName = "_$" + name;
|
45 |
|
|
46 | 383 |
op.claimProperty(name); |
47 |
|
|
48 | 383 |
Object injectedValue = _provider.obtainValue(objectReference, is.getLocation()); |
49 |
|
|
50 | 383 |
if (injectedValue == null) |
51 | 1 |
throw new ApplicationRuntimeException(EnhanceMessages |
52 |
.locatedValueIsNull(objectReference), is.getLocation(), null);
|
|
53 |
|
|
54 | 382 |
if (!propertyType.isAssignableFrom(injectedValue.getClass()))
|
55 | 1 |
throw new ApplicationRuntimeException(EnhanceMessages.incompatibleInjectType( |
56 |
objectReference, |
|
57 |
injectedValue, |
|
58 |
propertyType), is.getLocation(), null);
|
|
59 |
|
|
60 | 381 |
op.addInjectedField(fieldName, injectedValue); |
61 |
|
|
62 | 381 |
String methodName = EnhanceUtils.createAccessorMethodName(name); |
63 |
|
|
64 | 381 |
op.addMethod( |
65 |
Modifier.PUBLIC, |
|
66 |
new MethodSignature(propertyType, methodName, null, null), |
|
67 |
"return " + fieldName + ";"); |
|
68 |
} |
|
69 |
|
|
70 | 47 |
public void setProvider(InjectedValueProvider provider) |
71 |
{ |
|
72 | 47 |
_provider = provider; |
73 |
} |
|
74 |
} |
|