Home | History | Annotate | Download | only in reflect

Lines Matching refs:array

32  * The {@code Array} class provides static methods to dynamically create and
35 * <p>{@code Array} permits widening conversions to occur during a get or set
42 class Array {
45 * Constructor. Class Array is not instantiable.
47 private Array() {}
50 * Creates a new array with the specified component type and
52 * Invoking this method is equivalent to creating an array
57 * Array.newInstance(componentType, x);
61 * <p>The number of dimensions of the new array must not
65 * component type of the new array
66 * @param length the length of the new array
67 * @return the new array
71 * Void#TYPE} or if the number of dimensions of the requested array
82 * Creates a new array
85 * represents a non-array class or interface, the new array
88 * {@code componentType} represents an array class, the
89 * number of dimensions of the new array is equal to the sum
92 * component type of the new array is the component type of
95 * <p>The number of dimensions of the new array must not
99 * type of the new array
100 * @param dimensions an array of {@code int} representing the dimensions of
101 * the new array
102 * @return the new array
106 * argument is a zero-dimensional array, if componentType is {@link
107 * Void#TYPE}, or if the number of dimensions of the requested array
119 throw new IllegalArgumentException("Can't allocate an array of void");
128 * Returns the length of the specified array object, as an {@code int}.
130 * @param array the array
131 * @return the length of the array
133 * an array
137 public static int getLength(Object array)
139 if (array instanceof Object[]) {
140 return ((Object[]) array).length;
141 } else if (array instanceof boolean[]) {
142 return ((boolean[]) array).length;
143 } else if (array instanceof byte[]) {
144 return ((byte[]) array).length;
145 } else if (array instanceof char[]) {
146 return ((char[]) array).length;
147 } else if (array instanceof double[]) {
148 return ((double[]) array).length;
149 } else if (array instanceof float[]) {
150 return ((float[]) array).length;
151 } else if (array instanceof int[]) {
152 return ((int[]) array).length;
153 } else if (array instanceof long[]) {
154 return ((long[]) array).length;
155 } else if (array instanceof short[]) {
156 return ((short[]) array).length;
158 throw badArray(array);
163 * array object. The value is automatically wrapped in an object
166 * @param array the array
169 * the specified array
172 * an array
175 * length of the specified array
178 public static Object get(Object array, int index)
180 if (array instanceof Object[]) {
181 return ((Object[]) array)[index];
183 if (array instanceof boolean[]) {
184 return ((boolean[]) array)[index] ? Boolean.TRUE : Boolean.FALSE;
186 if (array instanceof byte[]) {
187 return Byte.valueOf(((byte[]) array)[index]);
189 if (array instanceof char[]) {
190 return Character.valueOf(((char[]) array)[index]);
192 if (array instanceof short[]) {
193 return Short.valueOf(((short[]) array)[index]);
195 if (array instanceof int[]) {
196 return Integer.valueOf(((int[]) array)[index]);
198 if (array instanceof long[]) {
199 return Long.valueOf(((long[]) array)[index]);
201 if (array instanceof float[]) {
202 return new Float(((float[]) array)[index]);
204 if (array instanceof double[]) {
205 return new Double(((double[]) array)[index]);
207 if (array == null) {
208 throw new NullPointerException("array == null");
210 throw notAnArray(array);
215 * array object, as a {@code boolean}.
217 * @param array the array
219 * @return the value of the indexed component in the specified array
222 * an array, or if the indexed element cannot be converted to the
226 * length of the specified array
227 * @see Array#get
230 public static boolean getBoolean(Object array, int index)
232 if (array instanceof boolean[]) {
233 return ((boolean[]) array)[index];
235 throw badArray(array);
240 * array object, as a {@code byte}.
242 * @param array the array
244 * @return the value of the indexed component in the specified array
247 * an array, or if the indexed element cannot be converted to the
251 * length of the specified array
252 * @see Array#get
255 public static byte getByte(Object array, int index)
257 if (array instanceof byte[]) {
258 return ((byte[]) array)[index];
260 throw badArray(array);
265 * array object, as a {@code char}.
267 * @param array the array
269 * @return the value of the indexed component in the specified array
272 * an array, or if the indexed element cannot be converted to the
276 * length of the specified array
277 * @see Array#get
280 public static char getChar(Object array, int index)
282 if (array instanceof char[]) {
283 return ((char[]) array)[index];
285 throw badArray(array);
290 * array object, as a {@code short}.
292 * @param array the array
294 * @return the value of the indexed component in the specified array
297 * an array, or if the indexed element cannot be converted to the
301 * length of the specified array
302 * @see Array#get
305 public static short getShort(Object array, int index)
307 if (array instanceof short[]) {
308 return ((short[]) array)[index];
309 } else if (array instanceof byte[]) {
310 return ((byte[]) array)[index];
312 throw badArray(array);
317 * array object, as an {@code int}.
319 * @param array the array
321 * @return the value of the indexed component in the specified array
324 * an array, or if the indexed element cannot be converted to the
328 * length of the specified array
329 * @see Array#get
332 public static int getInt(Object array, int index)
334 if (array instanceof int[]) {
335 return ((int[]) array)[index];
336 } else if (array instanceof byte[]) {
337 return ((byte[]) array)[index];
338 } else if (array instanceof char[]) {
339 return ((char[]) array)[index];
340 } else if (array instanceof short[]) {
341 return ((short[]) array)[index];
343 throw badArray(array);
348 * array object, as a {@code long}.
350 * @param array the array
352 * @return the value of the indexed component in the specified array
355 * an array, or if the indexed element cannot be converted to the
359 * length of the specified array
360 * @see Array#get
363 public static long getLong(Object array, int index)
365 if (array instanceof long[]) {
366 return ((long[]) array)[index];
367 } else if (array instanceof byte[]) {
368 return ((byte[]) array)[index];
369 } else if (array instanceof char[]) {
370 return ((char[]) array)[index];
371 } else if (array instanceof int[]) {
372 return ((int[]) array)[index];
373 } else if (array instanceof short[]) {
374 return ((short[]) array)[index];
376 throw badArray(array);
381 * array object, as a {@code float}.
383 * @param array the array
385 * @return the value of the indexed component in the specified array
388 * an array, or if the indexed element cannot be converted to the
392 * length of the specified array
393 * @see Array#get
396 public static float getFloat(Object array, int index)
398 if (array instanceof float[]) {
399 return ((float[]) array)[index];
400 } else if (array instanceof byte[]) {
401 return ((byte[]) array)[index];
402 } else if (array instanceof char[]) {
403 return ((char[]) array)[index];
404 } else if (array instanceof int[]) {
405 return ((int[]) array)[index];
406 } else if (array instanceof long[]) {
407 return ((long[]) array)[index];
408 } else if (array instanceof short[]) {
409 return ((short[]) array)[index];
411 throw badArray(array);
416 * array object, as a {@code double}.
418 * @param array the array
420 * @return the value of the indexed component in the specified array
423 * an array, or if the indexed element cannot be converted to the
427 * length of the specified array
428 * @see Array#get
431 public static double getDouble(Object array, int index)
433 if (array instanceof double[]) {
434 return ((double[]) array)[index];
435 } else if (array instanceof byte[]) {
436 return ((byte[]) array)[index];
437 } else if (array instanceof char[]) {
438 return ((char[]) array)[index];
439 } else if (array instanceof float[]) {
440 return ((float[]) array)[index];
441 } else if (array instanceof int[]) {
442 return ((int[]) array)[index];
443 } else if (array instanceof long[]) {
444 return ((long[]) array)[index];
445 } else if (array instanceof short[]) {
446 return ((short[]) array)[index];
448 throw badArray(array);
452 * Sets the value of the indexed component of the specified array
454 * automatically unwrapped if the array has a primitive component
456 * @param array the array
457 * @param index the index into the array
462 * is not an array, or if the array component type is primitive and
466 * the length of the specified array
469 public static void set(Object array, int index, Object value)
471 if (!array.getClass().isArray()) {
472 throw notAnArray(array);
475 if (array instanceof Object[]) {
476 if (value != null && !array.getClass().getComponentType().isInstance(value)) {
477 throw incompatibleType(array);
479 ((Object[]) array)[index] = value;
482 throw new IllegalArgumentException("Primitive array can't take null values.");
485 setBoolean(array, index, ((Boolean) value).booleanValue());
487 setByte(array, index, ((Byte) value).byteValue());
489 setChar(array, index, ((Character) value).charValue());
491 setShort(array, index, ((Short) value).shortValue());
493 setInt(array, index, ((Integer) value).intValue());
495 setLong(array, index, ((Long) value).longValue());
497 setFloat(array, index, ((Float) value).floatValue());
499 setDouble(array, index, ((Double) value).doubleValue());
505 * Sets the value of the indexed component of the specified array
507 * @param array the array
508 * @param index the index into the array
513 * is not an array, or if the specified value cannot be converted
514 * to the underlying array's component type by an identity or a
518 * the length of the specified array
519 * @see Array#set
523 public static void setBoolean(Object array, int index, boolean z)
525 if (array instanceof boolean[]) {
526 ((boolean[]) array)[index] = z;
528 throw badArray(array);
533 * Sets the value of the indexed component of the specified array
535 * @param array the array
536 * @param index the index into the array
541 * is not an array, or if the specified value cannot be converted
542 * to the underlying array's component type by an identity or a
546 * the length of the specified array
547 * @see Array#set
550 public static void setByte(Object array, int index, byte b)
552 if (array instanceof byte[]) {
553 ((byte[]) array)[index] = b;
554 } else if (array instanceof double[]) {
555 ((double[]) array)[index] = b;
556 } else if (array instanceof float[]) {
557 ((float[]) array)[index] = b;
558 } else if (array instanceof int[]) {
559 ((int[]) array)[index] = b;
560 } else if (array instanceof long[]) {
561 ((long[]) array)[index] = b;
562 } else if (array instanceof short[]) {
563 ((short[]) array)[index] = b;
565 throw badArray(array);
570 * Sets the value of the indexed component of the specified array
572 * @param array the array
573 * @param index the index into the array
578 * is not an array, or if the specified value cannot be converted
579 * to the underlying array's component type by an identity or a
583 * the length of the specified array
584 * @see Array#set
587 public static void setChar(Object array, int index, char c)
589 if (array instanceof char[]) {
590 ((char[]) array)[index] = c;
591 } else if (array instanceof double[]) {
592 ((double[]) array)[index] = c;
593 } else if (array instanceof float[]) {
594 ((float[]) array)[index] = c;
595 } else if (array instanceof int[]) {
596 ((int[]) array)[index] = c;
597 } else if (array instanceof long[]) {
598 ((long[]) array)[index] = c;
600 throw badArray(array);
605 * Sets the value of the indexed component of the specified array
607 * @param array the array
608 * @param index the index into the array
613 * is not an array, or if the specified value cannot be converted
614 * to the underlying array's component type by an identity or a
618 * the length of the specified array
619 * @see Array#set
622 public static void setShort(Object array, int index, short s)
624 if (array instanceof short[]) {
625 ((short[]) array)[index] = s;
626 } else if (array instanceof double[]) {
627 ((double[]) array)[index] = s;
628 } else if (array instanceof float[]) {
629 ((float[]) array)[index] = s;
630 } else if (array instanceof int[]) {
631 ((int[]) array)[index] = s;
632 } else if (array instanceof long[]) {
633 ((long[]) array)[index] = s;
635 throw badArray(array);
640 * Sets the value of the indexed component of the specified array
642 * @param array the array
643 * @param index the index into the array
648 * is not an array, or if the specified value cannot be converted
649 * to the underlying array's component type by an identity or a
653 * the length of the specified array
654 * @see Array#set
657 public static void setInt(Object array, int index, int i)
659 if (array instanceof int[]) {
660 ((int[]) array)[index] = i;
661 } else if (array instanceof double[]) {
662 ((double[]) array)[index] = i;
663 } else if (array instanceof float[]) {
664 ((float[]) array)[index] = i;
665 } else if (array instanceof long[]) {
666 ((long[]) array)[index] = i;
668 throw badArray(array);
673 * Sets the value of the indexed component of the specified array
675 * @param array the array
676 * @param index the index into the array
681 * is not an array, or if the specified value cannot be converted
682 * to the underlying array's component type by an identity or a
686 * the length of the specified array
687 * @see Array#set
690 public static void setLong(Object array, int index, long l)
692 if (array instanceof long[]) {
693 ((long[]) array)[index] = l;
694 } else if (array instanceof double[]) {
695 ((double[]) array)[index] = l;
696 } else if (array instanceof float[]) {
697 ((float[]) array)[index] = l;
699 throw badArray(array);
704 * Sets the value of the indexed component of the specified array
706 * @param array the array
707 * @param index the index into the array
712 * is not an array, or if the specified value cannot be converted
713 * to the underlying array's component type by an identity or a
717 * the length of the specified array
718 * @see Array#set
720 public static void setFloat(Object array, int index, float f)
722 if (array instanceof float[]) {
723 ((float[]) array)[index] = f;
724 } else if (array instanceof double[]) {
725 ((double[]) array)[index] = f;
727 throw badArray(array);
732 * Sets the value of the indexed component of the specified array
734 * @param array the array
735 * @param index the index into the array
740 * is not an array, or if the specified value cannot be converted
741 * to the underlying array's component type by an identity or a
745 * the length of the specified array
746 * @see Array#set
749 public static void setDouble(Object array, int index, double d)
751 if (array instanceof double[]) {
752 ((double[]) array)[index] = d;
754 throw badArray(array);
764 * Returns a new array
794 throw new IllegalArgumentException("Can't allocate an array of void");
808 * Create a multi-dimensional array of objects with the specified type.
816 * Create a one-dimensional array of objects with the specified type.
823 throw new IllegalArgumentException("Not an array: " + o.getClass());
827 throw new IllegalArgumentException("Array has incompatible type: " + o.getClass());
830 private static RuntimeException badArray(Object array) {
831 if (array == null) {
832 throw new NullPointerException("array == null");
833 } else if (!array.getClass().isArray()) {
834 throw notAnArray(array);
836 throw incompatibleType(array);