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