Reusing methods from java.lang.reflect.Array when creating new instances of one-dimensional arrays
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 return multiNewArray(componentType, dimensions);
150 * Returns the length of the specified array object, as an {@code int}.
152 * @param array the array
153 * @return the length of the array
154 * @exception IllegalArgumentException if the object argument is not
157 public static native int getLength(Object array)
158 throws IllegalArgumentException;
161 * Returns the value of the indexed component in the specified
162 * array object. The value is automatically wrapped in an object
163 * if it has a primitive type.
165 * @param array the array
166 * @param index the index
167 * @return the (possibly wrapped) value of the indexed component in
168 * the specified array
169 * @exception NullPointerException If the specified object is null
170 * @exception IllegalArgumentException If the specified object is not
172 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
173 * argument is negative, or if it is greater than or equal to the
174 * length of the specified array
176 public static native Object get(Object array, int index)
177 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
180 * Returns the value of the indexed component in the specified
181 * array object, as a {@code boolean}.
183 * @param array the array
184 * @param index the index
185 * @return the value of the indexed component in the specified array
186 * @exception NullPointerException If the specified object is null
187 * @exception IllegalArgumentException If the specified object is not
188 * an array, or if the indexed element cannot be converted to the
189 * return type by an identity or widening conversion
190 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
191 * argument is negative, or if it is greater than or equal to the
192 * length of the specified array
195 public static native boolean getBoolean(Object array, int index)
196 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
199 * Returns the value of the indexed component in the specified
200 * array object, as a {@code byte}.
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 byte getByte(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 char}.
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 native char getChar(Object array, int index)
234 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
237 * Returns the value of the indexed component in the specified
238 * array object, as a {@code short}.
240 * @param array the array
241 * @param index the index
242 * @return the value of the indexed component in the specified array
243 * @exception NullPointerException If the specified object is null
244 * @exception IllegalArgumentException If the specified object is not
245 * an array, or if the indexed element cannot be converted to the
246 * return type by an identity or widening conversion
247 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
248 * argument is negative, or if it is greater than or equal to the
249 * length of the specified array
252 public static native short getShort(Object array, int index)
253 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
256 * Returns the value of the indexed component in the specified
257 * array object, as an {@code int}.
259 * @param array the array
260 * @param index the index
261 * @return the value of the indexed component in the specified array
262 * @exception NullPointerException If the specified object is null
263 * @exception IllegalArgumentException If the specified object is not
264 * an array, or if the indexed element cannot be converted to the
265 * return type by an identity or widening conversion
266 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
267 * argument is negative, or if it is greater than or equal to the
268 * length of the specified array
271 public static native int getInt(Object array, int index)
272 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
275 * Returns the value of the indexed component in the specified
276 * array object, as a {@code long}.
278 * @param array the array
279 * @param index the index
280 * @return the value of the indexed component in the specified array
281 * @exception NullPointerException If the specified object is null
282 * @exception IllegalArgumentException If the specified object is not
283 * an array, or if the indexed element cannot be converted to the
284 * return type by an identity or widening conversion
285 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
286 * argument is negative, or if it is greater than or equal to the
287 * length of the specified array
290 public static native long getLong(Object array, int index)
291 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
294 * Returns the value of the indexed component in the specified
295 * array object, as a {@code float}.
297 * @param array the array
298 * @param index the index
299 * @return the value of the indexed component in the specified array
300 * @exception NullPointerException If the specified object is null
301 * @exception IllegalArgumentException If the specified object is not
302 * an array, or if the indexed element cannot be converted to the
303 * return type by an identity or widening conversion
304 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
305 * argument is negative, or if it is greater than or equal to the
306 * length of the specified array
309 public static native float getFloat(Object array, int index)
310 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
313 * Returns the value of the indexed component in the specified
314 * array object, as a {@code double}.
316 * @param array the array
317 * @param index the index
318 * @return the value of the indexed component in the specified array
319 * @exception NullPointerException If the specified object is null
320 * @exception IllegalArgumentException If the specified object is not
321 * an array, or if the indexed element cannot be converted to the
322 * return type by an identity or widening conversion
323 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
324 * argument is negative, or if it is greater than or equal to the
325 * length of the specified array
328 public static native double getDouble(Object array, int index)
329 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
332 * Sets the value of the indexed component of the specified array
333 * object to the specified new value. The new value is first
334 * automatically unwrapped if the array has a primitive component
336 * @param array the array
337 * @param index the index into the array
338 * @param value the new value of the indexed component
339 * @exception NullPointerException If the specified object argument
341 * @exception IllegalArgumentException If the specified object argument
342 * is not an array, or if the array component type is primitive and
343 * an unwrapping conversion fails
344 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
345 * argument is negative, or if it is greater than or equal to
346 * the length of the specified array
348 public static native void set(Object array, int index, Object value)
349 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
352 * Sets the value of the indexed component of the specified array
353 * object to the specified {@code boolean} value.
354 * @param array the array
355 * @param index the index into the array
356 * @param z the new value of the indexed component
357 * @exception NullPointerException If the specified object argument
359 * @exception IllegalArgumentException If the specified object argument
360 * is not an array, or if the specified value cannot be converted
361 * to the underlying array's component type by an identity or a
362 * primitive widening conversion
363 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
364 * argument is negative, or if it is greater than or equal to
365 * the length of the specified array
368 public static native void setBoolean(Object array, int index, boolean z)
369 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
372 * Sets the value of the indexed component of the specified array
373 * object to the specified {@code byte} value.
374 * @param array the array
375 * @param index the index into the array
376 * @param b the new value of the indexed component
377 * @exception NullPointerException If the specified object argument
379 * @exception IllegalArgumentException If the specified object argument
380 * is not an array, or if the specified value cannot be converted
381 * to the underlying array's component type by an identity or a
382 * primitive widening conversion
383 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
384 * argument is negative, or if it is greater than or equal to
385 * the length of the specified array
388 public static native void setByte(Object array, int index, byte b)
389 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
392 * Sets the value of the indexed component of the specified array
393 * object to the specified {@code char} value.
394 * @param array the array
395 * @param index the index into the array
396 * @param c the new value of the indexed component
397 * @exception NullPointerException If the specified object argument
399 * @exception IllegalArgumentException If the specified object argument
400 * is not an array, or if the specified value cannot be converted
401 * to the underlying array's component type by an identity or a
402 * primitive widening conversion
403 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
404 * argument is negative, or if it is greater than or equal to
405 * the length of the specified array
408 public static native void setChar(Object array, int index, char c)
409 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
412 * Sets the value of the indexed component of the specified array
413 * object to the specified {@code short} value.
414 * @param array the array
415 * @param index the index into the array
416 * @param s the new value of the indexed component
417 * @exception NullPointerException If the specified object argument
419 * @exception IllegalArgumentException If the specified object argument
420 * is not an array, or if the specified value cannot be converted
421 * to the underlying array's component type by an identity or a
422 * primitive widening conversion
423 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
424 * argument is negative, or if it is greater than or equal to
425 * the length of the specified array
428 public static native void setShort(Object array, int index, short s)
429 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
432 * Sets the value of the indexed component of the specified array
433 * object to the specified {@code int} value.
434 * @param array the array
435 * @param index the index into the array
436 * @param i the new value of the indexed component
437 * @exception NullPointerException If the specified object argument
439 * @exception IllegalArgumentException If the specified object argument
440 * is not an array, or if the specified value cannot be converted
441 * to the underlying array's component type by an identity or a
442 * primitive widening conversion
443 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
444 * argument is negative, or if it is greater than or equal to
445 * the length of the specified array
448 public static native void setInt(Object array, int index, int i)
449 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
452 * Sets the value of the indexed component of the specified array
453 * object to the specified {@code long} value.
454 * @param array the array
455 * @param index the index into the array
456 * @param l the new value of the indexed component
457 * @exception NullPointerException If the specified object argument
459 * @exception IllegalArgumentException If the specified object argument
460 * is not an array, or if the specified value cannot be converted
461 * to the underlying array's component type by an identity or a
462 * primitive widening conversion
463 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
464 * argument is negative, or if it is greater than or equal to
465 * the length of the specified array
468 public static native void setLong(Object array, int index, long l)
469 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
472 * Sets the value of the indexed component of the specified array
473 * object to the specified {@code float} value.
474 * @param array the array
475 * @param index the index into the array
476 * @param f the new value of the indexed component
477 * @exception NullPointerException If the specified object argument
479 * @exception IllegalArgumentException If the specified object argument
480 * is not an array, or if the specified value cannot be converted
481 * to the underlying array's component type by an identity or a
482 * primitive widening conversion
483 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
484 * argument is negative, or if it is greater than or equal to
485 * the length of the specified array
488 public static native void setFloat(Object array, int index, float f)
489 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
492 * Sets the value of the indexed component of the specified array
493 * object to the specified {@code double} value.
494 * @param array the array
495 * @param index the index into the array
496 * @param d the new value of the indexed component
497 * @exception NullPointerException If the specified object argument
499 * @exception IllegalArgumentException If the specified object argument
500 * is not an array, or if the specified value cannot be converted
501 * to the underlying array's component type by an identity or a
502 * primitive widening conversion
503 * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
504 * argument is negative, or if it is greater than or equal to
505 * the length of the specified array
508 public static native void setDouble(Object array, int index, double d)
509 throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
515 @JavaScriptBody(args = { "primitive", "sig", "length" }, body =
516 "var arr = new Array(length);\n"
517 + "var value = primitive ? 0 : null;\n"
518 + "for(var i = 0; i < length; i++) arr[i] = value;\n"
519 + "arr.jvmName = sig;\n"
522 private static native Object newArray(boolean primitive, String sig, int length);
524 private static native Object multiNewArray(Class componentType,
526 throws IllegalArgumentException, NegativeArraySizeException;