|
|||||||||||||||||||
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 |
import java.io.Serializable;
|
|
19 |
|
|
20 |
/**
|
|
21 |
* A ConstantParameter should be used to pass in "constant" arguments
|
|
22 |
* to constructors. This includes {@link String}s, {@link Integer}s or
|
|
23 |
* any other object that is not registered in the container.
|
|
24 |
*
|
|
25 |
* @author Jon Tirsén
|
|
26 |
* @author Aslak Hellesøy
|
|
27 |
* @author Jörg Schaible
|
|
28 |
* @version $Revision: 1.19 $
|
|
29 |
*/
|
|
30 |
public class ConstantParameter implements Parameter, Serializable { |
|
31 |
private final Object value;
|
|
32 |
|
|
33 | 47 |
public ConstantParameter(Object value) {
|
34 | 47 |
this.value = value;
|
35 |
} |
|
36 |
|
|
37 | 69 |
public ComponentAdapter resolveAdapter(PicoContainer picoContainer, Class expectedType) throws AssignabilityRegistrationException, NotConcreteRegistrationException { |
38 | 69 |
ComponentAdapter result = null;
|
39 | 69 |
if (expectedType.isAssignableFrom(value.getClass())) {
|
40 | 32 |
result = new InstanceComponentAdapter(uniqueishKey(), value);
|
41 | 37 |
} else if (expectedType.isPrimitive()) { |
42 | 20 |
try {
|
43 | 20 |
Field field = value.getClass().getField("TYPE");
|
44 | 19 |
Class type = (Class) field.get(value); |
45 | 19 |
if (expectedType.isAssignableFrom(type)) {
|
46 | 11 |
result = new InstanceComponentAdapter(uniqueishKey(), value);
|
47 |
} else {
|
|
48 | 8 |
result = null;
|
49 |
} |
|
50 |
} catch (NoSuchFieldException e) {
|
|
51 | 1 |
result = null;
|
52 |
} catch (IllegalArgumentException e) {
|
|
53 | 0 |
result = null;
|
54 |
} catch (IllegalAccessException e) {
|
|
55 | 0 |
result = null;
|
56 |
} catch (ClassCastException e) {
|
|
57 | 0 |
result = null;
|
58 |
} |
|
59 |
} |
|
60 | 69 |
return result;
|
61 |
} |
|
62 |
|
|
63 | 43 |
private Integer uniqueishKey() {
|
64 | 43 |
return new Integer(System.identityHashCode(value)); |
65 |
} |
|
66 |
|
|
67 |
} |
|
68 |
|
|