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