|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| package org.objectledge.pico; |
|
30 |
| |
|
31 |
| import java.lang.reflect.Array; |
|
32 |
| import java.lang.reflect.Constructor; |
|
33 |
| import java.util.Arrays; |
|
34 |
| import java.util.Collection; |
|
35 |
| |
|
36 |
| import org.picocontainer.ComponentAdapter; |
|
37 |
| import org.picocontainer.Parameter; |
|
38 |
| import org.picocontainer.PicoContainer; |
|
39 |
| import org.picocontainer.PicoException; |
|
40 |
| import org.picocontainer.PicoInitializationException; |
|
41 |
| import org.picocontainer.PicoIntrospectionException; |
|
42 |
| import org.picocontainer.PicoVisitor; |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| public class SequenceParameter implements Parameter |
|
57 |
| { |
|
58 |
| private Parameter[] elements; |
|
59 |
| private Class implClass; |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
860
| public SequenceParameter(Parameter[] elements, Class implClass)
|
|
68 |
| { |
|
69 |
860
| this.elements = elements;
|
|
70 |
860
| this.implClass = implClass;
|
|
71 |
| } |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
860
| public Object resolveInstance(PicoContainer container, ComponentAdapter adapter,
|
|
77 |
| Class expectedType) |
|
78 |
| throws PicoInitializationException |
|
79 |
| { |
|
80 |
860
| Class elementType = getElementType(expectedType);
|
|
81 |
860
| Object[] items = new Object[elements.length];
|
|
82 |
860
| for(int i = 0; i < elements.length; i++)
|
|
83 |
| { |
|
84 |
1978
| items[i] = elements[i].resolveInstance(container, adapter, elementType);
|
|
85 |
| } |
|
86 |
860
| if(expectedType.getComponentType() != null)
|
|
87 |
| { |
|
88 |
774
| return toArray(items, expectedType);
|
|
89 |
| } |
|
90 |
86
| if(Collection.class.isAssignableFrom(expectedType))
|
|
91 |
| { |
|
92 |
86
| if(implClass != null)
|
|
93 |
| { |
|
94 |
86
| return toCollection(items, implClass);
|
|
95 |
| } |
|
96 |
| else |
|
97 |
| { |
|
98 |
0
| return toCollection(items, expectedType);
|
|
99 |
| } |
|
100 |
| } |
|
101 |
0
| throw new PicoIntrospectionException("not a collection nor array type");
|
|
102 |
| } |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
1118
| public boolean isResolvable(PicoContainer container, ComponentAdapter adapter,
|
|
108 |
| Class expectedType) |
|
109 |
| { |
|
110 |
1118
| try
|
|
111 |
| { |
|
112 |
1118
| Class elementType = getElementType(expectedType);
|
|
113 |
1032
| for(Parameter element : elements)
|
|
114 |
| { |
|
115 |
2150
| if(!element.isResolvable(container, adapter, elementType))
|
|
116 |
| { |
|
117 |
172
| return false;
|
|
118 |
| } |
|
119 |
| } |
|
120 |
860
| return true;
|
|
121 |
| } |
|
122 |
| catch(PicoIntrospectionException e) |
|
123 |
| { |
|
124 |
| |
|
125 |
86
| return false;
|
|
126 |
| } |
|
127 |
| } |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
0
| public void verify(PicoContainer container, ComponentAdapter adapter, Class expectedType)
|
|
133 |
| throws PicoIntrospectionException |
|
134 |
| { |
|
135 |
0
| Class elementType = getElementType(expectedType);
|
|
136 |
0
| for(Parameter element : elements)
|
|
137 |
| { |
|
138 |
0
| element.verify(container, adapter, expectedType);
|
|
139 |
| } |
|
140 |
| } |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
0
| public void accept(PicoVisitor visitor)
|
|
146 |
| { |
|
147 |
0
| visitor.visitParameter(this);
|
|
148 |
0
| for(Parameter element : elements)
|
|
149 |
| { |
|
150 |
0
| visitor.visitParameter(element);
|
|
151 |
| } |
|
152 |
| } |
|
153 |
| |
|
154 |
1978
| private Class getElementType(Class expectedType)
|
|
155 |
| { |
|
156 |
1978
| if(expectedType.getComponentType() != null)
|
|
157 |
| { |
|
158 |
1720
| return expectedType.getComponentType();
|
|
159 |
| } |
|
160 |
258
| if(Collection.class.isAssignableFrom(expectedType))
|
|
161 |
| { |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
172
| return Object.class;
|
|
167 |
| } |
|
168 |
86
| throw new PicoIntrospectionException("not a collection nor array type");
|
|
169 |
| } |
|
170 |
| |
|
171 |
774
| private Object toArray(Object[] source, Class targetType)
|
|
172 |
| { |
|
173 |
774
| Object target = Array.newInstance(targetType.getComponentType(), source.length);
|
|
174 |
774
| System.arraycopy(source, 0, target, 0, source.length);
|
|
175 |
774
| return target;
|
|
176 |
| } |
|
177 |
| |
|
178 |
86
| private Object toCollection(Object[] source, Class targetType)
|
|
179 |
| { |
|
180 |
86
| Collection target;
|
|
181 |
86
| try
|
|
182 |
| { |
|
183 |
86
| try
|
|
184 |
| { |
|
185 |
86
| Constructor ctor = targetType.getConstructor(new Class[] { Integer.TYPE });
|
|
186 |
86
| target = (Collection)ctor.newInstance(new Object[] { new Integer(source.length) });
|
|
187 |
| } |
|
188 |
| catch(NoSuchMethodException e) |
|
189 |
| { |
|
190 |
0
| try
|
|
191 |
| { |
|
192 |
0
| Constructor ctor = targetType.getConstructor(new Class[0]);
|
|
193 |
0
| target = (Collection)ctor.newInstance(new Object[0]);
|
|
194 |
| } |
|
195 |
| catch(NoSuchMethodException ee) |
|
196 |
| { |
|
197 |
0
| throw new CollectionInstantiationException("cannot instantiate Collection "
|
|
198 |
| + targetType.getName() + " no supported constructors found", ee); |
|
199 |
| } |
|
200 |
| } |
|
201 |
| } |
|
202 |
| catch(Exception e) |
|
203 |
| { |
|
204 |
0
| if(e instanceof PicoException)
|
|
205 |
| { |
|
206 |
0
| throw (PicoException)e;
|
|
207 |
| } |
|
208 |
| else |
|
209 |
| { |
|
210 |
0
| throw new CollectionInstantiationException("cannot instantiate Collection "
|
|
211 |
| + targetType.getName(), e); |
|
212 |
| } |
|
213 |
| } |
|
214 |
86
| target.addAll(Arrays.asList(source));
|
|
215 |
86
| return target;
|
|
216 |
| } |
|
217 |
| } |