|
|||||||||||||||||||
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% | 100% | 100% | 100% |
|
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 |
import org.picocontainer.PicoException;
|
|
17 |
import org.picocontainer.PicoIntrospectionException;
|
|
18 |
import org.picocontainer.PicoVisitor;
|
|
19 |
|
|
20 |
import java.io.Serializable;
|
|
21 |
import java.lang.reflect.Field;
|
|
22 |
|
|
23 |
|
|
24 |
/**
|
|
25 |
* A ConstantParameter should be used to pass in "constant" arguments to constructors. This
|
|
26 |
* includes {@link String}s,{@link Integer}s or any other object that is not registered in
|
|
27 |
* the container.
|
|
28 |
*
|
|
29 |
* @author Jon Tirsén
|
|
30 |
* @author Aslak Hellesøy
|
|
31 |
* @author Jörg Schaible
|
|
32 |
* @author Thomas Heller
|
|
33 |
* @version $Revision: 1.24 $
|
|
34 |
*/
|
|
35 |
public class ConstantParameter |
|
36 |
implements Parameter, Serializable {
|
|
37 |
|
|
38 |
private final Object value;
|
|
39 |
|
|
40 | 208 |
public ConstantParameter(Object value) {
|
41 | 208 |
this.value = value;
|
42 |
} |
|
43 |
|
|
44 | 158 |
public Object resolveInstance(PicoContainer container, ComponentAdapter adapter, Class expectedType) {
|
45 | 158 |
return value;
|
46 |
} |
|
47 |
|
|
48 | 178 |
public boolean isResolvable(PicoContainer container, ComponentAdapter adapter, Class expectedType) { |
49 | 178 |
try {
|
50 | 178 |
verify(container, adapter, expectedType); |
51 | 130 |
return true; |
52 |
} catch(final PicoIntrospectionException e) {
|
|
53 | 48 |
return false; |
54 |
} |
|
55 |
} |
|
56 |
|
|
57 |
/**
|
|
58 |
* {@inheritDoc}
|
|
59 |
*
|
|
60 |
* @see org.picocontainer.Parameter#verify(org.picocontainer.PicoContainer,
|
|
61 |
* org.picocontainer.ComponentAdapter, java.lang.Class)
|
|
62 |
*/
|
|
63 | 186 |
public void verify(PicoContainer container, ComponentAdapter adapter, Class expectedType) throws PicoException { |
64 | 186 |
if (!checkPrimitive(expectedType) && !expectedType.isInstance(value)) {
|
65 | 48 |
throw new PicoIntrospectionException(expectedType.getClass().getName() |
66 |
+ " is not assignable from "
|
|
67 |
+ value.getClass().getName()); |
|
68 |
} |
|
69 |
} |
|
70 |
|
|
71 |
/**
|
|
72 |
* Visit the current {@link Parameter}.
|
|
73 |
*
|
|
74 |
* @see org.picocontainer.Parameter#accept(org.picocontainer.PicoVisitor)
|
|
75 |
*/
|
|
76 | 26 |
public void accept(final PicoVisitor visitor) { |
77 | 26 |
visitor.visitParameter(this);
|
78 |
} |
|
79 |
|
|
80 | 186 |
private boolean checkPrimitive(Class expectedType) { |
81 | 186 |
try {
|
82 | 186 |
if (expectedType.isPrimitive()) {
|
83 | 28 |
final Field field = value.getClass().getField("TYPE");
|
84 | 26 |
final Class type = (Class) field.get(value); |
85 | 26 |
return expectedType.isAssignableFrom(type);
|
86 |
} |
|
87 |
} catch (NoSuchFieldException e) {
|
|
88 |
} catch (IllegalAccessException e) {
|
|
89 |
} |
|
90 | 160 |
return false; |
91 |
} |
|
92 |
|
|
93 |
} |
|
94 |
|
|