2 * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
26 package java.lang.reflect;
28 import org.apidesign.bck2brwsr.core.JavaScriptBody;
29 import org.apidesign.bck2brwsr.core.JavaScriptPrototype;
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
36 * operation, but throws an {@code IllegalArgumentException} if a narrowing
37 * conversion would occur.
39 * @author Nakul Saraiya
41 @JavaScriptPrototype(prototype = "new Array", container = "Array.prototype")
46 * Constructor. Class Array is not instantiable.
51 * Creates a new array with the specified component type and
53 * Invoking this method is equivalent to creating an array
58 * Array.newInstance(componentType, x);
62 * @param componentType the {@code Class} object representing the
63 * component type of the new array
64 * @param length the length of the new array
65 * @return the new array
66 * @exception NullPointerException if the specified
67 * {@code componentType} parameter is null
68 * @exception IllegalArgumentException if componentType is {@link Void#TYPE}
69 * @exception NegativeArraySizeException if the specified {@code length}
72 public static Object newInstance(Class<?> componentType, int length)
73 throws NegativeArraySizeException {
75 throw new NegativeArraySizeException();
77 String sig = findSignature(componentType);
78 return newArray(componentType.isPrimitive(), sig, length);
81 private static String findSignature(Class<?> type) {
82 if (type == Integer.TYPE) {
85 if (type == Long.TYPE) {
88 if (type == Double.TYPE) {
91 if (type == Float.TYPE) {
94 if (type == Byte.TYPE) {
97 if (type == Boolean.TYPE) {
100 if (type == Short.TYPE) {
103 if (type == Character.TYPE) {
106 if (type.getName().equals("void")) {
107 throw new IllegalStateException("Can't create array for " + type);
109 return "[L" + type.getName() + ";";
112 * Creates a new array
113 * with the specified component type and dimensions.
114 * If {@code componentType}
115 * represents a non-array class or interface, the new array
116 * has {@code dimensions.length} dimensions and
117 * {@code componentType} as its component type. If
118 * {@code componentType} represents an array class, the
119 * number of dimensions of the new array is equal to the sum
120 * of {@code dimensions.length} and the number of
121 * dimensions of {@code componentType}. In this case, the
122 * component type of the new array is the component type of
123 * {@code componentType}.
125 * <p>The number of dimensions of the new array must not
126 * exceed the number of array dimensions supported by the
127 * implementation (typically 255).
129 * @param componentType the {@code Class} object representing the component
130 * type of the new array
131 * @param dimensions an array of {@code int} representing the dimensions of
133 * @return the new array
134 * @exception NullPointerException if the specified
135 * {@code componentType} argument is null
136 * @exception IllegalArgumentException if the specified {@code dimensions}
137 * argument is a zero-dimensional array, or if the number of
138 * requested dimensions exceeds the limit on the number of array dimensions
139 * supported by the implementation (typically 255), or if componentType
140 * is {@link Void#TYPE}.
141 * @exception NegativeArraySizeException if any of the components in
142 * the specified {@code dimensions} argument is negative.
144 public static Object newInstance(Class<?> componentType, int... dimensions)
145 throws IllegalArgumentException, NegativeArraySizeException {
146 StringBuilder sig = new StringBuilder();
147 for (int i = 1; i < dimensions.length; i++) {
150 sig.append(findSignature(componentType));
151 return multiNewArray(sig.toString(), dimensions, 0);
155 * Returns the length of the specified array object, as an {@code int}.
157 * @param array the array
158 * @return the length of the array
159 * @exception IllegalArgumentException if the object argument is not
162 public static int getLength(Object array)
163 throws IllegalArgumentException {
164 if (!array.getClass().isArray()) {
165 throw new IllegalArgumentException("Argument is not an array");
167 return length(array);
170 @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
171 private static native int length(Object arr);
174 * Returns the value of the indexed component in the specified
175 * array object. The value is automatically wrapped in an object
176 * if it has a primitive type.
178 * @param array the array
179 * @param index the index
180 * @return the (possibly wrapped) value of the indexed component in
181 * the specified array
182 * @exception NullPointerException If the specified object is null
183 * @exception IllegalArgumentException If the specified object is not
185 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
186 * argument is negative, or if it is greater than or equal to the
187 * length of the specified array
189 public static Object get(Object array, int index)
190 throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
191 if (array.getClass().getComponentType().isPrimitive()) {
192 throw new IllegalArgumentException();
194 return ((Object[])array)[index];
199 * Returns the value of the indexed component in the specified
200 * array object, as a {@code boolean}.
202 * @param array the array
203 * @param index the index
204 * @return the value of the indexed component in the specified array
205 * @exception NullPointerException If the specified object is null
206 * @exception IllegalArgumentException If the specified object is not
207 * an array, or if the indexed element cannot be converted to the
208 * return type by an identity or widening conversion
209 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
210 * argument is negative, or if it is greater than or equal to the
211 * length of the specified array
214 public static native boolean getBoolean(Object array, int index)
215 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
218 * Returns the value of the indexed component in the specified
219 * array object, as a {@code byte}.
221 * @param array the array
222 * @param index the index
223 * @return the value of the indexed component in the specified array
224 * @exception NullPointerException If the specified object is null
225 * @exception IllegalArgumentException If the specified object is not
226 * an array, or if the indexed element cannot be converted to the
227 * return type by an identity or widening conversion
228 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
229 * argument is negative, or if it is greater than or equal to the
230 * length of the specified array
233 public static byte getByte(Object array, int index)
234 throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
235 if (array.getClass().getComponentType() != Byte.TYPE) {
236 throw new IllegalArgumentException();
238 byte[] arr = (byte[]) array;
243 * Returns the value of the indexed component in the specified
244 * array object, as a {@code char}.
246 * @param array the array
247 * @param index the index
248 * @return the value of the indexed component in the specified array
249 * @exception NullPointerException If the specified object is null
250 * @exception IllegalArgumentException If the specified object is not
251 * an array, or if the indexed element cannot be converted to the
252 * return type by an identity or widening conversion
253 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
254 * argument is negative, or if it is greater than or equal to the
255 * length of the specified array
258 public static native char getChar(Object array, int index)
259 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
262 * Returns the value of the indexed component in the specified
263 * array object, as a {@code short}.
265 * @param array the array
266 * @param index the index
267 * @return the value of the indexed component in the specified array
268 * @exception NullPointerException If the specified object is null
269 * @exception IllegalArgumentException If the specified object is not
270 * an array, or if the indexed element cannot be converted to the
271 * return type by an identity or widening conversion
272 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
273 * argument is negative, or if it is greater than or equal to the
274 * length of the specified array
277 public static short getShort(Object array, int index)
278 throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
279 final Class<?> t = array.getClass().getComponentType();
280 if (t == Short.TYPE) {
281 short[] arr = (short[]) array;
284 return getByte(array, index);
288 * Returns the value of the indexed component in the specified
289 * array object, as an {@code int}.
291 * @param array the array
292 * @param index the index
293 * @return the value of the indexed component in the specified array
294 * @exception NullPointerException If the specified object is null
295 * @exception IllegalArgumentException If the specified object is not
296 * an array, or if the indexed element cannot be converted to the
297 * return type by an identity or widening conversion
298 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
299 * argument is negative, or if it is greater than or equal to the
300 * length of the specified array
303 public static int getInt(Object array, int index)
304 throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
305 final Class<?> t = array.getClass().getComponentType();
306 if (t == Integer.TYPE) {
307 int[] arr = (int[]) array;
310 return getShort(array, index);
314 * Returns the value of the indexed component in the specified
315 * array object, as a {@code long}.
317 * @param array the array
318 * @param index the index
319 * @return the value of the indexed component in the specified array
320 * @exception NullPointerException If the specified object is null
321 * @exception IllegalArgumentException If the specified object is not
322 * an array, or if the indexed element cannot be converted to the
323 * return type by an identity or widening conversion
324 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
325 * argument is negative, or if it is greater than or equal to the
326 * length of the specified array
329 public static long getLong(Object array, int index)
330 throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
331 final Class<?> t = array.getClass().getComponentType();
332 if (t == Long.TYPE) {
333 long[] arr = (long[]) array;
336 return getInt(array, index);
340 * Returns the value of the indexed component in the specified
341 * array object, as a {@code float}.
343 * @param array the array
344 * @param index the index
345 * @return the value of the indexed component in the specified array
346 * @exception NullPointerException If the specified object is null
347 * @exception IllegalArgumentException If the specified object is not
348 * an array, or if the indexed element cannot be converted to the
349 * return type by an identity or widening conversion
350 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
351 * argument is negative, or if it is greater than or equal to the
352 * length of the specified array
355 public static float getFloat(Object array, int index)
356 throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
357 final Class<?> t = array.getClass().getComponentType();
358 if (t == Float.TYPE) {
359 float[] arr = (float[]) array;
362 return getLong(array, index);
366 * Returns the value of the indexed component in the specified
367 * array object, as a {@code double}.
369 * @param array the array
370 * @param index the index
371 * @return the value of the indexed component in the specified array
372 * @exception NullPointerException If the specified object is null
373 * @exception IllegalArgumentException If the specified object is not
374 * an array, or if the indexed element cannot be converted to the
375 * return type by an identity or widening conversion
376 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
377 * argument is negative, or if it is greater than or equal to the
378 * length of the specified array
381 public static double getDouble(Object array, int index)
382 throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
383 final Class<?> t = array.getClass().getComponentType();
384 if (t == Double.TYPE) {
385 double[] arr = (double[]) array;
388 return getFloat(array, index);
392 * Sets the value of the indexed component of the specified array
393 * object to the specified new value. The new value is first
394 * automatically unwrapped if the array has a primitive component
396 * @param array the array
397 * @param index the index into the array
398 * @param value the new value of the indexed component
399 * @exception NullPointerException If the specified object argument
401 * @exception IllegalArgumentException If the specified object argument
402 * is not an array, or if the array component type is primitive and
403 * an unwrapping conversion fails
404 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
405 * argument is negative, or if it is greater than or equal to
406 * the length of the specified array
408 public static void set(Object array, int index, Object value)
409 throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
410 if (array.getClass().getComponentType().isPrimitive()) {
411 throw new IllegalArgumentException();
413 Object[] arr = (Object[])array;
419 * Sets the value of the indexed component of the specified array
420 * object to the specified {@code boolean} value.
421 * @param array the array
422 * @param index the index into the array
423 * @param z the new value of the indexed component
424 * @exception NullPointerException If the specified object argument
426 * @exception IllegalArgumentException If the specified object argument
427 * is not an array, or if the specified value cannot be converted
428 * to the underlying array's component type by an identity or a
429 * primitive widening conversion
430 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
431 * argument is negative, or if it is greater than or equal to
432 * the length of the specified array
435 public static native void setBoolean(Object array, int index, boolean z)
436 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
439 * Sets the value of the indexed component of the specified array
440 * object to the specified {@code byte} value.
441 * @param array the array
442 * @param index the index into the array
443 * @param b the new value of the indexed component
444 * @exception NullPointerException If the specified object argument
446 * @exception IllegalArgumentException If the specified object argument
447 * is not an array, or if the specified value cannot be converted
448 * to the underlying array's component type by an identity or a
449 * primitive widening conversion
450 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
451 * argument is negative, or if it is greater than or equal to
452 * the length of the specified array
455 public static void setByte(Object array, int index, byte b)
456 throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
457 Class<?> t = array.getClass().getComponentType();
458 if (t == Byte.TYPE) {
459 byte[] arr = (byte[]) array;
462 setShort(array, index, b);
467 * Sets the value of the indexed component of the specified array
468 * object to the specified {@code char} value.
469 * @param array the array
470 * @param index the index into the array
471 * @param c the new value of the indexed component
472 * @exception NullPointerException If the specified object argument
474 * @exception IllegalArgumentException If the specified object argument
475 * is not an array, or if the specified value cannot be converted
476 * to the underlying array's component type by an identity or a
477 * primitive widening conversion
478 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
479 * argument is negative, or if it is greater than or equal to
480 * the length of the specified array
483 public static native void setChar(Object array, int index, char c)
484 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
487 * Sets the value of the indexed component of the specified array
488 * object to the specified {@code short} value.
489 * @param array the array
490 * @param index the index into the array
491 * @param s the new value of the indexed component
492 * @exception NullPointerException If the specified object argument
494 * @exception IllegalArgumentException If the specified object argument
495 * is not an array, or if the specified value cannot be converted
496 * to the underlying array's component type by an identity or a
497 * primitive widening conversion
498 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
499 * argument is negative, or if it is greater than or equal to
500 * the length of the specified array
503 public static void setShort(Object array, int index, short s)
504 throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
505 Class<?> t = array.getClass().getComponentType();
506 if (t == Short.TYPE) {
507 short[] arr = (short[]) array;
510 setInt(array, index, s);
516 * Sets the value of the indexed component of the specified array
517 * object to the specified {@code int} value.
518 * @param array the array
519 * @param index the index into the array
520 * @param i the new value of the indexed component
521 * @exception NullPointerException If the specified object argument
523 * @exception IllegalArgumentException If the specified object argument
524 * is not an array, or if the specified value cannot be converted
525 * to the underlying array's component type by an identity or a
526 * primitive widening conversion
527 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
528 * argument is negative, or if it is greater than or equal to
529 * the length of the specified array
532 public static void setInt(Object array, int index, int i)
533 throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
534 Class<?> t = array.getClass().getComponentType();
535 if (t == Integer.TYPE) {
536 long[] arr = (long[]) array;
539 setLong(array, index, i);
544 * Sets the value of the indexed component of the specified array
545 * object to the specified {@code long} value.
546 * @param array the array
547 * @param index the index into the array
548 * @param l the new value of the indexed component
549 * @exception NullPointerException If the specified object argument
551 * @exception IllegalArgumentException If the specified object argument
552 * is not an array, or if the specified value cannot be converted
553 * to the underlying array's component type by an identity or a
554 * primitive widening conversion
555 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
556 * argument is negative, or if it is greater than or equal to
557 * the length of the specified array
560 public static void setLong(Object array, int index, long l)
561 throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
562 Class<?> t = array.getClass().getComponentType();
563 if (t == Long.TYPE) {
564 long[] arr = (long[]) array;
567 setFloat(array, index, l);
572 * Sets the value of the indexed component of the specified array
573 * object to the specified {@code float} value.
574 * @param array the array
575 * @param index the index into the array
576 * @param f the new value of the indexed component
577 * @exception NullPointerException If the specified object argument
579 * @exception IllegalArgumentException If the specified object argument
580 * is not an array, or if the specified value cannot be converted
581 * to the underlying array's component type by an identity or a
582 * primitive widening conversion
583 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
584 * argument is negative, or if it is greater than or equal to
585 * the length of the specified array
588 public static void setFloat(Object array, int index, float f)
589 throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
590 Class<?> t = array.getClass().getComponentType();
591 if (t == Float.TYPE) {
592 float[] arr = (float[])array;
595 setDouble(array, index, f);
600 * Sets the value of the indexed component of the specified array
601 * object to the specified {@code double} value.
602 * @param array the array
603 * @param index the index into the array
604 * @param d the new value of the indexed component
605 * @exception NullPointerException If the specified object argument
607 * @exception IllegalArgumentException If the specified object argument
608 * is not an array, or if the specified value cannot be converted
609 * to the underlying array's component type by an identity or a
610 * primitive widening conversion
611 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
612 * argument is negative, or if it is greater than or equal to
613 * the length of the specified array
616 public static void setDouble(Object array, int index, double d)
617 throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
618 Class<?> t = array.getClass().getComponentType();
619 if (t == Double.TYPE) {
620 double[] arr = (double[])array;
623 throw new IllegalArgumentException("argument type mismatch");
631 @JavaScriptBody(args = { "primitive", "sig", "length" }, body =
632 "var arr = new Array(length);\n"
633 + "var value = primitive ? 0 : null;\n"
634 + "for(var i = 0; i < length; i++) arr[i] = value;\n"
635 + "arr.jvmName = sig;\n"
638 private static native Object newArray(boolean primitive, String sig, int length);
640 private static Object multiNewArray(String sig, int[] dims, int index)
641 throws IllegalArgumentException, NegativeArraySizeException {
642 if (dims.length == index + 1) {
643 return newArray(sig.length() == 2, sig, dims[index]);
645 Object[] arr = (Object[]) newArray(false, sig, dims[index]);
646 String compsig = sig.substring(1);
647 for (int i = 0; i < arr.length; i++) {
648 arr[i] = multiNewArray(compsig, dims, index + 1);