1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
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.PicoInstantiationException; |
16 |
| import org.picocontainer.PicoIntrospectionException; |
17 |
| import org.picocontainer.PicoVisitor; |
18 |
| |
19 |
| import java.io.Serializable; |
20 |
| import java.lang.reflect.Field; |
21 |
| import java.util.Arrays; |
22 |
| import java.util.HashSet; |
23 |
| import java.util.List; |
24 |
| import java.util.Iterator; |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| public class BasicComponentParameter |
41 |
| implements Parameter, Serializable { |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| public static final BasicComponentParameter BASIC_DEFAULT = new BasicComponentParameter(); |
47 |
| |
48 |
| private Object componentKey; |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
266
| public BasicComponentParameter(Object componentKey) {
|
56 |
266
| this.componentKey = componentKey;
|
57 |
| } |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
70
| public BasicComponentParameter() {
|
63 |
| } |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
1648
| public boolean isResolvable(PicoContainer container, ComponentAdapter adapter, Class expectedType) {
|
73 |
1648
| return resolveAdapter(container, adapter, expectedType) != null;
|
74 |
| } |
75 |
| |
76 |
464
| public Object resolveInstance(PicoContainer container, ComponentAdapter adapter, Class expectedType)
|
77 |
| throws PicoInstantiationException { |
78 |
464
| final ComponentAdapter componentAdapter = resolveAdapter(container, adapter, expectedType);
|
79 |
464
| if (componentAdapter != null) {
|
80 |
394
| return container.getComponentInstance(componentAdapter.getComponentKey());
|
81 |
| } |
82 |
70
| return null;
|
83 |
| } |
84 |
| |
85 |
16
| public void verify(PicoContainer container, ComponentAdapter adapter, Class expectedType) throws PicoIntrospectionException {
|
86 |
16
| final ComponentAdapter componentAdapter = resolveAdapter(container, adapter, expectedType);
|
87 |
16
| if (componentAdapter == null) {
|
88 |
2
| final HashSet set = new HashSet();
|
89 |
2
| set.add(Arrays.asList(new Class[] {expectedType}));
|
90 |
2
| throw new UnsatisfiableDependenciesException(adapter, set);
|
91 |
| } |
92 |
14
| componentAdapter.verify(container);
|
93 |
| } |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
24
| public void accept(final PicoVisitor visitor) {
|
101 |
24
| visitor.visitParameter(this);
|
102 |
| } |
103 |
| |
104 |
2128
| private ComponentAdapter resolveAdapter(PicoContainer container, ComponentAdapter adapter, Class expectedType) {
|
105 |
| |
106 |
2128
| final ComponentAdapter result = getTargetAdapter(container, expectedType,adapter);
|
107 |
2114
| if (result == null) {
|
108 |
1186
| return null;
|
109 |
| } |
110 |
| |
111 |
928
| if (!expectedType.isAssignableFrom(result.getComponentImplementation())) {
|
112 |
| |
113 |
20
| if (expectedType.isPrimitive()) {
|
114 |
6
| try {
|
115 |
6
| final Field field = result.getComponentImplementation().getField("TYPE");
|
116 |
4
| final Class type = (Class) field.get(result.getComponentInstance(null));
|
117 |
4
| if (expectedType.isAssignableFrom(type)) {
|
118 |
4
| return result;
|
119 |
| } |
120 |
| } catch (NoSuchFieldException e) { |
121 |
| } catch (IllegalArgumentException e) { |
122 |
| } catch (IllegalAccessException e) { |
123 |
| } catch (ClassCastException e) { |
124 |
| } |
125 |
| } |
126 |
16
| return null;
|
127 |
| } |
128 |
908
| return result;
|
129 |
| } |
130 |
| |
131 |
2128
| private ComponentAdapter getTargetAdapter(PicoContainer container, Class expectedType, ComponentAdapter excludeAdapter) {
|
132 |
2128
| if (componentKey != null) {
|
133 |
| |
134 |
54
| return container.getComponentAdapter(componentKey);
|
135 |
2074
| } else if(excludeAdapter == null) {
|
136 |
2
| return container.getComponentAdapterOfType(expectedType);
|
137 |
| } else { |
138 |
2072
| Object excludeKey = excludeAdapter.getComponentKey();
|
139 |
2072
| ComponentAdapter byKey = container.getComponentAdapter(expectedType);
|
140 |
2072
| if(byKey != null ) {
|
141 |
656
| if( byKey.getComponentKey().equals(excludeKey)) {
|
142 |
22
| return null;
|
143 |
| } |
144 |
634
| return byKey;
|
145 |
| } |
146 |
1416
| List found = container.getComponentAdaptersOfType(expectedType);
|
147 |
1416
| ComponentAdapter exclude = null;
|
148 |
1416
| ComponentAdapter work;
|
149 |
1416
| for(Iterator iterator = found.iterator(); iterator.hasNext();) {
|
150 |
324
| work = (ComponentAdapter) iterator.next();
|
151 |
324
| if( work.getComponentKey().equals(excludeKey)) {
|
152 |
94
| exclude = work;
|
153 |
| } |
154 |
| } |
155 |
1416
| found.remove(exclude);
|
156 |
1416
| if(found.size() == 0) {
|
157 |
1200
| if( container.getParent() != null) {
|
158 |
68
| return container.getParent().getComponentAdapterOfType(expectedType);
|
159 |
| } else { |
160 |
1132
| return null;
|
161 |
| } |
162 |
216
| } else if(found.size() == 1) {
|
163 |
202
| return (ComponentAdapter)found.get(0);
|
164 |
| } else { |
165 |
14
| Class[] foundClasses = new Class[found.size()];
|
166 |
14
| for (int i = 0; i < foundClasses.length; i++) {
|
167 |
28
| foundClasses[i] = ((ComponentAdapter) found.get(i)).getComponentImplementation();
|
168 |
| } |
169 |
14
| throw new AmbiguousComponentResolutionException(expectedType, foundClasses);
|
170 |
| } |
171 |
| } |
172 |
| } |
173 |
| } |