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();
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 native Object get(Object array, int index)
190 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
193 * Returns the value of the indexed component in the specified
194 * array object, as a {@code boolean}.
196 * @param array the array
197 * @param index the index
198 * @return the value of the indexed component in the specified array
199 * @exception NullPointerException If the specified object is null
200 * @exception IllegalArgumentException If the specified object is not
201 * an array, or if the indexed element cannot be converted to the
202 * return type by an identity or widening conversion
203 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
204 * argument is negative, or if it is greater than or equal to the
205 * length of the specified array
208 public static native boolean getBoolean(Object array, int index)
209 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
212 * Returns the value of the indexed component in the specified
213 * array object, as a {@code byte}.
215 * @param array the array
216 * @param index the index
217 * @return the value of the indexed component in the specified array
218 * @exception NullPointerException If the specified object is null
219 * @exception IllegalArgumentException If the specified object is not
220 * an array, or if the indexed element cannot be converted to the
221 * return type by an identity or widening conversion
222 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
223 * argument is negative, or if it is greater than or equal to the
224 * length of the specified array
227 public static native byte getByte(Object array, int index)
228 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
231 * Returns the value of the indexed component in the specified
232 * array object, as a {@code char}.
234 * @param array the array
235 * @param index the index
236 * @return the value of the indexed component in the specified array
237 * @exception NullPointerException If the specified object is null
238 * @exception IllegalArgumentException If the specified object is not
239 * an array, or if the indexed element cannot be converted to the
240 * return type by an identity or widening conversion
241 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
242 * argument is negative, or if it is greater than or equal to the
243 * length of the specified array
246 public static native char getChar(Object array, int index)
247 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
250 * Returns the value of the indexed component in the specified
251 * array object, as a {@code short}.
253 * @param array the array
254 * @param index the index
255 * @return the value of the indexed component in the specified array
256 * @exception NullPointerException If the specified object is null
257 * @exception IllegalArgumentException If the specified object is not
258 * an array, or if the indexed element cannot be converted to the
259 * return type by an identity or widening conversion
260 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
261 * argument is negative, or if it is greater than or equal to the
262 * length of the specified array
265 public static native short getShort(Object array, int index)
266 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
269 * Returns the value of the indexed component in the specified
270 * array object, as an {@code int}.
272 * @param array the array
273 * @param index the index
274 * @return the value of the indexed component in the specified array
275 * @exception NullPointerException If the specified object is null
276 * @exception IllegalArgumentException If the specified object is not
277 * an array, or if the indexed element cannot be converted to the
278 * return type by an identity or widening conversion
279 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
280 * argument is negative, or if it is greater than or equal to the
281 * length of the specified array
284 public static native int getInt(Object array, int index)
285 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
288 * Returns the value of the indexed component in the specified
289 * array object, as a {@code long}.
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 native long getLong(Object array, int index)
304 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
307 * Returns the value of the indexed component in the specified
308 * array object, as a {@code float}.
310 * @param array the array
311 * @param index the index
312 * @return the value of the indexed component in the specified array
313 * @exception NullPointerException If the specified object is null
314 * @exception IllegalArgumentException If the specified object is not
315 * an array, or if the indexed element cannot be converted to the
316 * return type by an identity or widening conversion
317 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
318 * argument is negative, or if it is greater than or equal to the
319 * length of the specified array
322 public static native float getFloat(Object array, int index)
323 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
326 * Returns the value of the indexed component in the specified
327 * array object, as a {@code double}.
329 * @param array the array
330 * @param index the index
331 * @return the value of the indexed component in the specified array
332 * @exception NullPointerException If the specified object is null
333 * @exception IllegalArgumentException If the specified object is not
334 * an array, or if the indexed element cannot be converted to the
335 * return type by an identity or widening conversion
336 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
337 * argument is negative, or if it is greater than or equal to the
338 * length of the specified array
341 public static native double getDouble(Object array, int index)
342 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
345 * Sets the value of the indexed component of the specified array
346 * object to the specified new value. The new value is first
347 * automatically unwrapped if the array has a primitive component
349 * @param array the array
350 * @param index the index into the array
351 * @param value the new value of the indexed component
352 * @exception NullPointerException If the specified object argument
354 * @exception IllegalArgumentException If the specified object argument
355 * is not an array, or if the array component type is primitive and
356 * an unwrapping conversion fails
357 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
358 * argument is negative, or if it is greater than or equal to
359 * the length of the specified array
361 public static native void set(Object array, int index, Object value)
362 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
365 * Sets the value of the indexed component of the specified array
366 * object to the specified {@code boolean} value.
367 * @param array the array
368 * @param index the index into the array
369 * @param z the new value of the indexed component
370 * @exception NullPointerException If the specified object argument
372 * @exception IllegalArgumentException If the specified object argument
373 * is not an array, or if the specified value cannot be converted
374 * to the underlying array's component type by an identity or a
375 * primitive widening conversion
376 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
377 * argument is negative, or if it is greater than or equal to
378 * the length of the specified array
381 public static native void setBoolean(Object array, int index, boolean z)
382 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
385 * Sets the value of the indexed component of the specified array
386 * object to the specified {@code byte} value.
387 * @param array the array
388 * @param index the index into the array
389 * @param b the new value of the indexed component
390 * @exception NullPointerException If the specified object argument
392 * @exception IllegalArgumentException If the specified object argument
393 * is not an array, or if the specified value cannot be converted
394 * to the underlying array's component type by an identity or a
395 * primitive widening conversion
396 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
397 * argument is negative, or if it is greater than or equal to
398 * the length of the specified array
401 public static native void setByte(Object array, int index, byte b)
402 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
405 * Sets the value of the indexed component of the specified array
406 * object to the specified {@code char} value.
407 * @param array the array
408 * @param index the index into the array
409 * @param c the new value of the indexed component
410 * @exception NullPointerException If the specified object argument
412 * @exception IllegalArgumentException If the specified object argument
413 * is not an array, or if the specified value cannot be converted
414 * to the underlying array's component type by an identity or a
415 * primitive widening conversion
416 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
417 * argument is negative, or if it is greater than or equal to
418 * the length of the specified array
421 public static native void setChar(Object array, int index, char c)
422 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
425 * Sets the value of the indexed component of the specified array
426 * object to the specified {@code short} value.
427 * @param array the array
428 * @param index the index into the array
429 * @param s the new value of the indexed component
430 * @exception NullPointerException If the specified object argument
432 * @exception IllegalArgumentException If the specified object argument
433 * is not an array, or if the specified value cannot be converted
434 * to the underlying array's component type by an identity or a
435 * primitive widening conversion
436 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
437 * argument is negative, or if it is greater than or equal to
438 * the length of the specified array
441 public static native void setShort(Object array, int index, short s)
442 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
445 * Sets the value of the indexed component of the specified array
446 * object to the specified {@code int} value.
447 * @param array the array
448 * @param index the index into the array
449 * @param i the new value of the indexed component
450 * @exception NullPointerException If the specified object argument
452 * @exception IllegalArgumentException If the specified object argument
453 * is not an array, or if the specified value cannot be converted
454 * to the underlying array's component type by an identity or a
455 * primitive widening conversion
456 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
457 * argument is negative, or if it is greater than or equal to
458 * the length of the specified array
461 public static native void setInt(Object array, int index, int i)
462 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
465 * Sets the value of the indexed component of the specified array
466 * object to the specified {@code long} value.
467 * @param array the array
468 * @param index the index into the array
469 * @param l the new value of the indexed component
470 * @exception NullPointerException If the specified object argument
472 * @exception IllegalArgumentException If the specified object argument
473 * is not an array, or if the specified value cannot be converted
474 * to the underlying array's component type by an identity or a
475 * primitive widening conversion
476 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
477 * argument is negative, or if it is greater than or equal to
478 * the length of the specified array
481 public static native void setLong(Object array, int index, long l)
482 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
485 * Sets the value of the indexed component of the specified array
486 * object to the specified {@code float} value.
487 * @param array the array
488 * @param index the index into the array
489 * @param f the new value of the indexed component
490 * @exception NullPointerException If the specified object argument
492 * @exception IllegalArgumentException If the specified object argument
493 * is not an array, or if the specified value cannot be converted
494 * to the underlying array's component type by an identity or a
495 * primitive widening conversion
496 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
497 * argument is negative, or if it is greater than or equal to
498 * the length of the specified array
501 public static native void setFloat(Object array, int index, float f)
502 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
505 * Sets the value of the indexed component of the specified array
506 * object to the specified {@code double} value.
507 * @param array the array
508 * @param index the index into the array
509 * @param d the new value of the indexed component
510 * @exception NullPointerException If the specified object argument
512 * @exception IllegalArgumentException If the specified object argument
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
515 * primitive widening conversion
516 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
517 * argument is negative, or if it is greater than or equal to
518 * the length of the specified array
521 public static native void setDouble(Object array, int index, double d)
522 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
528 @JavaScriptBody(args = { "primitive", "sig", "length" }, body =
529 "var arr = new Array(length);\n"
530 + "var value = primitive ? 0 : null;\n"
531 + "for(var i = 0; i < length; i++) arr[i] = value;\n"
532 + "arr.jvmName = sig;\n"
535 private static native Object newArray(boolean primitive, String sig, int length);
537 private static Object multiNewArray(String sig, int[] dims, int index)
538 throws IllegalArgumentException, NegativeArraySizeException {
539 if (dims.length == index + 1) {
540 return newArray(sig.length() == 2, sig, dims[index]);
542 Object[] arr = (Object[]) newArray(false, sig, dims[index]);
543 String compsig = sig.substring(1);
544 for (int i = 0; i < arr.length; i++) {
545 arr[i] = multiNewArray(compsig, dims, index + 1);