|
|||||||||||||||||||
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 | |||||||||||||||
InjectWorker.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 |
import java.util.Iterator;
|
|
19 |
|
|
20 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
21 |
import org.apache.hivemind.ErrorLog;
|
|
22 |
import org.apache.hivemind.service.MethodSignature;
|
|
23 |
import org.apache.tapestry.services.InjectedValueProvider;
|
|
24 |
import org.apache.tapestry.spec.IComponentSpecification;
|
|
25 |
import org.apache.tapestry.spec.InjectSpecification;
|
|
26 |
|
|
27 |
/**
|
|
28 |
* Adds read-only properties to the enhanced class that contain data injected from HiveMind.
|
|
29 |
*
|
|
30 |
* @author Howard M. Lewis Ship
|
|
31 |
* @since 4.0
|
|
32 |
*/
|
|
33 |
public class InjectWorker implements EnhancementWorker |
|
34 |
{ |
|
35 |
private ErrorLog _errorLog;
|
|
36 |
|
|
37 |
private InjectedValueProvider _provider;
|
|
38 |
|
|
39 | 468 |
public void performEnhancement(EnhancementOperation op, IComponentSpecification spec) |
40 |
{ |
|
41 | 468 |
Iterator i = spec.getInjectSpecifications().iterator(); |
42 |
|
|
43 | 468 |
while (i.hasNext())
|
44 |
{ |
|
45 | 351 |
InjectSpecification is = (InjectSpecification) i.next(); |
46 |
|
|
47 | 351 |
try
|
48 |
{ |
|
49 | 351 |
performEnhancement(op, is); |
50 |
} |
|
51 |
catch (Exception ex)
|
|
52 |
{ |
|
53 | 2 |
_errorLog.error(EnhanceMessages.errorAddingProperty( |
54 |
is.getProperty(), |
|
55 |
op.getBaseClass(), |
|
56 |
ex), is.getLocation(), ex); |
|
57 |
} |
|
58 |
} |
|
59 |
} |
|
60 |
|
|
61 | 351 |
private void performEnhancement(EnhancementOperation op, InjectSpecification is) |
62 |
{ |
|
63 | 351 |
String name = is.getProperty(); |
64 | 351 |
String objectReference = is.getObjectReference(); |
65 |
|
|
66 | 351 |
Class propertyType = op.getPropertyType(name); |
67 | 351 |
if (propertyType == null) |
68 | 62 |
propertyType = Object.class;
|
69 |
|
|
70 | 351 |
String fieldName = "_$" + name;
|
71 |
|
|
72 | 351 |
op.claimProperty(name); |
73 |
|
|
74 | 351 |
Object injectedValue = _provider.obtainValue(objectReference, is.getLocation()); |
75 |
|
|
76 | 351 |
if (injectedValue == null) |
77 | 1 |
throw new ApplicationRuntimeException(EnhanceMessages |
78 |
.locatedValueIsNull(objectReference)); |
|
79 |
|
|
80 | 350 |
if (!propertyType.isAssignableFrom(injectedValue.getClass()))
|
81 | 1 |
throw new ApplicationRuntimeException(EnhanceMessages.incompatibleInjectType( |
82 |
objectReference, |
|
83 |
injectedValue, |
|
84 |
propertyType)); |
|
85 |
|
|
86 | 349 |
op.addField(fieldName, propertyType, injectedValue); |
87 |
|
|
88 | 349 |
String methodName = EnhanceUtils.createAccessorMethodName(name); |
89 |
|
|
90 | 349 |
op.addMethod( |
91 |
Modifier.PUBLIC, |
|
92 |
new MethodSignature(propertyType, methodName, null, null), |
|
93 |
"return " + fieldName + ";"); |
|
94 |
} |
|
95 |
|
|
96 | 47 |
public void setErrorLog(ErrorLog errorLog) |
97 |
{ |
|
98 | 47 |
_errorLog = errorLog; |
99 |
} |
|
100 |
|
|
101 | 49 |
public void setProvider(InjectedValueProvider provider) |
102 |
{ |
|
103 | 49 |
_provider = provider; |
104 |
} |
|
105 |
} |
|