|
|||||||||||||||||||
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 | |||||||||||||||
InstantiatingComponentAdapter.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.PicoInitializationException;
|
|
16 |
import org.picocontainer.PicoIntrospectionException;
|
|
17 |
|
|
18 |
import java.lang.reflect.Constructor;
|
|
19 |
import java.util.ArrayList;
|
|
20 |
import java.util.Iterator;
|
|
21 |
import java.util.List;
|
|
22 |
|
|
23 |
/**
|
|
24 |
* This ComponentAdapter will instantiate a new object for each call to
|
|
25 |
* {@link org.picocontainer.ComponentAdapter#getComponentInstance()}. That means that
|
|
26 |
* when used with a PicoContainer, getComponentInstance will return a new
|
|
27 |
* object each time.
|
|
28 |
*
|
|
29 |
* @author Aslak Hellesøy
|
|
30 |
* @author Paul Hammant
|
|
31 |
* @version $Revision: 1.18 $
|
|
32 |
*/
|
|
33 |
public abstract class InstantiatingComponentAdapter extends AbstractComponentAdapter { |
|
34 |
private transient boolean verifying; |
|
35 |
protected Parameter[] parameters;
|
|
36 |
|
|
37 | 257 |
protected InstantiatingComponentAdapter(Object componentKey, Class componentImplementation, Parameter[] parameters) throws AssignabilityRegistrationException, NotConcreteRegistrationException { |
38 | 257 |
super(componentKey, componentImplementation);
|
39 | 257 |
this.parameters = parameters;
|
40 |
} |
|
41 |
|
|
42 | 224 |
public Object getComponentInstance()
|
43 |
throws PicoInitializationException, PicoIntrospectionException, AssignabilityRegistrationException, NotConcreteRegistrationException {
|
|
44 | 224 |
List adapterInstantiationOrderTrackingList = new ArrayList();
|
45 | 224 |
Object instance = instantiateComponent(adapterInstantiationOrderTrackingList); |
46 |
|
|
47 |
// Now, track the instantiation order
|
|
48 | 198 |
for (Iterator it = adapterInstantiationOrderTrackingList.iterator(); it.hasNext(); ) {
|
49 | 140 |
ComponentAdapter dependencyAdapter = (ComponentAdapter) it.next(); |
50 | 140 |
getContainer().addOrderedComponentAdapter(dependencyAdapter); |
51 |
} |
|
52 | 198 |
return instance;
|
53 |
} |
|
54 |
|
|
55 | 398 |
protected Parameter[] createDefaultParameters(Class[] parameters) {
|
56 | 398 |
Parameter[] componentParameters = new Parameter[parameters.length];
|
57 | 398 |
for (int i = 0; i < parameters.length; i++) { |
58 | 503 |
if(PicoContainer.class.isAssignableFrom(parameters[i])) { |
59 | 8 |
componentParameters[i] = new ConstantParameter(getContainer());
|
60 |
} else {
|
|
61 | 495 |
componentParameters[i] = new ComponentParameter();
|
62 |
} |
|
63 |
} |
|
64 | 398 |
return componentParameters;
|
65 |
} |
|
66 |
|
|
67 | 15 |
public void verify() throws UnsatisfiableDependenciesException { |
68 | 15 |
try {
|
69 | 15 |
List adapterDependencies = new ArrayList();
|
70 | 15 |
getGreediestSatisifableConstructor(adapterDependencies); |
71 | 9 |
if (verifying) {
|
72 | 1 |
throw new CyclicDependencyException(getDependencyTypes(adapterDependencies)); |
73 |
} |
|
74 | 8 |
verifying = true;
|
75 | 8 |
for (int i = 0; i < adapterDependencies.size(); i++) { |
76 | 5 |
ComponentAdapter adapterDependency = (ComponentAdapter) adapterDependencies.get(i); |
77 | 5 |
adapterDependency.verify(); |
78 |
} |
|
79 |
} finally {
|
|
80 | 15 |
verifying = false;
|
81 |
} |
|
82 |
} |
|
83 |
|
|
84 | 1 |
private Class[] getDependencyTypes(List adapterDependencies) {
|
85 | 1 |
Class[] result = new Class[adapterDependencies.size()];
|
86 | 1 |
for (int i = 0; i < adapterDependencies.size(); i++) { |
87 | 1 |
ComponentAdapter adapterDependency = (ComponentAdapter) adapterDependencies.get(i); |
88 | 1 |
result[i] = adapterDependency.getComponentImplementation(); |
89 |
} |
|
90 | 1 |
return result;
|
91 |
} |
|
92 |
|
|
93 |
/**
|
|
94 |
* Instantiate the object.
|
|
95 |
* @param adapterInstantiationOrderTrackingList This list is filled with the dependent adapters of the instance.
|
|
96 |
* @return Returns the new instance.
|
|
97 |
* @throws PicoInitializationException
|
|
98 |
* @throws PicoIntrospectionException
|
|
99 |
* @throws AssignabilityRegistrationException
|
|
100 |
* @throws NotConcreteRegistrationException
|
|
101 |
*/
|
|
102 |
protected abstract Object instantiateComponent(List adapterInstantiationOrderTrackingList) throws PicoInitializationException, PicoIntrospectionException, AssignabilityRegistrationException, NotConcreteRegistrationException; |
|
103 |
protected abstract Constructor getGreediestSatisifableConstructor(List adapterInstantiationOrderTrackingList) throws PicoIntrospectionException, UnsatisfiableDependenciesException, AmbiguousComponentResolutionException, AssignabilityRegistrationException, NotConcreteRegistrationException; |
|
104 |
} |
|
105 |
|
|