|
|||||||||||||||||||
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 | |||||||||||||||
ConstantParameter.java | 100% | 82.4% | 100% | 88.5% |
|
1 |
/*****************************************************************************
|
|
2 |
* Copyright (c) PicoContainer Organization. All rights reserved. *
|
|
3 |
* ------------------------------------------------------------------------- *
|
|
4 |
* The software in this package is published under the terms of the BSD *
|
|
5 |
* style license a copy of which has been included with this distribution in *
|
|
6 |
* the LICENSE.txt file. *
|
|
7 |
* *
|
|
8 |
* Idea by Rachel Davies, Original code by Jon Tirsen *
|
|
9 |
*****************************************************************************/
|
|
10 |
|
|
11 |
package org.picocontainer.defaults;
|
|
12 |
|
|
13 |
import org.picocontainer.ComponentAdapter;
|
|
14 |
import org.picocontainer.Parameter;
|
|
15 |
import org.picocontainer.PicoContainer;
|
|
16 |
|
|
17 |
import java.lang.reflect.Field;
|
|
18 |
|
|
19 |
/**
|
|
20 |
* A ConstantParameter should be used to pass in "constant" arguments
|
|
21 |
* to constructors. This includes {@link String}s, {@link Integer}s or
|
|
22 |
* any other object that is not registered in the container.
|
|
23 |
*
|
|
24 |
* @author Jon Tirsén
|
|
25 |
* @author Aslak Hellesøy
|
|
26 |
* @author Jörg Schaible
|
|
27 |
* @version $Revision: 1.18 $
|
|
28 |
*/
|
|
29 |
public class ConstantParameter implements Parameter { |
|
30 |
private final Object value;
|
|
31 |
|
|
32 | 45 |
public ConstantParameter(Object value) {
|
33 | 45 |
this.value = value;
|
34 |
} |
|
35 |
|
|
36 | 69 |
public ComponentAdapter resolveAdapter(PicoContainer picoContainer, Class expectedType) throws AssignabilityRegistrationException, NotConcreteRegistrationException { |
37 | 69 |
ComponentAdapter result = null;
|
38 | 69 |
if (expectedType.isAssignableFrom(value.getClass())) {
|
39 | 32 |
result = new InstanceComponentAdapter(uniqueishKey(), value);
|
40 | 37 |
} else if (expectedType.isPrimitive()) { |
41 | 20 |
try {
|
42 | 20 |
Field field = value.getClass().getField("TYPE");
|
43 | 19 |
Class type = (Class) field.get(value); |
44 | 19 |
if (expectedType.isAssignableFrom(type)) {
|
45 | 11 |
result = new InstanceComponentAdapter(uniqueishKey(), value);
|
46 |
} else {
|
|
47 | 8 |
result = null;
|
48 |
} |
|
49 |
} catch (NoSuchFieldException e) {
|
|
50 | 1 |
result = null;
|
51 |
} catch (IllegalArgumentException e) {
|
|
52 | 0 |
result = null;
|
53 |
} catch (IllegalAccessException e) {
|
|
54 | 0 |
result = null;
|
55 |
} catch (ClassCastException e) {
|
|
56 | 0 |
result = null;
|
57 |
} |
|
58 |
} |
|
59 | 69 |
return result;
|
60 |
} |
|
61 |
|
|
62 | 43 |
private Integer uniqueishKey() {
|
63 | 43 |
return new Integer(System.identityHashCode(value)); |
64 |
} |
|
65 |
|
|
66 |
} |
|
67 |
|
|