1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
package org.picocontainer.defaults;
|
12
|
|
|
13
|
|
import org.picocontainer.ComponentAdapter;
|
14
|
|
import org.picocontainer.Parameter;
|
15
|
|
import org.picocontainer.PicoInitializationException;
|
16
|
|
import org.picocontainer.PicoIntrospectionException;
|
17
|
|
|
18
|
|
import java.lang.reflect.Array;
|
19
|
|
import java.lang.reflect.Constructor;
|
20
|
|
import java.lang.reflect.InvocationTargetException;
|
21
|
|
import java.util.ArrayList;
|
22
|
|
import java.util.Arrays;
|
23
|
|
import java.util.Collections;
|
24
|
|
import java.util.Comparator;
|
25
|
|
import java.util.HashSet;
|
26
|
|
import java.util.List;
|
27
|
|
import java.util.Set;
|
28
|
|
|
29
|
|
|
30
|
|
|
31
|
|
|
32
|
|
|
33
|
|
|
34
|
|
|
35
|
|
|
36
|
|
|
37
|
|
|
38
|
|
|
39
|
|
|
40
|
|
|
41
|
|
|
42
|
|
|
43
|
|
|
44
|
|
public class ConstructorInjectionComponentAdapter extends InstantiatingComponentAdapter {
|
45
|
|
private transient boolean instantiating;
|
46
|
|
private transient List sortedMatchingConstructors;
|
47
|
|
|
48
|
|
|
49
|
|
|
50
|
|
|
51
|
|
|
52
|
235
|
public ConstructorInjectionComponentAdapter(final Object componentKey,
|
53
|
|
final Class componentImplementation,
|
54
|
|
Parameter[] parameters) throws AssignabilityRegistrationException, NotConcreteRegistrationException {
|
55
|
235
|
super(componentKey, componentImplementation, parameters);
|
56
|
|
}
|
57
|
|
|
58
|
|
|
59
|
|
|
60
|
|
|
61
|
17
|
public ConstructorInjectionComponentAdapter(Object componentKey,
|
62
|
|
Class componentImplementation) throws AssignabilityRegistrationException, NotConcreteRegistrationException {
|
63
|
17
|
this(componentKey, componentImplementation, null);
|
64
|
|
}
|
65
|
|
|
66
|
220
|
protected Constructor getGreediestSatisifableConstructor(List adapterInstantiationOrderTrackingList) throws PicoIntrospectionException, UnsatisfiableDependenciesException, AmbiguousComponentResolutionException, AssignabilityRegistrationException, NotConcreteRegistrationException {
|
67
|
220
|
Constructor greediestConstructor = null;
|
68
|
220
|
final Set conflicts = new HashSet();
|
69
|
220
|
final Set unsatisfiableDependencyTypes = new HashSet();
|
70
|
220
|
if(sortedMatchingConstructors == null) {
|
71
|
192
|
sortedMatchingConstructors = getSortedMatchingConstructors();
|
72
|
|
}
|
73
|
220
|
for (int i = 0; i < sortedMatchingConstructors.size(); i++) {
|
74
|
427
|
List adapterDependencies = new ArrayList();
|
75
|
427
|
boolean failedDependency = false;
|
76
|
427
|
Constructor constructor = (Constructor) sortedMatchingConstructors.get(i);
|
77
|
427
|
Class[] parameterTypes = constructor.getParameterTypes();
|
78
|
427
|
Parameter[] currentParameters = parameters != null ? parameters : createDefaultParameters(parameterTypes);
|
79
|
|
|
80
|
|
|
81
|
|
|
82
|
427
|
for (int j = 0; j < currentParameters.length; j++) {
|
83
|
547
|
ComponentAdapter adapter = currentParameters[j].resolveAdapter(getContainer(), parameterTypes[j]);
|
84
|
545
|
if (adapter == null) {
|
85
|
|
|
86
|
341
|
if (parameterTypes[j].getComponentType() != null) {
|
87
|
115
|
ComponentAdapter genericCollectionComponentAdapter = getGenericCollectionComponentAdapter(parameterTypes[j].getComponentType());
|
88
|
115
|
if (genericCollectionComponentAdapter != null) {
|
89
|
6
|
genericCollectionComponentAdapter.setContainer(getContainer());
|
90
|
6
|
adapterDependencies.add(genericCollectionComponentAdapter);
|
91
|
|
} else {
|
92
|
109
|
failedDependency = true;
|
93
|
109
|
unsatisfiableDependencyTypes.add(Arrays.asList(parameterTypes));
|
94
|
|
}
|
95
|
|
} else {
|
96
|
226
|
failedDependency = true;
|
97
|
226
|
unsatisfiableDependencyTypes.add(Arrays.asList(parameterTypes));
|
98
|
|
}
|
99
|
|
} else {
|
100
|
|
|
101
|
204
|
if (adapter.equals(this)) {
|
102
|
36
|
failedDependency = true;
|
103
|
36
|
unsatisfiableDependencyTypes.add(Arrays.asList(parameterTypes));
|
104
|
168
|
} else if (getComponentKey().equals(adapter.getComponentKey())) {
|
105
|
11
|
failedDependency = true;
|
106
|
11
|
unsatisfiableDependencyTypes.add(Arrays.asList(parameterTypes));
|
107
|
|
} else {
|
108
|
157
|
adapterDependencies.add(adapter);
|
109
|
|
}
|
110
|
|
}
|
111
|
|
}
|
112
|
425
|
if (!failedDependency) {
|
113
|
210
|
if(conflicts.size() == 0 && greediestConstructor == null) {
|
114
|
203
|
greediestConstructor = constructor;
|
115
|
203
|
adapterInstantiationOrderTrackingList.addAll(adapterDependencies);
|
116
|
7
|
} else if (conflicts.size() == 0 && greediestConstructor.getParameterTypes().length > parameterTypes.length) {
|
117
|
|
|
118
|
5
|
break;
|
119
|
|
} else {
|
120
|
2
|
if (greediestConstructor != null) {
|
121
|
1
|
conflicts.add(greediestConstructor);
|
122
|
1
|
greediestConstructor = null;
|
123
|
|
}
|
124
|
2
|
conflicts.add(constructor);
|
125
|
2
|
adapterInstantiationOrderTrackingList.clear();
|
126
|
|
}
|
127
|
|
}
|
128
|
|
}
|
129
|
218
|
if (!conflicts.isEmpty()) {
|
130
|
1
|
throw new TooManySatisfiableConstructorsException(getComponentImplementation(), conflicts);
|
131
|
|
}
|
132
|
217
|
if (greediestConstructor == null && unsatisfiableDependencyTypes.size() > 0) {
|
133
|
13
|
throw new UnsatisfiableDependenciesException(this, unsatisfiableDependencyTypes);
|
134
|
|
}
|
135
|
204
|
if (greediestConstructor == null) {
|
136
|
|
|
137
|
2
|
final Set nonMatching = new HashSet();
|
138
|
2
|
final Constructor[] constructors = getComponentImplementation().getConstructors();
|
139
|
2
|
for (int i = 0; i < constructors.length; i++) {
|
140
|
1
|
if (!sortedMatchingConstructors.contains(constructors[i])) {
|
141
|
1
|
nonMatching.add(constructors[i]);
|
142
|
|
} else {
|
143
|
|
|
144
|
|
}
|
145
|
|
}
|
146
|
2
|
if (nonMatching.size() > 0) {
|
147
|
1
|
throw new PicoInitializationException("The specified parameters do not match any of the following constructors: " + nonMatching.toString() + " for '" + getComponentImplementation() + "'");
|
148
|
|
} else {
|
149
|
1
|
throw new PicoInitializationException("There are no public constructors for '" + getComponentImplementation() + "'");
|
150
|
|
}
|
151
|
|
}
|
152
|
202
|
return greediestConstructor;
|
153
|
|
}
|
154
|
|
|
155
|
115
|
private ComponentAdapter getGenericCollectionComponentAdapter(Class componentType) {
|
156
|
115
|
GenericCollectionComponentAdapter result = null;
|
157
|
|
|
158
|
115
|
if(getContainer().getComponentAdaptersOfType(componentType).size() == 0) {
|
159
|
109
|
return null;
|
160
|
|
} else {
|
161
|
6
|
Object componentKey = new Object[]{this, componentType};
|
162
|
6
|
return new GenericCollectionComponentAdapter(componentKey, null, componentType, Array.class);
|
163
|
|
}
|
164
|
|
}
|
165
|
|
|
166
|
|
|
167
|
|
|
168
|
|
|
169
|
|
|
170
|
|
|
171
|
|
|
172
|
|
|
173
|
|
|
174
|
|
|
175
|
|
|
176
|
|
|
177
|
|
|
178
|
|
|
179
|
|
|
180
|
|
|
181
|
|
|
182
|
|
|
183
|
|
|
184
|
|
|
185
|
|
|
186
|
|
|
187
|
|
|
188
|
205
|
protected Object instantiateComponent(List adapterInstantiationOrderTrackingList) throws PicoInitializationException, PicoIntrospectionException, AssignabilityRegistrationException, NotConcreteRegistrationException {
|
189
|
205
|
try {
|
190
|
205
|
Constructor constructor = getGreediestSatisifableConstructor(adapterInstantiationOrderTrackingList);
|
191
|
193
|
if (instantiating) {
|
192
|
3
|
throw new CyclicDependencyException(constructor.getParameterTypes());
|
193
|
|
}
|
194
|
190
|
instantiating = true;
|
195
|
190
|
Object[] parameters = getConstructorArguments(adapterInstantiationOrderTrackingList);
|
196
|
|
|
197
|
185
|
return constructor.newInstance(parameters);
|
198
|
|
} catch (InvocationTargetException e) {
|
199
|
4
|
if (e.getTargetException() instanceof RuntimeException) {
|
200
|
2
|
throw (RuntimeException) e.getTargetException();
|
201
|
2
|
} else if (e.getTargetException() instanceof Error) {
|
202
|
1
|
throw (Error) e.getTargetException();
|
203
|
|
}
|
204
|
1
|
throw new PicoInvocationTargetInitializationException(e.getTargetException());
|
205
|
|
} catch (InstantiationException e) {
|
206
|
|
|
207
|
0
|
throw new RuntimeException("Should never get here");
|
208
|
|
} catch (IllegalAccessException e) {
|
209
|
|
|
210
|
0
|
throw new RuntimeException("Should never get here");
|
211
|
|
} finally {
|
212
|
205
|
instantiating = false;
|
213
|
|
}
|
214
|
|
}
|
215
|
|
|
216
|
190
|
protected Object[] getConstructorArguments(List adapterDependencies) {
|
217
|
190
|
Object[] result = new Object[adapterDependencies.size()];
|
218
|
190
|
for (int i = 0; i < adapterDependencies.size(); i++) {
|
219
|
114
|
ComponentAdapter adapterDependency = (ComponentAdapter) adapterDependencies.get(i);
|
220
|
114
|
result[i] = adapterDependency.getComponentInstance();
|
221
|
|
}
|
222
|
185
|
return result;
|
223
|
|
}
|
224
|
|
|
225
|
192
|
private List getSortedMatchingConstructors() {
|
226
|
192
|
List matchingConstructors = new ArrayList();
|
227
|
192
|
Constructor[] allConstructors = getComponentImplementation().getConstructors();
|
228
|
|
|
229
|
192
|
for (int i = 0; i < allConstructors.length; i++) {
|
230
|
333
|
Constructor constructor = allConstructors[i];
|
231
|
333
|
if (parameters == null || constructor.getParameterTypes().length == parameters.length) {
|
232
|
303
|
matchingConstructors.add(constructor);
|
233
|
|
}
|
234
|
|
}
|
235
|
|
|
236
|
192
|
if (parameters == null) {
|
237
|
171
|
Collections.sort(matchingConstructors, new Comparator() {
|
238
|
161
|
public int compare(Object arg0, Object arg1) {
|
239
|
161
|
return ((Constructor)arg1).getParameterTypes().length - ((Constructor)arg0).getParameterTypes().length;
|
240
|
|
}
|
241
|
|
});
|
242
|
|
}
|
243
|
192
|
return matchingConstructors;
|
244
|
|
}
|
245
|
|
}
|
246
|
|
|