1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.apache.commons.clazz.reflect.common; |
17 | |
|
18 | |
import java.lang.reflect.Array; |
19 | |
import java.lang.reflect.InvocationTargetException; |
20 | |
import java.lang.reflect.Method; |
21 | |
import java.util.AbstractList; |
22 | |
import java.util.ArrayList; |
23 | |
import java.util.Iterator; |
24 | |
import java.util.List; |
25 | |
import java.util.ListIterator; |
26 | |
|
27 | |
import org.apache.commons.clazz.ClazzAccessException; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | 0 | public class ReflectedList extends AbstractList { |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
private Object instance; |
59 | |
private ReflectedListProperty property; |
60 | |
|
61 | 0 | private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | 0 | public ReflectedList(Object instance, ReflectedListProperty property) { |
67 | 0 | this.instance = instance; |
68 | 0 | this.property = property; |
69 | 0 | } |
70 | |
|
71 | |
public Object getPropertyValue() { |
72 | 0 | Method readMethod = property.getReadMethod(); |
73 | 0 | if (readMethod == null) { |
74 | 0 | throw new ClazzAccessException( |
75 | |
"Cannot read property " |
76 | |
+ property.getName() |
77 | |
+ ": no read method"); |
78 | |
} |
79 | |
try { |
80 | 0 | return readMethod.invoke(instance, null); |
81 | |
} |
82 | 0 | catch (Exception ex) { |
83 | 0 | throw accessException("Cannot read property", readMethod, ex); |
84 | |
} |
85 | |
} |
86 | |
|
87 | |
public void setPropertyValue(Object value) { |
88 | 0 | Method writeMethod = property.getWriteMethod(); |
89 | 0 | if (writeMethod == null) { |
90 | 0 | throw new ClazzAccessException( |
91 | |
"Cannot set property: " |
92 | |
+ property.getName() |
93 | |
+ ": no set(array) method"); |
94 | |
} |
95 | |
|
96 | |
try { |
97 | 0 | writeMethod.invoke(instance, new Object[] { value }); |
98 | |
} |
99 | 0 | catch (Exception ex) { |
100 | 0 | throw accessException("Cannot set property", writeMethod, ex); |
101 | 0 | } |
102 | 0 | } |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public int size() { |
108 | 0 | Method sizeMethod = property.getSizeMethod(); |
109 | 0 | if (sizeMethod != null) { |
110 | |
try { |
111 | 0 | Object value = sizeMethod.invoke(instance, null); |
112 | 0 | return ((Integer) value).intValue(); |
113 | |
} |
114 | 0 | catch (Exception ex) { |
115 | 0 | throw accessException("Cannot get list size", sizeMethod, ex); |
116 | |
} |
117 | |
} |
118 | |
else { |
119 | 0 | Object list = getPropertyValue(); |
120 | 0 | if (list == null) { |
121 | 0 | return 0; |
122 | |
} |
123 | |
|
124 | 0 | if (list instanceof List) { |
125 | 0 | return ((List) list).size(); |
126 | |
} |
127 | |
|
128 | 0 | return Array.getLength(list); |
129 | |
} |
130 | |
} |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public Object get(int index) { |
136 | 0 | Method getMethod = property.getGetMethod(); |
137 | 0 | if (getMethod != null) { |
138 | |
Object value; |
139 | |
try { |
140 | 0 | value = |
141 | |
getMethod.invoke( |
142 | |
instance, |
143 | |
new Object[] { new Integer(index)}); |
144 | |
} |
145 | 0 | catch (Throwable ex) { |
146 | 0 | throw accessException("Cannot get property", getMethod, ex); |
147 | 0 | } |
148 | 0 | return value; |
149 | |
} |
150 | |
else { |
151 | 0 | Object list = getPropertyValue(); |
152 | 0 | if (list == null) { |
153 | 0 | return null; |
154 | |
} |
155 | |
|
156 | 0 | if (list instanceof List) { |
157 | 0 | return ((List) list).get(index); |
158 | |
} |
159 | |
|
160 | 0 | return Array.get(list, index); |
161 | |
} |
162 | |
} |
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
public Iterator iterator() { |
168 | 0 | return new QuickList().iterator(); |
169 | |
} |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
public ListIterator listIterator() { |
175 | 0 | return new QuickList().listIterator(); |
176 | |
} |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
public ListIterator listIterator(int index) { |
182 | 0 | return new QuickList().listIterator(index); |
183 | |
} |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
public Object set(int index, Object element) { |
189 | 0 | modCount++; |
190 | 0 | Method setMethod = property.getSetMethod(); |
191 | 0 | if (setMethod != null) { |
192 | 0 | Object oldValue = null; |
193 | |
try { |
194 | 0 | oldValue = get(index); |
195 | |
} |
196 | 0 | catch (Throwable t) { |
197 | |
|
198 | 0 | } |
199 | |
try { |
200 | 0 | setMethod.invoke( |
201 | |
instance, |
202 | |
new Object[] { new Integer(index), element }); |
203 | |
} |
204 | 0 | catch (Exception ex) { |
205 | 0 | throw accessException( |
206 | |
"Cannot set property element", |
207 | |
setMethod, |
208 | |
ex); |
209 | 0 | } |
210 | 0 | return oldValue; |
211 | |
} |
212 | |
else { |
213 | 0 | Object list = getPropertyValue(); |
214 | 0 | if (list == null) { |
215 | 0 | return null; |
216 | |
} |
217 | |
|
218 | 0 | if (list instanceof List) { |
219 | 0 | return ((List) list).set(index, element); |
220 | |
} |
221 | |
|
222 | 0 | Object oldValue = Array.get(list, index); |
223 | 0 | Array.set(list, index, element); |
224 | 0 | return oldValue; |
225 | |
} |
226 | |
} |
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
public boolean add(Object element) { |
253 | 0 | modCount++; |
254 | 0 | if (property.getAddMethod() != null) { |
255 | 0 | Method addMethod = property.getAddMethod(); |
256 | |
try { |
257 | 0 | addMethod.invoke(instance, new Object[] { element }); |
258 | |
} |
259 | 0 | catch (Exception ex) { |
260 | 0 | throw accessException( |
261 | |
"Cannot add value to property", |
262 | |
addMethod, |
263 | |
ex); |
264 | 0 | } |
265 | 0 | } |
266 | |
else { |
267 | 0 | add(-1, element); |
268 | |
} |
269 | 0 | return true; |
270 | |
} |
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
public void add(int index, Object element) { |
296 | 0 | modCount++; |
297 | 0 | if (property.getAddIndexedMethod() != null) { |
298 | 0 | if (index == -1) { |
299 | |
|
300 | |
|
301 | 0 | index = size(); |
302 | |
} |
303 | 0 | Method addIndexedMethod = property.getAddIndexedMethod(); |
304 | |
try { |
305 | 0 | addIndexedMethod.invoke( |
306 | |
instance, |
307 | |
new Object[] { new Integer(index), element }); |
308 | |
} |
309 | 0 | catch (Exception ex) { |
310 | 0 | throw accessException( |
311 | |
"Cannot add value to property", |
312 | |
addIndexedMethod, |
313 | |
ex); |
314 | 0 | } |
315 | 0 | return; |
316 | |
} |
317 | |
|
318 | 0 | if (property.getAddMethod() != null && index == size()) { |
319 | |
|
320 | |
|
321 | 0 | add(element); |
322 | |
} |
323 | 0 | else if (property.getType().isArray()) { |
324 | 0 | addToArray(index, element); |
325 | |
} |
326 | |
else { |
327 | 0 | addToList(index, element); |
328 | |
} |
329 | 0 | } |
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
private void addToArray(int index, Object element) { |
335 | |
Object newList; |
336 | 0 | Object list = getPropertyValue(); |
337 | 0 | if (list == null) { |
338 | 0 | if (index != 0 && index != -1) { |
339 | 0 | throw new ArrayIndexOutOfBoundsException( |
340 | |
"Size: 0; Index: " + index); |
341 | |
} |
342 | 0 | Class contentType = property.getContentType(); |
343 | 0 | newList = Array.newInstance(contentType, 1); |
344 | 0 | Array.set(newList, 0, element); |
345 | 0 | } |
346 | |
else { |
347 | 0 | Class contentType = property.getContentType(); |
348 | 0 | int size = Array.getLength(list); |
349 | 0 | if (index == -1) { |
350 | 0 | index = size; |
351 | |
} |
352 | 0 | if (index < 0 || index > size) { |
353 | 0 | throw new ArrayIndexOutOfBoundsException( |
354 | |
"Size: " + size + "; Index: " + index); |
355 | |
} |
356 | 0 | newList = Array.newInstance(contentType, size + 1); |
357 | 0 | System.arraycopy(list, 0, newList, 0, index); |
358 | 0 | Array.set(newList, index, element); |
359 | 0 | System.arraycopy(list, index, newList, index + 1, size - index); |
360 | |
} |
361 | |
|
362 | 0 | setPropertyValue(newList); |
363 | 0 | } |
364 | |
|
365 | |
|
366 | |
|
367 | |
|
368 | |
private void addToList(int index, Object element) { |
369 | 0 | Object list = getPropertyValue(); |
370 | 0 | if (list == null) { |
371 | |
List newList; |
372 | 0 | Class type = property.getType(); |
373 | 0 | if (!type.isInterface()) { |
374 | |
try { |
375 | 0 | newList = (List) type.newInstance(); |
376 | |
} |
377 | 0 | catch (Exception ex) { |
378 | 0 | throw new ClazzAccessException( |
379 | |
"Cannot add value to property : " |
380 | |
+ property.getName() |
381 | |
+ ": cannot create List of type " |
382 | |
+ type.getName(), |
383 | |
ex); |
384 | 0 | } |
385 | |
} |
386 | |
else { |
387 | 0 | newList = new ArrayList(); |
388 | |
} |
389 | 0 | newList.add(element); |
390 | |
|
391 | 0 | setPropertyValue(newList); |
392 | 0 | } |
393 | 0 | else if (index == -1) { |
394 | 0 | ((List) list).add(element); |
395 | |
} |
396 | |
else { |
397 | 0 | ((List) list).add(index, element); |
398 | |
} |
399 | 0 | } |
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
public boolean remove(Object element) { |
417 | 0 | modCount++; |
418 | 0 | if (property.getRemoveMethod() != null) { |
419 | 0 | Method removeMethod = property.getRemoveMethod(); |
420 | |
try { |
421 | 0 | removeMethod.invoke(instance, new Object[] { element }); |
422 | |
} |
423 | 0 | catch (Exception ex) { |
424 | 0 | throw accessException( |
425 | |
"Cannot remove value from property", |
426 | |
removeMethod, |
427 | |
ex); |
428 | 0 | } |
429 | 0 | return true; |
430 | |
} |
431 | |
else { |
432 | 0 | return super.remove(element); |
433 | |
} |
434 | |
} |
435 | |
|
436 | |
|
437 | |
|
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
|
459 | |
|
460 | |
public Object remove(int index) { |
461 | 0 | modCount++; |
462 | |
|
463 | 0 | if (property.getRemoveIndexedMethod() != null) { |
464 | 0 | Object value = null; |
465 | |
|
466 | |
try { |
467 | 0 | value = get(index); |
468 | |
} |
469 | 0 | catch (Throwable t) { |
470 | |
|
471 | 0 | } |
472 | |
|
473 | 0 | Method removeIndexedMethod = property.getRemoveIndexedMethod(); |
474 | |
try { |
475 | 0 | removeIndexedMethod.invoke( |
476 | |
instance, |
477 | |
new Object[] { new Integer(index)}); |
478 | |
} |
479 | 0 | catch (Exception ex) { |
480 | 0 | throw accessException( |
481 | |
"Cannot remove value from property", |
482 | |
removeIndexedMethod, |
483 | |
ex); |
484 | 0 | } |
485 | |
|
486 | 0 | return value; |
487 | |
} |
488 | |
|
489 | 0 | Object list = getPropertyValue(); |
490 | 0 | if (list == null) { |
491 | 0 | throw new ArrayIndexOutOfBoundsException( |
492 | |
"Size: 0; Index: " + index); |
493 | |
} |
494 | 0 | else if (property.getType().isArray()) { |
495 | |
Object value; |
496 | 0 | Class contentType = property.getContentType(); |
497 | 0 | int size = Array.getLength(list); |
498 | 0 | if (index < 0 || index >= size) { |
499 | 0 | throw new ArrayIndexOutOfBoundsException( |
500 | |
"Size: " + size + "; Index: " + index); |
501 | |
} |
502 | 0 | value = Array.get(list, index); |
503 | |
|
504 | 0 | Object newList = Array.newInstance(contentType, size - 1); |
505 | 0 | System.arraycopy(list, 0, newList, 0, index); |
506 | 0 | System.arraycopy(list, index + 1, newList, index, size - index - 1); |
507 | 0 | setPropertyValue(newList); |
508 | 0 | return value; |
509 | |
} |
510 | |
else { |
511 | 0 | return ((List) list).remove(index); |
512 | |
} |
513 | |
} |
514 | |
|
515 | |
private RuntimeException accessException( |
516 | |
String message, |
517 | |
Method method, |
518 | |
Throwable ex) |
519 | |
{ |
520 | 0 | if (ex instanceof InvocationTargetException) { |
521 | 0 | ex = ((InvocationTargetException) ex).getTargetException(); |
522 | |
} |
523 | |
|
524 | |
|
525 | |
|
526 | 0 | if (ex instanceof RuntimeException) { |
527 | 0 | throw (RuntimeException) ex; |
528 | |
} |
529 | 0 | if (ex instanceof Error) { |
530 | 0 | throw (Error) ex; |
531 | |
} |
532 | |
|
533 | 0 | throw new ClazzAccessException( |
534 | |
message |
535 | |
+ ": " |
536 | |
+ property.getName() |
537 | |
+ ": cannot invoke method: " |
538 | |
+ method.getName(), |
539 | |
ex); |
540 | |
} |
541 | |
|
542 | |
private int getModCount() { |
543 | 0 | return modCount; |
544 | |
} |
545 | |
|
546 | |
|
547 | |
|
548 | |
|
549 | |
|
550 | |
|
551 | |
|
552 | |
|
553 | |
|
554 | |
|
555 | |
|
556 | |
|
557 | |
|
558 | |
|
559 | |
private class QuickList extends AbstractList { |
560 | |
private Object list; |
561 | |
private int size; |
562 | |
|
563 | 0 | public QuickList() { |
564 | 0 | update(); |
565 | 0 | } |
566 | |
|
567 | |
public void refresh() { |
568 | |
|
569 | |
|
570 | 0 | if (super.modCount != getModCount()) { |
571 | 0 | update(); |
572 | |
} |
573 | 0 | } |
574 | |
|
575 | |
public void update() { |
576 | |
|
577 | |
|
578 | 0 | super.modCount = getModCount(); |
579 | |
|
580 | 0 | if (property.getReadMethod() != null) { |
581 | 0 | list = getPropertyValue(); |
582 | 0 | if (list == null) { |
583 | 0 | size = 0; |
584 | |
} |
585 | 0 | else if (list instanceof List) { |
586 | 0 | size = ((List) list).size(); |
587 | |
} |
588 | |
else { |
589 | 0 | size = Array.getLength(list); |
590 | |
} |
591 | |
} |
592 | |
else { |
593 | 0 | list = NOT_ACCESSIBLE; |
594 | 0 | size = ReflectedList.this.size(); |
595 | |
} |
596 | 0 | } |
597 | |
|
598 | |
public int size() { |
599 | 0 | refresh(); |
600 | 0 | return size; |
601 | |
} |
602 | |
|
603 | |
public Object get(int index) { |
604 | 0 | refresh(); |
605 | 0 | if (list != NOT_ACCESSIBLE) { |
606 | 0 | if (list == null) { |
607 | 0 | throw new IndexOutOfBoundsException( |
608 | |
"Size= 0; index=" + index); |
609 | |
} |
610 | |
|
611 | 0 | if (list instanceof List) { |
612 | 0 | return ((List) list).get(index); |
613 | |
} |
614 | |
|
615 | 0 | return Array.get(list, index); |
616 | |
} |
617 | |
else { |
618 | 0 | return ReflectedList.this.get(index); |
619 | |
} |
620 | |
} |
621 | |
|
622 | |
public Object set(int index, Object value) { |
623 | 0 | return ReflectedList.this.set(index, value); |
624 | |
} |
625 | |
|
626 | |
public void add(int index, Object value) { |
627 | 0 | ReflectedList.this.add(index, value); |
628 | 0 | } |
629 | |
|
630 | |
public Object remove(int index) { |
631 | 0 | return ReflectedList.this.remove(index); |
632 | |
} |
633 | |
|
634 | |
} |
635 | |
|
636 | 0 | private static final Object NOT_ACCESSIBLE = new Object(); |
637 | |
} |