|
|||||||||||||||||||
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 | |||||||||||||||
ComponentParameter.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 |
* Original code by *
|
|
9 |
*****************************************************************************/
|
|
10 |
package org.picocontainer.defaults;
|
|
11 |
|
|
12 |
import org.picocontainer.ComponentAdapter;
|
|
13 |
import org.picocontainer.Parameter;
|
|
14 |
import org.picocontainer.PicoContainer;
|
|
15 |
import org.picocontainer.PicoIntrospectionException;
|
|
16 |
|
|
17 |
import java.io.Serializable;
|
|
18 |
|
|
19 |
/**
|
|
20 |
* A ComponentParameter should be used to pass in a particular component
|
|
21 |
* as argument to a different component's constructor. This is particularly
|
|
22 |
* useful in cases where several components of the same type have been registered,
|
|
23 |
* but with a different key. Passing a ComponentParameter as a parameter
|
|
24 |
* when registering a component will give PicoContainer a hint about what
|
|
25 |
* other component to use in the constructor.
|
|
26 |
*
|
|
27 |
* @author Jon Tirsén
|
|
28 |
* @author Aslak Hellesøy
|
|
29 |
* @version $Revision: 1.18 $
|
|
30 |
*/
|
|
31 |
public class ComponentParameter implements Parameter, Serializable { |
|
32 |
|
|
33 |
private Object componentKey;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* Expect a parameter matching a component of a specific key.
|
|
37 |
*
|
|
38 |
* @param componentKey the key of the desired component
|
|
39 |
*/
|
|
40 | 8 |
public ComponentParameter(Object componentKey) {
|
41 | 8 |
this.componentKey = componentKey;
|
42 |
} |
|
43 |
|
|
44 |
/**
|
|
45 |
* Expect any paramter of the appropriate type.
|
|
46 |
*/
|
|
47 | 539 |
public ComponentParameter() {
|
48 |
} |
|
49 |
|
|
50 | 577 |
public ComponentAdapter resolveAdapter(PicoContainer picoContainer, Class expectedType) throws PicoIntrospectionException { |
51 | 577 |
ComponentAdapter result; |
52 | 577 |
if (componentKey != null) { |
53 | 18 |
result = picoContainer.getComponentAdapter(componentKey); |
54 | 18 |
if (result != null && !expectedType.isAssignableFrom(result.getComponentImplementation())) { |
55 |
// found one, but it wasn't of the expected type
|
|
56 | 9 |
result = null;
|
57 |
} |
|
58 |
} else {
|
|
59 | 559 |
result = picoContainer.getComponentAdapterOfType(expectedType); |
60 |
} |
|
61 | 575 |
return result;
|
62 |
} |
|
63 |
|
|
64 |
} |
|
65 |
|
|