1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.util; |
12 | |
|
13 | |
import java.lang.reflect.Array; |
14 | |
|
15 | |
|
16 | 0 | public class ArrayUtils extends org.apache.commons.lang.ArrayUtils |
17 | |
{ |
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
public static String toString(Object array, int maxElements) |
24 | |
{ |
25 | |
String result; |
26 | |
|
27 | 14 | Class componentType = array.getClass().getComponentType(); |
28 | 16 | if (Object.class.isAssignableFrom(componentType)) |
29 | |
{ |
30 | 4 | result = ArrayUtils.toString((ArrayUtils.subarray((Object[]) array, 0, maxElements))); |
31 | |
} |
32 | 10 | else if (componentType.equals(Boolean.TYPE)) |
33 | |
{ |
34 | 0 | result = ArrayUtils.toString((ArrayUtils.subarray((boolean[]) array, 0, maxElements))); |
35 | |
} |
36 | 10 | else if (componentType.equals(Byte.TYPE)) |
37 | |
{ |
38 | 6 | result = ArrayUtils.toString((ArrayUtils.subarray((byte[]) array, 0, maxElements))); |
39 | |
} |
40 | 4 | else if (componentType.equals(Character.TYPE)) |
41 | |
{ |
42 | 0 | result = ArrayUtils.toString((ArrayUtils.subarray((char[]) array, 0, maxElements))); |
43 | |
} |
44 | 4 | else if (componentType.equals(Short.TYPE)) |
45 | |
{ |
46 | 0 | result = ArrayUtils.toString((ArrayUtils.subarray((short[]) array, 0, maxElements))); |
47 | |
} |
48 | 4 | else if (componentType.equals(Integer.TYPE)) |
49 | |
{ |
50 | 0 | result = ArrayUtils.toString((ArrayUtils.subarray((int[]) array, 0, maxElements))); |
51 | |
} |
52 | 4 | else if (componentType.equals(Long.TYPE)) |
53 | |
{ |
54 | 2 | result = ArrayUtils.toString((ArrayUtils.subarray((long[]) array, 0, maxElements))); |
55 | |
} |
56 | 2 | else if (componentType.equals(Float.TYPE)) |
57 | |
{ |
58 | 0 | result = ArrayUtils.toString((ArrayUtils.subarray((float[]) array, 0, maxElements))); |
59 | |
} |
60 | 2 | else if (componentType.equals(Double.TYPE)) |
61 | |
{ |
62 | 2 | result = ArrayUtils.toString((ArrayUtils.subarray((double[]) array, 0, maxElements))); |
63 | |
} |
64 | |
else |
65 | |
{ |
66 | 0 | throw new IllegalArgumentException("Unknown array component type: " + componentType.getName()); |
67 | |
} |
68 | |
|
69 | 14 | if (Array.getLength(array) > maxElements) |
70 | |
{ |
71 | 4 | StringBuffer buf = new StringBuffer(result); |
72 | 4 | buf.insert(buf.length() - 1, " [..]"); |
73 | 4 | result = buf.toString(); |
74 | |
} |
75 | |
|
76 | 14 | return result; |
77 | |
|
78 | |
} |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public static Object[] toArrayOfComponentType(Object[] objects, Class clazz) |
98 | |
{ |
99 | 84 | if (objects == null || objects.getClass().getComponentType().equals(clazz)) |
100 | |
{ |
101 | 4 | return objects; |
102 | |
} |
103 | |
|
104 | 80 | if (clazz == null) |
105 | |
{ |
106 | 2 | throw new IllegalArgumentException("Array target class must not be null"); |
107 | |
} |
108 | |
|
109 | 78 | Object[] result = (Object[]) Array.newInstance(clazz, objects.length); |
110 | 78 | System.arraycopy(objects, 0, result, 0, objects.length); |
111 | 76 | return result; |
112 | |
} |
113 | |
|
114 | |
} |