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 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 Class componentType = array.getClass().getComponentType();
28 if (Object.class.isAssignableFrom(componentType))
29 {
30 result = ArrayUtils.toString((ArrayUtils.subarray((Object[]) array, 0, maxElements)));
31 }
32 else if (componentType.equals(Boolean.TYPE))
33 {
34 result = ArrayUtils.toString((ArrayUtils.subarray((boolean[]) array, 0, maxElements)));
35 }
36 else if (componentType.equals(Byte.TYPE))
37 {
38 result = ArrayUtils.toString((ArrayUtils.subarray((byte[]) array, 0, maxElements)));
39 }
40 else if (componentType.equals(Character.TYPE))
41 {
42 result = ArrayUtils.toString((ArrayUtils.subarray((char[]) array, 0, maxElements)));
43 }
44 else if (componentType.equals(Short.TYPE))
45 {
46 result = ArrayUtils.toString((ArrayUtils.subarray((short[]) array, 0, maxElements)));
47 }
48 else if (componentType.equals(Integer.TYPE))
49 {
50 result = ArrayUtils.toString((ArrayUtils.subarray((int[]) array, 0, maxElements)));
51 }
52 else if (componentType.equals(Long.TYPE))
53 {
54 result = ArrayUtils.toString((ArrayUtils.subarray((long[]) array, 0, maxElements)));
55 }
56 else if (componentType.equals(Float.TYPE))
57 {
58 result = ArrayUtils.toString((ArrayUtils.subarray((float[]) array, 0, maxElements)));
59 }
60 else if (componentType.equals(Double.TYPE))
61 {
62 result = ArrayUtils.toString((ArrayUtils.subarray((double[]) array, 0, maxElements)));
63 }
64 else
65 {
66 throw new IllegalArgumentException("Unknown array component type: " + componentType.getName());
67 }
68
69 if (Array.getLength(array) > maxElements)
70 {
71 StringBuffer buf = new StringBuffer(result);
72 buf.insert(buf.length() - 1, " [..]");
73 result = buf.toString();
74 }
75
76 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 if (objects == null || objects.getClass().getComponentType().equals(clazz))
100 {
101 return objects;
102 }
103
104 if (clazz == null)
105 {
106 throw new IllegalArgumentException("Array target class must not be null");
107 }
108
109 Object[] result = (Object[]) Array.newInstance(clazz, objects.length);
110 System.arraycopy(objects, 0, result, 0, objects.length);
111 return result;
112 }
113
114 }