1 |
/* java.lang.reflect.Array |
/* java.lang.reflect.Array - manipulate arrays by reflection |
2 |
Copyright (C) 1998, 1999 Free Software Foundation, Inc. |
Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
7 |
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
8 |
the Free Software Foundation; either version 2, or (at your option) |
the Free Software Foundation; either version 2, or (at your option) |
9 |
any later version. |
any later version. |
10 |
|
|
11 |
GNU Classpath is distributed in the hope that it will be useful, but |
GNU Classpath is distributed in the hope that it will be useful, but |
12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
30 |
import gnu.classpath.Configuration; |
import gnu.classpath.Configuration; |
31 |
|
|
32 |
/** |
/** |
33 |
** Array is a set of static helper functions that allow you to create and manipulate arrays |
* Array holds static helper functions that allow you to create and |
34 |
** without knowing their type.<P> |
* manipulate arrays by reflection. Operations know how to perform widening |
35 |
** |
* conversions, but throw {@link IllegalArgumentException} if you attempt |
36 |
** <B>Note:</B> This class uses a Class object to tell what type of thing to work with. If you wish |
* a narrowing conversion. Also, when accessing primitive arrays, this |
37 |
** to work with primitive types, you may still use the Class functions; there are Class types |
* class performs object wrapping and unwrapping as necessary.<p> |
38 |
** defined that represent each different primitive type. They are <code>java.lang.Boolean.TYPE, |
* |
39 |
** java.lang.Byte.TYPE, </code>etc. These are not to be confused with the classes |
* <B>Note:</B> This class returns and accepts types as Classes, even |
40 |
** <code>java.lang.Boolean, java.lang.Byte</code>, etc., which are real classes.<P> |
* primitive types; there are Class types defined that represent each |
41 |
** |
* different primitive type. They are <code>java.lang.Boolean.TYPE, |
42 |
** <B>Also:</B> If the type of the array is primitive, the accessor functions will wrap the returned |
* java.lang.Byte.TYPE,</code>, also available as <code>boolean.class, |
43 |
** value in the appropriate class type (boolean = java.lang.Boolean, etc.).<P> |
* byte.class</code>, etc. These are not to be confused with the |
44 |
** |
* classes <code>java.lang.Boolean, java.lang.Byte</code>, etc., which are |
45 |
** <B>Performance note:</B> This class performs best when it does not have to convert primitive types. The further |
* real classes. Note also that the shorthand <code>Object[].class</code> |
46 |
** along the chain it has to convert, the worse performance will be. You're best off using the array as whatever |
* is a convenient way to get array Classes.<p> |
47 |
** type it already is, and then converting the result. You will do even worse if you do this and use the generic |
* |
48 |
** set() function. |
* <B>Performance note:</B> This class performs best when it does not have |
49 |
** |
* to convert primitive types. The further along the chain it has to convert, |
50 |
** @author John Keiser |
* the worse performance will be. You're best off using the array as whatever |
51 |
** @version 1.1.0, 31 May 1998 |
* type it already is, and then converting the result. You will do even |
52 |
** @see java.lang.Boolean#TYPE |
* worse if you do this and use the generic set() function. |
53 |
** @see java.lang.Byte#TYPE |
* |
54 |
** @see java.lang.Short#TYPE |
* @author John Keiser |
55 |
** @see java.lang.Character#TYPE |
* @author Eric Blake <ebb9@email.byu.edu> |
56 |
** @see java.lang.Integer#TYPE |
* @see java.lang.Boolean#TYPE |
57 |
** @see java.lang.Long#TYPE |
* @see java.lang.Byte#TYPE |
58 |
** @see java.lang.Float#TYPE |
* @see java.lang.Short#TYPE |
59 |
** @see java.lang.Double#TYPE |
* @see java.lang.Character#TYPE |
60 |
**/ |
* @see java.lang.Integer#TYPE |
61 |
|
* @see java.lang.Long#TYPE |
62 |
|
* @see java.lang.Float#TYPE |
63 |
|
* @see java.lang.Double#TYPE |
64 |
|
* @since 1.1 |
65 |
|
* @status updated to 1.4 |
66 |
|
*/ |
67 |
public final class Array |
public final class Array |
68 |
{ |
{ |
69 |
static |
static |
70 |
{ |
{ |
71 |
if (Configuration.INIT_LOAD_LIBRARY) |
if (Configuration.INIT_LOAD_LIBRARY) |
72 |
{ |
{ |
73 |
System.loadLibrary ("javalangreflect"); |
System.loadLibrary ("javalangreflect"); |
74 |
} |
} |
75 |
} |
} |
76 |
|
|
77 |
// Make this class uninstantiable. |
/** |
78 |
|
* This class is uninstantiable. |
79 |
|
*/ |
80 |
private Array () |
private Array () |
81 |
{ |
{ |
82 |
} |
} |
83 |
|
|
84 |
/** Creates a new single-dimensioned array. Will return null if the array is Void. |
/** |
85 |
** @param componentType the type of the array to create. |
* Creates a new single-dimensioned array. |
86 |
** @param length the length of the array to create. |
* @param componentType the type of the array to create |
87 |
** @exception NegativeArraySizeException when length is less than 0. |
* @param length the length of the array to create |
88 |
** @return the created array, cast to an Object. |
* @return the created array, cast to an Object |
89 |
**/ |
* @throws NullPointerException if <code>componentType</code> is null |
90 |
public static Object newInstance (Class componentType, int length) |
* @throws IllegalArgumentException if <code>componentType</code> is |
91 |
throws NegativeArraySizeException |
* <code>Void.TYPE</code> |
92 |
|
* @throws NegativeArraySizeException when length is less than 0 |
93 |
|
* @throws OutOfMemoryError if memory allocation fails |
94 |
|
*/ |
95 |
|
public static Object newInstance(Class componentType, int length) |
96 |
{ |
{ |
97 |
if (componentType == Boolean.TYPE) |
if (! componentType.isPrimitive()) |
98 |
{ |
return createObjectArray(componentType, length); |
99 |
return new boolean[length]; |
if (componentType == boolean.class) |
100 |
} |
return new boolean[length]; |
101 |
else if (componentType == Byte.TYPE) |
if (componentType == byte.class) |
102 |
{ |
return new byte[length]; |
103 |
return new byte[length]; |
if (componentType == char.class) |
104 |
} |
return new char[length]; |
105 |
else if (componentType == Character.TYPE) |
if (componentType == short.class) |
106 |
{ |
return new short[length]; |
107 |
return new char[length]; |
if (componentType == int.class) |
108 |
} |
return new int[length]; |
109 |
else if (componentType == Short.TYPE) |
if (componentType == long.class) |
110 |
{ |
return new long[length]; |
111 |
return new short[length]; |
if (componentType == float.class) |
112 |
} |
return new float[length]; |
113 |
else if (componentType == Integer.TYPE) |
if (componentType == double.class) |
114 |
{ |
return new double[length]; |
115 |
return new int[length]; |
// assert componentType == void.class |
116 |
} |
throw new IllegalArgumentException(); |
117 |
else if (componentType == Long.TYPE) |
} |
118 |
{ |
|
119 |
return new long[length]; |
/** |
120 |
} |
* Creates a new multi-dimensioned array. The new array has the same |
121 |
else if (componentType == Float.TYPE) |
* component type as the argument class, and the number of dimensions |
122 |
{ |
* in the new array is the sum of the dimensions of the argument class |
123 |
return new float[length]; |
* and the length of the argument dimensions. Virtual Machine limitations |
124 |
} |
* forbid too many dimensions (usually 255 is the maximum); but even |
125 |
else if (componentType == Double.TYPE) |
* 50 dimensions of 2 elements in each dimension would exceed your memory |
126 |
{ |
* long beforehand! |
127 |
return new double[length]; |
* |
128 |
} |
* @param componentType the type of the array to create. |
129 |
else if (componentType == Void.TYPE) |
* @param dimensions the dimensions of the array to create. Each element |
130 |
{ |
* in <code>dimensions</code> makes another dimension of the new |
131 |
return null; |
* array. Thus, <code>Array.newInstance(java.lang.Boolean, |
132 |
} |
* new int[]{1,2,3})</code> is the same as |
133 |
else |
* <code>new java.lang.Boolean[1][2][3]</code> |
134 |
{ |
* @return the created array, cast to an Object |
135 |
return createObjectArray (componentType, length); |
* @throws NullPointerException if componentType or dimension is null |
136 |
} |
* @throws IllegalArgumentException if the the size of |
137 |
} |
* <code>dimensions</code> is 0 or exceeds the maximum number of |
138 |
|
* array dimensions in the VM; or if componentType is Void.TYPE |
139 |
/** Creates a new multi-dimensioned array. Returns null if array is Void. |
* @throws NegativeArraySizeException when any of the dimensions is less |
140 |
** @param componentType the type of the array to create. |
* than 0 |
141 |
** @param dimensions the dimensions of the array to create. Each element in the |
* @throws OutOfMemoryError if memory allocation fails |
142 |
** <code>dimensions</code> represents another dimension of the created |
*/ |
143 |
** array. Thus, <code>newInstance(java.lang.Boolean, {1,2,3})</code> |
public static Object newInstance(Class componentType, int[] dimensions) |
|
** is the same as <code>new java.lang.Boolean[1][2][3]</code>. |
|
|
** @exception NegativeArraySizeException when any of the dimensions is less than 0. |
|
|
** @exception IllegalArgumentException if the the size of <code>dimensions</code> is 0 |
|
|
** or exceeds the maximum number of array dimensions |
|
|
** the underlying JVM can handle. |
|
|
** @return the created array, cast to an Object. |
|
|
**/ |
|
|
public static Object newInstance (Class componentType, int[]dimensions) |
|
|
throws IllegalArgumentException, NegativeArraySizeException |
|
144 |
{ |
{ |
|
|
|
145 |
if (dimensions.length <= 0) |
if (dimensions.length <= 0) |
146 |
{ |
throw new IllegalArgumentException ("Empty dimensions array."); |
147 |
throw new IllegalArgumentException ("Empty dimensions array."); |
return createMultiArray(componentType, dimensions, |
148 |
} |
dimensions.length - 1); |
|
return createDimensionedArray (componentType, dimensions, |
|
|
dimensions.length - 1); |
|
149 |
} |
} |
150 |
|
|
151 |
|
/** |
152 |
/** Gets the array length. |
* Gets the array length. |
153 |
** @param array the array. |
* @param array the array |
154 |
** @return the length of the array. |
* @return the length of the array |
155 |
** @exception IllegalArgumentException if <code>array</code> is not an array. |
* @throws IllegalArgumentException if <code>array</code> is not an array |
156 |
**/ |
* @throws NullPointerException if <code>array</code> is null |
157 |
public static native int getLength (Object array) |
*/ |
158 |
throws IllegalArgumentException; |
public static int getLength(Object array) |
|
|
|
|
/** Gets an element of an array. Primitive elements will be wrapped in the corresponding class type.<P> |
|
|
** |
|
|
** <B>Note:</B> For performance reasons, all true booleans will return the same true object (Boolean.TRUE) |
|
|
** and all false booleans will return the same false object (Boolean.FALSE). |
|
|
** |
|
|
** @param array the array to access. |
|
|
** @param index the array index to access. |
|
|
** @exception IllegalArgumentException if <code>array</code> is not an array. |
|
|
** @exception ArrayIndexOutOfBoundsException if <code>index</code> is out of bounds. |
|
|
** @return the element at index <code>index</code> in the array <code>array</code>. |
|
|
**/ |
|
|
public static Object get (Object array, int index) |
|
|
throws IllegalArgumentException, ArrayIndexOutOfBoundsException |
|
159 |
{ |
{ |
160 |
if (array instanceof Object[]) |
if (array instanceof Object[]) |
161 |
{ |
return ((Object[]) array).length; |
|
return ((Object[])array)[index]; |
|
|
} |
|
|
else if (array instanceof boolean[]) |
|
|
{ |
|
|
return ((boolean[])array)[index] ? Boolean.TRUE : Boolean.FALSE; |
|
|
} |
|
|
else if (array instanceof byte[]) |
|
|
{ |
|
|
return new Byte (((byte[])array)[index]); |
|
|
} |
|
|
else if (array instanceof char[]) |
|
|
{ |
|
|
return new Character (((char[]) array)[index]); |
|
|
} |
|
|
else if (array instanceof short[]) |
|
|
{ |
|
|
return new Short (((short[]) array)[index]); |
|
|
} |
|
|
else if (array instanceof int[]) |
|
|
{ |
|
|
return new Integer (((int[]) array)[index]); |
|
|
} |
|
|
else if (array instanceof long[]) |
|
|
{ |
|
|
return new Long (((long[]) array)[index]); |
|
|
} |
|
|
else if (array instanceof float[]) |
|
|
{ |
|
|
return new Float (((float[]) array)[index]); |
|
|
} |
|
|
else if (array instanceof double[]) |
|
|
{ |
|
|
return new Double (((double[]) array)[index]); |
|
|
} |
|
|
else |
|
|
{ |
|
|
throw new IllegalArgumentException ("Parameter not an array."); |
|
|
} |
|
|
} |
|
|
|
|
|
/** Gets an element of a boolean array. |
|
|
** @param array the array to access. |
|
|
** @param index the array index to access. |
|
|
** @exception IllegalArgumentException if <code>array</code> is not a boolean array. |
|
|
** @exception ArrayIndexOutOfBoundsException if <code>index</code> is out of bounds. |
|
|
** @return the boolean element at index <code>index</code> in the array <code>array</code>. |
|
|
**/ |
|
|
public static boolean getBoolean (Object array, int index) |
|
|
throws IllegalArgumentException, ArrayIndexOutOfBoundsException |
|
|
{ |
|
162 |
if (array instanceof boolean[]) |
if (array instanceof boolean[]) |
163 |
{ |
return ((boolean[]) array).length; |
|
return ((boolean[])array)[index]; |
|
|
} |
|
|
else |
|
|
{ |
|
|
throw new IllegalArgumentException ("Not a boolean array."); |
|
|
} |
|
|
} |
|
|
|
|
|
/** Gets an element of a byte array. |
|
|
** @param array the array to access. |
|
|
** @param index the array index to access. |
|
|
** @exception IllegalArgumentException if <code>array</code> is not a byte array. |
|
|
** @exception ArrayIndexOutOfBoundsException if <code>index</code> is out of bounds. |
|
|
** @return the byte element at index <code>index</code> in the array <code>array</code>. |
|
|
**/ |
|
|
public static byte getByte (Object array, int index) |
|
|
throws IllegalArgumentException, ArrayIndexOutOfBoundsException |
|
|
{ |
|
164 |
if (array instanceof byte[]) |
if (array instanceof byte[]) |
165 |
{ |
return ((byte[]) array). length; |
166 |
return ((byte[])array)[index]; |
if (array instanceof char[]) |
167 |
} |
return ((char[]) array).length; |
168 |
else |
if (array instanceof short[]) |
169 |
{ |
return ((short[]) array).length; |
170 |
throw new |
if (array instanceof int[]) |
171 |
IllegalArgumentException ("Array is not of appropriate type."); |
return ((int[]) array).length; |
172 |
} |
if (array instanceof long[]) |
173 |
} |
return ((long[]) array).length; |
174 |
|
if (array instanceof float[]) |
175 |
/** Gets an element of a short array. |
return ((float[]) array).length; |
176 |
** @param array the array to access. |
if (array instanceof double[]) |
177 |
** @param index the array index to access. |
return ((double[]) array).length; |
178 |
** @exception IllegalArgumentException if the elements of <code>array</code> cannot be |
if (array == null) |
179 |
** converted via widening conversion to a short. |
throw new NullPointerException(); |
180 |
** @exception ArrayIndexOutOfBoundsException if <code>index</code> is out of bounds. |
throw new IllegalArgumentException(); |
181 |
** @return the short element at index <code>index</code> in the array <code>array</code>. |
} |
182 |
**/ |
|
183 |
public static short getShort (Object array, int index) |
/** |
184 |
throws IllegalArgumentException, ArrayIndexOutOfBoundsException |
* Gets an element of an array. Primitive elements will be wrapped in |
185 |
|
* the corresponding class type. |
186 |
|
* |
187 |
|
* @param array the array to access |
188 |
|
* @param index the array index to access |
189 |
|
* @return the element at <code>array[index]</code> |
190 |
|
* @throws IllegalArgumentException if <code>array</code> is not an array |
191 |
|
* @throws NullPointerException if <code>array</code> is null |
192 |
|
* @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of |
193 |
|
* bounds |
194 |
|
* @see #getBoolean(Object, int) |
195 |
|
* @see #getByte(Object, int) |
196 |
|
* @see #getChar(Object, int) |
197 |
|
* @see #getShort(Object, int) |
198 |
|
* @see #getInt(Object, int) |
199 |
|
* @see #getLong(Object, int) |
200 |
|
* @see #getFloat(Object, int) |
201 |
|
* @see #getDouble(Object, int) |
202 |
|
*/ |
203 |
|
public static Object get(Object array, int index) |
204 |
{ |
{ |
205 |
|
if (array instanceof Object[]) |
206 |
|
return ((Object[]) array)[index]; |
207 |
|
if (array instanceof boolean[]) |
208 |
|
return ((boolean[]) array)[index] ? Boolean.TRUE : Boolean.FALSE; |
209 |
|
if (array instanceof byte[]) |
210 |
|
return new Byte(((byte[]) array)[index]); |
211 |
|
if (array instanceof char[]) |
212 |
|
return new Character(((char[]) array)[index]); |
213 |
if (array instanceof short[]) |
if (array instanceof short[]) |
214 |
{ |
return new Short(((short[]) array)[index]); |
215 |
return ((short[]) array)[index]; |
if (array instanceof int[]) |
216 |
} |
return new Integer(((int[]) array)[index]); |
217 |
else |
if (array instanceof long[]) |
218 |
{ |
return new Long(((long[]) array)[index]); |
219 |
return getByte (array, index); |
if (array instanceof float[]) |
220 |
} |
return new Float(((float[]) array)[index]); |
221 |
} |
if (array instanceof double[]) |
222 |
|
return new Double(((double[]) array)[index]); |
223 |
/** Gets an element of a char array. |
if (array == null) |
224 |
** @param array the array to access. |
throw new NullPointerException(); |
225 |
** @param index the array index to access. |
throw new IllegalArgumentException(); |
226 |
** @exception IllegalArgumentException if <code>array</code> is not a char array. |
} |
227 |
** @exception ArrayIndexOutOfBoundsException if <code>index</code> is out of bounds. |
|
228 |
** @return the char element at index <code>index</code> in the array <code>array</code>. |
/** |
229 |
**/ |
* Gets an element of a boolean array. |
230 |
public static char getChar (Object array, int index) |
* |
231 |
throws IllegalArgumentException, ArrayIndexOutOfBoundsException |
* @param array the array to access |
232 |
|
* @param index the array index to access |
233 |
|
* @return the boolean element at <code>array[index]</code> |
234 |
|
* @throws IllegalArgumentException if <code>array</code> is not a boolean |
235 |
|
* array |
236 |
|
* @throws NullPointerException if <code>array</code> is null |
237 |
|
* @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of |
238 |
|
* bounds |
239 |
|
* @see #get(Object, int) |
240 |
|
*/ |
241 |
|
public static boolean getBoolean(Object array, int index) |
242 |
|
{ |
243 |
|
if (array instanceof boolean[]) |
244 |
|
return ((boolean[]) array)[index]; |
245 |
|
if (array == null) |
246 |
|
throw new NullPointerException(); |
247 |
|
throw new IllegalArgumentException(); |
248 |
|
} |
249 |
|
|
250 |
|
/** |
251 |
|
* Gets an element of a byte array. |
252 |
|
* |
253 |
|
* @param array the array to access |
254 |
|
* @param index the array index to access |
255 |
|
* @return the byte element at <code>array[index]</code> |
256 |
|
* @throws IllegalArgumentException if <code>array</code> is not a byte |
257 |
|
* array |
258 |
|
* @throws NullPointerException if <code>array</code> is null |
259 |
|
* @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of |
260 |
|
* bounds |
261 |
|
* @see #get(Object, int) |
262 |
|
*/ |
263 |
|
public static byte getByte(Object array, int index) |
264 |
|
{ |
265 |
|
if (array instanceof byte[]) |
266 |
|
return ((byte[]) array)[index]; |
267 |
|
if (array == null) |
268 |
|
throw new NullPointerException(); |
269 |
|
throw new IllegalArgumentException(); |
270 |
|
} |
271 |
|
|
272 |
|
/** |
273 |
|
* Gets an element of a char array. |
274 |
|
* |
275 |
|
* @param array the array to access |
276 |
|
* @param index the array index to access |
277 |
|
* @return the char element at <code>array[index]</code> |
278 |
|
* @throws IllegalArgumentException if <code>array</code> is not a char |
279 |
|
* array |
280 |
|
* @throws NullPointerException if <code>array</code> is null |
281 |
|
* @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of |
282 |
|
* bounds |
283 |
|
* @see #get(Object, int) |
284 |
|
*/ |
285 |
|
public static char getChar(Object array, int index) |
286 |
{ |
{ |
287 |
if (array instanceof char[]) |
if (array instanceof char[]) |
288 |
{ |
return ((char[]) array)[index]; |
289 |
return ((char[]) array)[index]; |
if (array == null) |
290 |
} |
throw new NullPointerException(); |
291 |
else |
throw new IllegalArgumentException(); |
292 |
{ |
} |
293 |
throw new IllegalArgumentException ("Not a char array."); |
|
294 |
} |
/** |
295 |
|
* Gets an element of a short array. |
296 |
|
* |
297 |
|
* @param array the array to access |
298 |
|
* @param index the array index to access |
299 |
|
* @return the short element at <code>array[index]</code> |
300 |
|
* @throws IllegalArgumentException if <code>array</code> is not a byte |
301 |
|
* or char array |
302 |
|
* @throws NullPointerException if <code>array</code> is null |
303 |
|
* @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of |
304 |
|
* bounds |
305 |
|
* @see #get(Object, int) |
306 |
|
*/ |
307 |
|
public static short getShort(Object array, int index) |
308 |
|
{ |
309 |
|
if (array instanceof short[]) |
310 |
|
return ((short[]) array)[index]; |
311 |
|
return getByte(array, index); |
312 |
} |
} |
313 |
|
|
314 |
/** Gets an element of an int array. |
/** |
315 |
** @param array the array to access. |
* Gets an element of an int array. |
316 |
** @param index the array index to access. |
* |
317 |
** @exception IllegalArgumentException if the elements of <code>array</code> cannot be |
* @param array the array to access |
318 |
** converted via widening conversion to an int. |
* @param index the array index to access |
319 |
** @exception ArrayIndexOutOfBoundsException if <code>index</code> is out of bounds. |
* @return the int element at <code>array[index]</code> |
320 |
** @return the int element at index <code>index</code> in the array <code>array</code>. |
* @throws IllegalArgumentException if <code>array</code> is not a byte, |
321 |
**/ |
* char, short, or int array |
322 |
public static int getInt (Object array, int index) |
* @throws NullPointerException if <code>array</code> is null |
323 |
throws IllegalArgumentException, ArrayIndexOutOfBoundsException |
* @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of |
324 |
|
* bounds |
325 |
|
* @see #get(Object, int) |
326 |
|
*/ |
327 |
|
public static int getInt(Object array, int index) |
328 |
{ |
{ |
329 |
if (array instanceof int[]) |
if (array instanceof int[]) |
330 |
{ |
return ((int[]) array)[index]; |
331 |
return ((int[]) array)[index]; |
if (array instanceof char[]) |
332 |
} |
return ((char[]) array)[index]; |
333 |
else if (array instanceof char[]) |
return getShort(array, index); |
|
{ |
|
|
return ((char[]) array)[index]; |
|
|
} |
|
|
else |
|
|
{ |
|
|
return getShort (array, index); |
|
|
} |
|
334 |
} |
} |
335 |
|
|
336 |
/** Gets an element of a long array. |
/** |
337 |
** @param array the array to access. |
* Gets an element of a long array. |
338 |
** @param index the array index to access. |
* |
339 |
** @exception IllegalArgumentException if the elements of <code>array</code> cannot be |
* @param array the array to access |
340 |
** converted via widening conversion to a long. |
* @param index the array index to access |
341 |
** @exception ArrayIndexOutOfBoundsException if <code>index</code> is out of bounds. |
* @return the long element at <code>array[index]</code> |
342 |
** @return the long element at index <code>index</code> in the array <code>array</code>. |
* @throws IllegalArgumentException if <code>array</code> is not a byte, |
343 |
**/ |
* char, short, int, or long array |
344 |
public static long getLong (Object array, int index) |
* @throws NullPointerException if <code>array</code> is null |
345 |
throws IllegalArgumentException, ArrayIndexOutOfBoundsException |
* @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of |
346 |
|
* bounds |
347 |
|
* @see #get(Object, int) |
348 |
|
*/ |
349 |
|
public static long getLong(Object array, int index) |
350 |
{ |
{ |
351 |
if (array instanceof long[]) |
if (array instanceof long[]) |
352 |
{ |
return ((long[]) array)[index]; |
353 |
return ((long[]) array)[index]; |
return getInt(array, index); |
|
} |
|
|
else |
|
|
{ |
|
|
return getInt (array, index); |
|
|
} |
|
354 |
} |
} |
355 |
|
|
356 |
/** Gets an element of a float array. |
/** |
357 |
** @param array the array to access. |
* Gets an element of a float array. |
358 |
** @param index the array index to access. |
* |
359 |
** @exception IllegalArgumentException if the elements of <code>array</code> cannot be |
* @param array the array to access |
360 |
** converted via widening conversion to a float. |
* @param index the array index to access |
361 |
** @exception ArrayIndexOutOfBoundsException if <code>index</code> is out of bounds. |
* @return the float element at <code>array[index]</code> |
362 |
** @return the float element at index <code>index</code> in the array <code>array</code>. |
* @throws IllegalArgumentException if <code>array</code> is not a byte, |
363 |
**/ |
* char, short, int, long, or float array |
364 |
public static float getFloat (Object array, int index) |
* @throws NullPointerException if <code>array</code> is null |
365 |
throws IllegalArgumentException, ArrayIndexOutOfBoundsException |
* @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of |
366 |
|
* bounds |
367 |
|
* @see #get(Object, int) |
368 |
|
*/ |
369 |
|
public static float getFloat(Object array, int index) |
370 |
{ |
{ |
371 |
if (array instanceof float[]) |
if (array instanceof float[]) |
372 |
{ |
return ((float[]) array)[index]; |
373 |
return ((float[]) array)[index]; |
return getLong(array, index); |
|
} |
|
|
else |
|
|
{ |
|
|
return getLong (array, index); |
|
|
} |
|
374 |
} |
} |
375 |
|
|
376 |
/** Gets an element of a double array. |
/** |
377 |
** @param array the array to access. |
* Gets an element of a double array. |
378 |
** @param index the array index to access. |
* |
379 |
** @exception IllegalArgumentException if the elements of <code>array</code> cannot be |
* @param array the array to access |
380 |
** converted via widening conversion to a double. |
* @param index the array index to access |
381 |
** @exception ArrayIndexOutOfBoundsException if <code>index</code> is out of bounds. |
* @return the double element at <code>array[index]</code> |
382 |
** @return the double element at index <code>index</code> in the array <code>array</code>. |
* @throws IllegalArgumentException if <code>array</code> is not a byte, |
383 |
**/ |
* char, short, int, long, float, or double array |
384 |
public static double getDouble (Object array, int index) |
* @throws NullPointerException if <code>array</code> is null |
385 |
throws IllegalArgumentException, ArrayIndexOutOfBoundsException |
* @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of |
386 |
|
* bounds |
387 |
|
* @see #get(Object, int) |
388 |
|
*/ |
389 |
|
public static double getDouble(Object array, int index) |
390 |
{ |
{ |
391 |
if (array instanceof double[]) |
if (array instanceof double[]) |
392 |
{ |
return ((double[]) array)[index]; |
393 |
return ((double[]) array)[index]; |
return getFloat(array, index); |
|
} |
|
|
else |
|
|
{ |
|
|
return getFloat (array, index); |
|
|
} |
|
394 |
} |
} |
395 |
|
|
396 |
|
/** |
397 |
/** Sets an element of an array. If the array is primitive, then the new value must be of the |
* Sets an element of an array. If the array is primitive, then the new |
398 |
** corresponding wrapper type (boolean = java.lang.Boolean).<P> |
* value is unwrapped and widened. |
399 |
** |
* |
400 |
** |
* @param array the array to set a value of |
401 |
** @param array the array to set a value of. |
* @param index the array index to set the value to |
402 |
** @param index the array index to set the value to. |
* @param value the value to set |
403 |
** @param value the value to set. |
* @throws IllegalArgumentException if <code>array</code> is not an array, |
404 |
** @exception IllegalArgumentException if <code>array</code> is not an array or the value |
* or the array is primitive and unwrapping value fails, or the |
405 |
** is of the wrong type for the array. |
* value is not assignable to the array component type |
406 |
** @exception ArrayIndexOutOfBoundsException if <code>index</code> is out of bounds. |
* @throws NullPointerException if array is null, or if array is primitive |
407 |
**/ |
* and value is null |
408 |
public static void set (Object array, int index, Object value) |
* @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of |
409 |
throws IllegalArgumentException, ArrayIndexOutOfBoundsException |
* bounds |
410 |
|
* @see #setBoolean(Object, int, boolean) |
411 |
|
* @see #setByte(Object, int, byte) |
412 |
|
* @see #setChar(Object, int, char) |
413 |
|
* @see #setShort(Object, int, short) |
414 |
|
* @see #setInt(Object, int, int) |
415 |
|
* @see #setLong(Object, int, long) |
416 |
|
* @see #setFloat(Object, int, float) |
417 |
|
* @see #setDouble(Object, int, double) |
418 |
|
*/ |
419 |
|
public static void set(Object array, int index, Object value) |
420 |
{ |
{ |
421 |
if (array instanceof Object[]) |
if (array instanceof Object[]) |
422 |
{ |
{ |
423 |
((Object[])array)[index] = value; |
// Too bad Sun won't let us throw the easier ArrayStoreException! |
424 |
|
if (! array.getClass().getComponentType().isInstance(value)) |
425 |
|
throw new IllegalArgumentException(); |
426 |
|
((Object[]) array)[index] = value; |
427 |
} |
} |
428 |
else if (value instanceof Boolean) |
else if (value instanceof Boolean) |
429 |
{ |
setBoolean(array, index, ((Boolean) value).booleanValue()); |
|
setBoolean (array, index, ((Boolean) value).booleanValue ()); |
|
|
} |
|
430 |
else if (value instanceof Byte) |
else if (value instanceof Byte) |
431 |
{ |
setByte(array, index, ((Byte) value).byteValue()); |
|
setByte (array, index, ((Byte) value).byteValue ()); |
|
|
} |
|
432 |
else if (value instanceof Character) |
else if (value instanceof Character) |
433 |
{ |
setChar(array, index, ((Character) value).charValue()); |
|
setChar (array, index, ((Character) value).charValue ()); |
|
|
} |
|
434 |
else if (value instanceof Short) |
else if (value instanceof Short) |
435 |
{ |
setShort(array, index, ((Short) value).shortValue()); |
|
setShort (array, index, ((Short) value).shortValue ()); |
|
|
} |
|
436 |
else if (value instanceof Integer) |
else if (value instanceof Integer) |
437 |
{ |
setInt(array, index, ((Integer) value).intValue()); |
|
setInt (array, index, ((Integer) value).intValue ()); |
|
|
} |
|
438 |
else if (value instanceof Long) |
else if (value instanceof Long) |
439 |
{ |
setLong(array, index, ((Long) value).longValue()); |
|
setLong (array, index, ((Long) value).longValue ()); |
|
|
} |
|
440 |
else if (value instanceof Float) |
else if (value instanceof Float) |
441 |
{ |
setFloat(array, index, ((Float) value).floatValue()); |
|
setFloat (array, index, ((Float) value).floatValue ()); |
|
|
} |
|
442 |
else if (value instanceof Double) |
else if (value instanceof Double) |
443 |
{ |
setDouble(array, index, ((Double) value).doubleValue()); |
444 |
setDouble (array, index, ((Double) value).doubleValue ()); |
else if (array == null) |
445 |
} |
throw new NullPointerException(); |
446 |
else |
else |
447 |
{ |
throw new IllegalArgumentException(); |
448 |
throw new |
} |
449 |
IllegalArgumentException ("Tried to set a value on a non-array."); |
|
450 |
} |
/** |
451 |
} |
* Sets an element of a boolean array. |
452 |
|
* |
453 |
/** Sets an element of a boolean array. |
* @param array the array to set a value of |
454 |
** |
* @param index the array index to set the value to |
455 |
** @param array the array to set a value of. |
* @param value the value to set |
456 |
** @param index the array index to set the value to. |
* @throws IllegalArgumentException if <code>array</code> is not a boolean |
457 |
** @param value the value to set. |
* array |
458 |
** @exception IllegalArgumentException if <code>array</code> is not a boolean array. |
* @throws NullPointerException if <code>array</code> is null |
459 |
** @exception ArrayIndexOutOfBoundsException if <code>index</code> is out of bounds. |
* @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of |
460 |
**/ |
* bounds |
461 |
public static void setBoolean (Object array, int index, boolean value) |
* @see #set(Object, int, Object) |
462 |
throws IllegalArgumentException, ArrayIndexOutOfBoundsException |
*/ |
463 |
|
public static void setBoolean(Object array, int index, boolean value) |
464 |
{ |
{ |
465 |
if (array instanceof boolean[]) |
if (array instanceof boolean[]) |
466 |
{ |
((boolean[]) array)[index] = value; |
467 |
((boolean[])array)[index] = value; |
else if (array == null) |
468 |
} |
throw new NullPointerException(); |
469 |
else |
else |
470 |
{ |
throw new IllegalArgumentException(); |
471 |
throw new IllegalArgumentException ("Not a boolean array."); |
} |
472 |
} |
|
473 |
} |
/** |
474 |
|
* Sets an element of a byte array. |
475 |
/** Sets an element of a byte array. |
* |
476 |
** |
* @param array the array to set a value of |
477 |
** @param array the array to set a value of. |
* @param index the array index to set the value to |
478 |
** @param index the array index to set the value to. |
* @param value the value to set |
479 |
** @param value the value to set. |
* @throws IllegalArgumentException if <code>array</code> is not a byte, |
480 |
** @exception IllegalArgumentException if the value cannot be converted via widening |
* short, int, long, float, or double array |
481 |
** conversion to the type of <code>array</code>. |
* @throws NullPointerException if <code>array</code> is null |
482 |
** @exception ArrayIndexOutOfBoundsException if <code>index</code> is out of bounds. |
* @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of |
483 |
**/ |
* bounds |
484 |
public static void setByte (Object array, int index, byte value) |
* @see #set(Object, int, Object) |
485 |
throws IllegalArgumentException, ArrayIndexOutOfBoundsException |
*/ |
486 |
|
public static void setByte(Object array, int index, byte value) |
487 |
{ |
{ |
488 |
if (array instanceof byte[]) |
if (array instanceof byte[]) |
489 |
{ |
((byte[]) array)[index] = value; |
|
((byte[])array)[index] = value; |
|
|
} |
|
490 |
else |
else |
491 |
{ |
setShort(array, index, value); |
|
setShort (array, index, value); |
|
|
} |
|
492 |
} |
} |
493 |
|
|
494 |
/** Sets an element of a char array. |
/** |
495 |
** |
* Sets an element of a char array. |
496 |
** @param array the array to set a value of. |
* |
497 |
** @param index the array index to set the value to. |
* @param array the array to set a value of |
498 |
** @param value the value to set. |
* @param index the array index to set the value to |
499 |
** @exception IllegalArgumentException if the value cannot be converted via widening |
* @param value the value to set |
500 |
** conversion to the type of <code>array</code>. |
* @throws IllegalArgumentException if <code>array</code> is not a char, |
501 |
** @exception ArrayIndexOutOfBoundsException if <code>index</code> is out of bounds. |
* int, long, float, or double array |
502 |
**/ |
* @throws NullPointerException if <code>array</code> is null |
503 |
public static void setChar (Object array, int index, char value) |
* @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of |
504 |
throws IllegalArgumentException, ArrayIndexOutOfBoundsException |
* bounds |
505 |
|
* @see #set(Object, int, Object) |
506 |
|
*/ |
507 |
|
public static void setChar(Object array, int index, char value) |
508 |
{ |
{ |
509 |
if (array instanceof char[]) |
if (array instanceof char[]) |
510 |
{ |
((char[]) array)[index] = value; |
|
((char[]) array)[index] = value; |
|
|
} |
|
511 |
else |
else |
512 |
{ |
setInt(array, index, value); |
|
setInt (array, index, value); |
|
|
} |
|
513 |
} |
} |
514 |
|
|
515 |
/** Sets an element of a short array. |
/** |
516 |
** |
* Sets an element of a short array. |
517 |
** @param array the array to set a value of. |
* |
518 |
** @param index the array index to set the value to. |
* @param array the array to set a value of |
519 |
** @param value the value to set. |
* @param index the array index to set the value to |
520 |
** @exception IllegalArgumentException if the value cannot be converted via widening |
* @param value the value to set |
521 |
** conversion to the type of <code>array</code>. |
* @throws IllegalArgumentException if <code>array</code> is not a short, |
522 |
** @exception ArrayIndexOutOfBoundsException if <code>index</code> is out of bounds. |
* int, long, float, or double array |
523 |
**/ |
* @throws NullPointerException if <code>array</code> is null |
524 |
public static void setShort (Object array, int index, short value) |
* @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of |
525 |
throws IllegalArgumentException, ArrayIndexOutOfBoundsException |
* bounds |
526 |
|
* @see #set(Object, int, Object) |
527 |
|
*/ |
528 |
|
public static void setShort(Object array, int index, short value) |
529 |
{ |
{ |
530 |
if (array instanceof short[]) |
if (array instanceof short[]) |
531 |
{ |
((short[]) array)[index] = value; |
|
((short[]) array)[index] = value; |
|
|
} |
|
532 |
else |
else |
533 |
{ |
setInt(array, index, value); |
|
setInt (array, index, value); |
|
|
} |
|
534 |
} |
} |
535 |
|
|
536 |
/** Sets an element of an int array. |
/** |
537 |
** |
* Sets an element of an int array. |
538 |
** @param array the array to set a value of. |
* |
539 |
** @param index the array index to set the value to. |
* @param array the array to set a value of |
540 |
** @param value the value to set. |
* @param index the array index to set the value to |
541 |
** @exception IllegalArgumentException if the value cannot be converted via widening |
* @param value the value to set |
542 |
** conversion to the type of <code>array</code>. |
* @throws IllegalArgumentException if <code>array</code> is not an int, |
543 |
** @exception ArrayIndexOutOfBoundsException if <code>index</code> is out of bounds. |
* long, float, or double array |
544 |
**/ |
* @throws NullPointerException if <code>array</code> is null |
545 |
public static void setInt (Object array, int index, int value) |
* @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of |
546 |
throws IllegalArgumentException, ArrayIndexOutOfBoundsException |
* bounds |
547 |
|
* @see #set(Object, int, Object) |
548 |
|
*/ |
549 |
|
public static void setInt(Object array, int index, int value) |
550 |
{ |
{ |
551 |
if (array instanceof float[]) |
if (array instanceof int[]) |
552 |
{ |
((int[]) array)[index] = value; |
|
((int[]) array)[index] = value; |
|
|
} |
|
553 |
else |
else |
554 |
{ |
setLong(array, index, value); |
|
setLong (array, index, value); |
|
|
} |
|
555 |
} |
} |
556 |
|
|
557 |
/** Sets an element of a long array. |
/** |
558 |
** |
* Sets an element of a long array. |
559 |
** @param array the array to set a value of. |
* |
560 |
** @param index the array index to set the value to. |
* @param array the array to set a value of |
561 |
** @param value the value to set. |
* @param index the array index to set the value to |
562 |
** @exception IllegalArgumentException if the value cannot be converted via widening |
* @param value the value to set |
563 |
** conversion to the type of <code>array</code>. |
* @throws IllegalArgumentException if <code>array</code> is not a long, |
564 |
** @exception ArrayIndexOutOfBoundsException if <code>index</code> is out of bounds. |
* float, or double array |
565 |
**/ |
* @throws NullPointerException if <code>array</code> is null |
566 |
public static void setLong (Object array, int index, long value) |
* @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of |
567 |
throws IllegalArgumentException, ArrayIndexOutOfBoundsException |
* bounds |
568 |
|
* @see #set(Object, int, Object) |
569 |
|
*/ |
570 |
|
public static void setLong(Object array, int index, long value) |
571 |
{ |
{ |
572 |
if (array instanceof long[]) |
if (array instanceof long[]) |
573 |
{ |
((long[]) array)[index] = value; |
|
((long[]) array)[index] = value; |
|
|
} |
|
574 |
else |
else |
575 |
{ |
setFloat(array, index, value); |
|
setFloat (array, index, value); |
|
|
} |
|
576 |
} |
} |
577 |
|
|
578 |
/** Sets an element of a float array. |
/** |
579 |
** |
* Sets an element of a float array. |
580 |
** @param array the array to set a value of. |
* |
581 |
** @param index the array index to set the value to. |
* @param array the array to set a value of |
582 |
** @param value the value to set. |
* @param index the array index to set the value to |
583 |
** @exception IllegalArgumentException if the value cannot be converted via widening |
* @param value the value to set |
584 |
** conversion to the type of <code>array</code>. |
* @throws IllegalArgumentException if <code>array</code> is not a float |
585 |
** @exception ArrayIndexOutOfBoundsException if <code>index</code> is out of bounds. |
* or double array |
586 |
**/ |
* @throws NullPointerException if <code>array</code> is null |
587 |
public static void setFloat (Object array, int index, float value) |
* @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of |
588 |
throws IllegalArgumentException, ArrayIndexOutOfBoundsException |
* bounds |
589 |
|
* @see #set(Object, int, Object) |
590 |
|
*/ |
591 |
|
public static void setFloat(Object array, int index, float value) |
592 |
{ |
{ |
593 |
if (array instanceof float[]) |
if (array instanceof float[]) |
594 |
{ |
((float[]) array)[index] = value; |
|
((float[]) array)[index] = value; |
|
|
} |
|
595 |
else |
else |
596 |
{ |
setDouble(array, index, value); |
|
setDouble (array, index, value); |
|
|
} |
|
597 |
} |
} |
598 |
|
|
599 |
/** Sets an element of a double array. |
/** |
600 |
** |
* Sets an element of a double array. |
601 |
** @param array the array to set a value of. |
* |
602 |
** @param index the array index to set the value to. |
* @param array the array to set a value of |
603 |
** @param value the value to set. |
* @param index the array index to set the value to |
604 |
** @exception IllegalArgumentException if <code>array</code> is not a double array. |
* @param value the value to set |
605 |
** @exception ArrayIndexOutOfBoundsException if <code>index</code> is out of bounds. |
* @throws IllegalArgumentException if <code>array</code> is not a double |
606 |
**/ |
* array |
607 |
public static void setDouble (Object array, int index, double value) |
* @throws NullPointerException if <code>array</code> is null |
608 |
throws IllegalArgumentException, ArrayIndexOutOfBoundsException |
* @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of |
609 |
|
* bounds |
610 |
|
* @see #set(Object, int, Object) |
611 |
|
*/ |
612 |
|
public static void setDouble(Object array, int index, double value) |
613 |
{ |
{ |
614 |
if (array instanceof double[]) |
if (array instanceof double[]) |
615 |
{ |
((double[]) array)[index] = value; |
616 |
((double[]) array)[index] = value; |
else if (array == null) |
617 |
} |
throw new NullPointerException(); |
618 |
else |
else |
619 |
{ |
throw new IllegalArgumentException(); |
620 |
throw new |
} |
621 |
IllegalArgumentException |
|
622 |
("Array is not of appropriate primitive type"); |
/** |
623 |
} |
* Dynamically and recursively create a multi-dimensioned array of objects. |
624 |
} |
* |
625 |
|
* @param type guaranteed to be a valid object type |
626 |
|
* @param dimensions the dimensions of the array |
627 |
/* |
* @param index index of the current dimension to build |
628 |
* PRIVATE HELPERS |
* @return the new multi-dimensioned array |
629 |
|
* @throws NegativeArraySizeException if any entry of dimensions is negative |
630 |
|
* @throws OutOfMemoryError if memory allocation fails |
631 |
*/ |
*/ |
632 |
|
// This would be faster if implemented natively, using the multianewarray |
633 |
private static Class objectClass; |
// bytecode instead of this recursive call |
634 |
static |
private static Object createMultiArray(Class type, int[] dimensions, |
635 |
{ |
int index) |
636 |
try |
{ |
637 |
{ |
if (index == 0) |
638 |
objectClass = Class.forName ("java.lang.Object"); |
return newInstance(type, dimensions[0]); |
639 |
} |
|
640 |
catch (Exception E) |
Object toAdd = createMultiArray(type, dimensions, index - 1); |
641 |
{ |
Class thisType = toAdd.getClass(); |
642 |
} |
Object[] retval |
643 |
} |
= (Object[]) createObjectArray(thisType, dimensions[index]); |
644 |
|
if (dimensions[index] > 0) |
645 |
private static Object createDimensionedArray (Class type, int[]dimensions, |
retval[0] = toAdd; |
646 |
int dimensionToAdd) throws |
int i = dimensions[index]; |
647 |
IllegalArgumentException, NegativeArraySizeException |
while (--i > 0) |
648 |
{ |
retval[i] = createMultiArray(type, dimensions, index - 1); |
649 |
if (dimensionToAdd > 0) |
return retval; |
650 |
{ |
} |
651 |
Object toAdd = |
|
652 |
createDimensionedArray (type, dimensions, dimensionToAdd - 1); |
/** |
653 |
Class thisType = toAdd.getClass (); |
* Dynamically create an array of objects. |
654 |
Object[] retval = |
* |
655 |
(Object[]) createObjectArray (thisType, dimensions[dimensionToAdd]); |
* @param type guaranteed to be a valid object type |
656 |
if (dimensions[dimensionToAdd] > 0) |
* @param dim the length of the array |
657 |
{ |
* @return the new array |
658 |
retval[0] = toAdd; |
* @throws NegativeArraySizeException if dim is negative |
659 |
} |
* @throws OutOfMemoryError if memory allocation fails |
660 |
for (int i = 1; i < dimensions[dimensionToAdd]; i++) |
*/ |
661 |
{ |
private static native Object createObjectArray(Class type, int dim); |
|
retval[i] = |
|
|
createDimensionedArray (type, dimensions, dimensionToAdd - 1); |
|
|
} |
|
|
return retval; |
|
|
} |
|
|
else |
|
|
{ |
|
|
Object toAdd = newInstance (type, dimensions[0]); |
|
|
return toAdd; |
|
|
} |
|
|
} |
|
|
|
|
|
private static native Object createObjectArray (Class type, int dim); |
|
662 |
} |
} |