1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/mini/src/main/java/java/lang/reflect/Array.java Thu Jan 24 18:03:29 2013 +0100
1.3 @@ -0,0 +1,663 @@
1.4 +/*
1.5 + * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.lang.reflect;
1.30 +
1.31 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.32 +import org.apidesign.bck2brwsr.core.JavaScriptPrototype;
1.33 +
1.34 +/**
1.35 + * The {@code Array} class provides static methods to dynamically create and
1.36 + * access Java arrays.
1.37 + *
1.38 + * <p>{@code Array} permits widening conversions to occur during a get or set
1.39 + * operation, but throws an {@code IllegalArgumentException} if a narrowing
1.40 + * conversion would occur.
1.41 + *
1.42 + * @author Nakul Saraiya
1.43 + */
1.44 +@JavaScriptPrototype(prototype = "new Array", container = "Array.prototype")
1.45 +public final
1.46 +class Array {
1.47 +
1.48 + /**
1.49 + * Constructor. Class Array is not instantiable.
1.50 + */
1.51 + private Array() {}
1.52 +
1.53 + /**
1.54 + * Creates a new array with the specified component type and
1.55 + * length.
1.56 + * Invoking this method is equivalent to creating an array
1.57 + * as follows:
1.58 + * <blockquote>
1.59 + * <pre>
1.60 + * int[] x = {length};
1.61 + * Array.newInstance(componentType, x);
1.62 + * </pre>
1.63 + * </blockquote>
1.64 + *
1.65 + * @param componentType the {@code Class} object representing the
1.66 + * component type of the new array
1.67 + * @param length the length of the new array
1.68 + * @return the new array
1.69 + * @exception NullPointerException if the specified
1.70 + * {@code componentType} parameter is null
1.71 + * @exception IllegalArgumentException if componentType is {@link Void#TYPE}
1.72 + * @exception NegativeArraySizeException if the specified {@code length}
1.73 + * is negative
1.74 + */
1.75 + public static Object newInstance(Class<?> componentType, int length)
1.76 + throws NegativeArraySizeException {
1.77 + if (length < 0) {
1.78 + throw new NegativeArraySizeException();
1.79 + }
1.80 + String sig = findSignature(componentType);
1.81 + return newArray(componentType.isPrimitive(), sig, length);
1.82 + }
1.83 +
1.84 + private static String findSignature(Class<?> type) {
1.85 + if (type == Integer.TYPE) {
1.86 + return "[I";
1.87 + }
1.88 + if (type == Long.TYPE) {
1.89 + return "[J";
1.90 + }
1.91 + if (type == Double.TYPE) {
1.92 + return "[D";
1.93 + }
1.94 + if (type == Float.TYPE) {
1.95 + return "[F";
1.96 + }
1.97 + if (type == Byte.TYPE) {
1.98 + return "[B";
1.99 + }
1.100 + if (type == Boolean.TYPE) {
1.101 + return "[Z";
1.102 + }
1.103 + if (type == Short.TYPE) {
1.104 + return "[S";
1.105 + }
1.106 + if (type == Character.TYPE) {
1.107 + return "[C";
1.108 + }
1.109 + if (type.getName().equals("void")) {
1.110 + throw new IllegalStateException("Can't create array for " + type);
1.111 + }
1.112 + return "[L" + type.getName() + ";";
1.113 + }
1.114 + /**
1.115 + * Creates a new array
1.116 + * with the specified component type and dimensions.
1.117 + * If {@code componentType}
1.118 + * represents a non-array class or interface, the new array
1.119 + * has {@code dimensions.length} dimensions and
1.120 + * {@code componentType} as its component type. If
1.121 + * {@code componentType} represents an array class, the
1.122 + * number of dimensions of the new array is equal to the sum
1.123 + * of {@code dimensions.length} and the number of
1.124 + * dimensions of {@code componentType}. In this case, the
1.125 + * component type of the new array is the component type of
1.126 + * {@code componentType}.
1.127 + *
1.128 + * <p>The number of dimensions of the new array must not
1.129 + * exceed the number of array dimensions supported by the
1.130 + * implementation (typically 255).
1.131 + *
1.132 + * @param componentType the {@code Class} object representing the component
1.133 + * type of the new array
1.134 + * @param dimensions an array of {@code int} representing the dimensions of
1.135 + * the new array
1.136 + * @return the new array
1.137 + * @exception NullPointerException if the specified
1.138 + * {@code componentType} argument is null
1.139 + * @exception IllegalArgumentException if the specified {@code dimensions}
1.140 + * argument is a zero-dimensional array, or if the number of
1.141 + * requested dimensions exceeds the limit on the number of array dimensions
1.142 + * supported by the implementation (typically 255), or if componentType
1.143 + * is {@link Void#TYPE}.
1.144 + * @exception NegativeArraySizeException if any of the components in
1.145 + * the specified {@code dimensions} argument is negative.
1.146 + */
1.147 + public static Object newInstance(Class<?> componentType, int... dimensions)
1.148 + throws IllegalArgumentException, NegativeArraySizeException {
1.149 + StringBuilder sig = new StringBuilder();
1.150 + for (int i = 1; i < dimensions.length; i++) {
1.151 + sig.append('[');
1.152 + }
1.153 + sig.append(findSignature(componentType));
1.154 + return multiNewArray(sig.toString(), dimensions, 0);
1.155 + }
1.156 +
1.157 + /**
1.158 + * Returns the length of the specified array object, as an {@code int}.
1.159 + *
1.160 + * @param array the array
1.161 + * @return the length of the array
1.162 + * @exception IllegalArgumentException if the object argument is not
1.163 + * an array
1.164 + */
1.165 + public static int getLength(Object array)
1.166 + throws IllegalArgumentException {
1.167 + if (!array.getClass().isArray()) {
1.168 + throw new IllegalArgumentException("Argument is not an array");
1.169 + }
1.170 + return length(array);
1.171 + }
1.172 +
1.173 + @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
1.174 + private static native int length(Object arr);
1.175 +
1.176 + /**
1.177 + * Returns the value of the indexed component in the specified
1.178 + * array object. The value is automatically wrapped in an object
1.179 + * if it has a primitive type.
1.180 + *
1.181 + * @param array the array
1.182 + * @param index the index
1.183 + * @return the (possibly wrapped) value of the indexed component in
1.184 + * the specified array
1.185 + * @exception NullPointerException If the specified object is null
1.186 + * @exception IllegalArgumentException If the specified object is not
1.187 + * an array
1.188 + * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
1.189 + * argument is negative, or if it is greater than or equal to the
1.190 + * length of the specified array
1.191 + */
1.192 + public static Object get(Object array, int index)
1.193 + throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
1.194 + final Class<?> t = array.getClass().getComponentType();
1.195 + if (t.isPrimitive()) {
1.196 + return fromPrimitive(t, array, index);
1.197 + } else {
1.198 + return ((Object[])array)[index];
1.199 + }
1.200 + }
1.201 +
1.202 + /**
1.203 + * Returns the value of the indexed component in the specified
1.204 + * array object, as a {@code boolean}.
1.205 + *
1.206 + * @param array the array
1.207 + * @param index the index
1.208 + * @return the value of the indexed component in the specified array
1.209 + * @exception NullPointerException If the specified object is null
1.210 + * @exception IllegalArgumentException If the specified object is not
1.211 + * an array, or if the indexed element cannot be converted to the
1.212 + * return type by an identity or widening conversion
1.213 + * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
1.214 + * argument is negative, or if it is greater than or equal to the
1.215 + * length of the specified array
1.216 + * @see Array#get
1.217 + */
1.218 + public static native boolean getBoolean(Object array, int index)
1.219 + throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
1.220 +
1.221 + /**
1.222 + * Returns the value of the indexed component in the specified
1.223 + * array object, as a {@code byte}.
1.224 + *
1.225 + * @param array the array
1.226 + * @param index the index
1.227 + * @return the value of the indexed component in the specified array
1.228 + * @exception NullPointerException If the specified object is null
1.229 + * @exception IllegalArgumentException If the specified object is not
1.230 + * an array, or if the indexed element cannot be converted to the
1.231 + * return type by an identity or widening conversion
1.232 + * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
1.233 + * argument is negative, or if it is greater than or equal to the
1.234 + * length of the specified array
1.235 + * @see Array#get
1.236 + */
1.237 + public static byte getByte(Object array, int index)
1.238 + throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
1.239 + if (array.getClass().getComponentType() != Byte.TYPE) {
1.240 + throw new IllegalArgumentException();
1.241 + }
1.242 + byte[] arr = (byte[]) array;
1.243 + return arr[index];
1.244 + }
1.245 +
1.246 + /**
1.247 + * Returns the value of the indexed component in the specified
1.248 + * array object, as a {@code char}.
1.249 + *
1.250 + * @param array the array
1.251 + * @param index the index
1.252 + * @return the value of the indexed component in the specified array
1.253 + * @exception NullPointerException If the specified object is null
1.254 + * @exception IllegalArgumentException If the specified object is not
1.255 + * an array, or if the indexed element cannot be converted to the
1.256 + * return type by an identity or widening conversion
1.257 + * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
1.258 + * argument is negative, or if it is greater than or equal to the
1.259 + * length of the specified array
1.260 + * @see Array#get
1.261 + */
1.262 + public static native char getChar(Object array, int index)
1.263 + throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
1.264 +
1.265 + /**
1.266 + * Returns the value of the indexed component in the specified
1.267 + * array object, as a {@code short}.
1.268 + *
1.269 + * @param array the array
1.270 + * @param index the index
1.271 + * @return the value of the indexed component in the specified array
1.272 + * @exception NullPointerException If the specified object is null
1.273 + * @exception IllegalArgumentException If the specified object is not
1.274 + * an array, or if the indexed element cannot be converted to the
1.275 + * return type by an identity or widening conversion
1.276 + * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
1.277 + * argument is negative, or if it is greater than or equal to the
1.278 + * length of the specified array
1.279 + * @see Array#get
1.280 + */
1.281 + public static short getShort(Object array, int index)
1.282 + throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
1.283 + final Class<?> t = array.getClass().getComponentType();
1.284 + if (t == Short.TYPE) {
1.285 + short[] arr = (short[]) array;
1.286 + return arr[index];
1.287 + }
1.288 + return getByte(array, index);
1.289 + }
1.290 +
1.291 + /**
1.292 + * Returns the value of the indexed component in the specified
1.293 + * array object, as an {@code int}.
1.294 + *
1.295 + * @param array the array
1.296 + * @param index the index
1.297 + * @return the value of the indexed component in the specified array
1.298 + * @exception NullPointerException If the specified object is null
1.299 + * @exception IllegalArgumentException If the specified object is not
1.300 + * an array, or if the indexed element cannot be converted to the
1.301 + * return type by an identity or widening conversion
1.302 + * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
1.303 + * argument is negative, or if it is greater than or equal to the
1.304 + * length of the specified array
1.305 + * @see Array#get
1.306 + */
1.307 + public static int getInt(Object array, int index)
1.308 + throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
1.309 + final Class<?> t = array.getClass().getComponentType();
1.310 + if (t == Integer.TYPE) {
1.311 + int[] arr = (int[]) array;
1.312 + return arr[index];
1.313 + }
1.314 + return getShort(array, index);
1.315 + }
1.316 +
1.317 + /**
1.318 + * Returns the value of the indexed component in the specified
1.319 + * array object, as a {@code long}.
1.320 + *
1.321 + * @param array the array
1.322 + * @param index the index
1.323 + * @return the value of the indexed component in the specified array
1.324 + * @exception NullPointerException If the specified object is null
1.325 + * @exception IllegalArgumentException If the specified object is not
1.326 + * an array, or if the indexed element cannot be converted to the
1.327 + * return type by an identity or widening conversion
1.328 + * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
1.329 + * argument is negative, or if it is greater than or equal to the
1.330 + * length of the specified array
1.331 + * @see Array#get
1.332 + */
1.333 + public static long getLong(Object array, int index)
1.334 + throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
1.335 + final Class<?> t = array.getClass().getComponentType();
1.336 + if (t == Long.TYPE) {
1.337 + long[] arr = (long[]) array;
1.338 + return arr[index];
1.339 + }
1.340 + return getInt(array, index);
1.341 + }
1.342 +
1.343 + /**
1.344 + * Returns the value of the indexed component in the specified
1.345 + * array object, as a {@code float}.
1.346 + *
1.347 + * @param array the array
1.348 + * @param index the index
1.349 + * @return the value of the indexed component in the specified array
1.350 + * @exception NullPointerException If the specified object is null
1.351 + * @exception IllegalArgumentException If the specified object is not
1.352 + * an array, or if the indexed element cannot be converted to the
1.353 + * return type by an identity or widening conversion
1.354 + * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
1.355 + * argument is negative, or if it is greater than or equal to the
1.356 + * length of the specified array
1.357 + * @see Array#get
1.358 + */
1.359 + public static float getFloat(Object array, int index)
1.360 + throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
1.361 + final Class<?> t = array.getClass().getComponentType();
1.362 + if (t == Float.TYPE) {
1.363 + float[] arr = (float[]) array;
1.364 + return arr[index];
1.365 + }
1.366 + return getLong(array, index);
1.367 + }
1.368 +
1.369 + /**
1.370 + * Returns the value of the indexed component in the specified
1.371 + * array object, as a {@code double}.
1.372 + *
1.373 + * @param array the array
1.374 + * @param index the index
1.375 + * @return the value of the indexed component in the specified array
1.376 + * @exception NullPointerException If the specified object is null
1.377 + * @exception IllegalArgumentException If the specified object is not
1.378 + * an array, or if the indexed element cannot be converted to the
1.379 + * return type by an identity or widening conversion
1.380 + * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
1.381 + * argument is negative, or if it is greater than or equal to the
1.382 + * length of the specified array
1.383 + * @see Array#get
1.384 + */
1.385 + public static double getDouble(Object array, int index)
1.386 + throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
1.387 + final Class<?> t = array.getClass().getComponentType();
1.388 + if (t == Double.TYPE) {
1.389 + double[] arr = (double[]) array;
1.390 + return arr[index];
1.391 + }
1.392 + return getFloat(array, index);
1.393 + }
1.394 +
1.395 + /**
1.396 + * Sets the value of the indexed component of the specified array
1.397 + * object to the specified new value. The new value is first
1.398 + * automatically unwrapped if the array has a primitive component
1.399 + * type.
1.400 + * @param array the array
1.401 + * @param index the index into the array
1.402 + * @param value the new value of the indexed component
1.403 + * @exception NullPointerException If the specified object argument
1.404 + * is null
1.405 + * @exception IllegalArgumentException If the specified object argument
1.406 + * is not an array, or if the array component type is primitive and
1.407 + * an unwrapping conversion fails
1.408 + * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
1.409 + * argument is negative, or if it is greater than or equal to
1.410 + * the length of the specified array
1.411 + */
1.412 + public static void set(Object array, int index, Object value)
1.413 + throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
1.414 + if (array.getClass().getComponentType().isPrimitive()) {
1.415 + throw new IllegalArgumentException();
1.416 + } else {
1.417 + Object[] arr = (Object[])array;
1.418 + arr[index] = value;
1.419 + }
1.420 + }
1.421 +
1.422 + /**
1.423 + * Sets the value of the indexed component of the specified array
1.424 + * object to the specified {@code boolean} value.
1.425 + * @param array the array
1.426 + * @param index the index into the array
1.427 + * @param z the new value of the indexed component
1.428 + * @exception NullPointerException If the specified object argument
1.429 + * is null
1.430 + * @exception IllegalArgumentException If the specified object argument
1.431 + * is not an array, or if the specified value cannot be converted
1.432 + * to the underlying array's component type by an identity or a
1.433 + * primitive widening conversion
1.434 + * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
1.435 + * argument is negative, or if it is greater than or equal to
1.436 + * the length of the specified array
1.437 + * @see Array#set
1.438 + */
1.439 + public static native void setBoolean(Object array, int index, boolean z)
1.440 + throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
1.441 +
1.442 + /**
1.443 + * Sets the value of the indexed component of the specified array
1.444 + * object to the specified {@code byte} value.
1.445 + * @param array the array
1.446 + * @param index the index into the array
1.447 + * @param b the new value of the indexed component
1.448 + * @exception NullPointerException If the specified object argument
1.449 + * is null
1.450 + * @exception IllegalArgumentException If the specified object argument
1.451 + * is not an array, or if the specified value cannot be converted
1.452 + * to the underlying array's component type by an identity or a
1.453 + * primitive widening conversion
1.454 + * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
1.455 + * argument is negative, or if it is greater than or equal to
1.456 + * the length of the specified array
1.457 + * @see Array#set
1.458 + */
1.459 + public static void setByte(Object array, int index, byte b)
1.460 + throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
1.461 + Class<?> t = array.getClass().getComponentType();
1.462 + if (t == Byte.TYPE) {
1.463 + byte[] arr = (byte[]) array;
1.464 + arr[index] = b;
1.465 + } else {
1.466 + setShort(array, index, b);
1.467 + }
1.468 + }
1.469 +
1.470 + /**
1.471 + * Sets the value of the indexed component of the specified array
1.472 + * object to the specified {@code char} value.
1.473 + * @param array the array
1.474 + * @param index the index into the array
1.475 + * @param c the new value of the indexed component
1.476 + * @exception NullPointerException If the specified object argument
1.477 + * is null
1.478 + * @exception IllegalArgumentException If the specified object argument
1.479 + * is not an array, or if the specified value cannot be converted
1.480 + * to the underlying array's component type by an identity or a
1.481 + * primitive widening conversion
1.482 + * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
1.483 + * argument is negative, or if it is greater than or equal to
1.484 + * the length of the specified array
1.485 + * @see Array#set
1.486 + */
1.487 + public static native void setChar(Object array, int index, char c)
1.488 + throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
1.489 +
1.490 + /**
1.491 + * Sets the value of the indexed component of the specified array
1.492 + * object to the specified {@code short} value.
1.493 + * @param array the array
1.494 + * @param index the index into the array
1.495 + * @param s the new value of the indexed component
1.496 + * @exception NullPointerException If the specified object argument
1.497 + * is null
1.498 + * @exception IllegalArgumentException If the specified object argument
1.499 + * is not an array, or if the specified value cannot be converted
1.500 + * to the underlying array's component type by an identity or a
1.501 + * primitive widening conversion
1.502 + * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
1.503 + * argument is negative, or if it is greater than or equal to
1.504 + * the length of the specified array
1.505 + * @see Array#set
1.506 + */
1.507 + public static void setShort(Object array, int index, short s)
1.508 + throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
1.509 + Class<?> t = array.getClass().getComponentType();
1.510 + if (t == Short.TYPE) {
1.511 + short[] arr = (short[]) array;
1.512 + arr[index] = s;
1.513 + } else {
1.514 + setInt(array, index, s);
1.515 + }
1.516 +
1.517 + }
1.518 +
1.519 + /**
1.520 + * Sets the value of the indexed component of the specified array
1.521 + * object to the specified {@code int} value.
1.522 + * @param array the array
1.523 + * @param index the index into the array
1.524 + * @param i the new value of the indexed component
1.525 + * @exception NullPointerException If the specified object argument
1.526 + * is null
1.527 + * @exception IllegalArgumentException If the specified object argument
1.528 + * is not an array, or if the specified value cannot be converted
1.529 + * to the underlying array's component type by an identity or a
1.530 + * primitive widening conversion
1.531 + * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
1.532 + * argument is negative, or if it is greater than or equal to
1.533 + * the length of the specified array
1.534 + * @see Array#set
1.535 + */
1.536 + public static void setInt(Object array, int index, int i)
1.537 + throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
1.538 + Class<?> t = array.getClass().getComponentType();
1.539 + if (t == Integer.TYPE) {
1.540 + int[] arr = (int[]) array;
1.541 + arr[index] = i;
1.542 + } else {
1.543 + setLong(array, index, i);
1.544 + }
1.545 + }
1.546 +
1.547 + /**
1.548 + * Sets the value of the indexed component of the specified array
1.549 + * object to the specified {@code long} value.
1.550 + * @param array the array
1.551 + * @param index the index into the array
1.552 + * @param l the new value of the indexed component
1.553 + * @exception NullPointerException If the specified object argument
1.554 + * is null
1.555 + * @exception IllegalArgumentException If the specified object argument
1.556 + * is not an array, or if the specified value cannot be converted
1.557 + * to the underlying array's component type by an identity or a
1.558 + * primitive widening conversion
1.559 + * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
1.560 + * argument is negative, or if it is greater than or equal to
1.561 + * the length of the specified array
1.562 + * @see Array#set
1.563 + */
1.564 + public static void setLong(Object array, int index, long l)
1.565 + throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
1.566 + Class<?> t = array.getClass().getComponentType();
1.567 + if (t == Long.TYPE) {
1.568 + long[] arr = (long[]) array;
1.569 + arr[index] = l;
1.570 + } else {
1.571 + setFloat(array, index, l);
1.572 + }
1.573 + }
1.574 +
1.575 + /**
1.576 + * Sets the value of the indexed component of the specified array
1.577 + * object to the specified {@code float} value.
1.578 + * @param array the array
1.579 + * @param index the index into the array
1.580 + * @param f the new value of the indexed component
1.581 + * @exception NullPointerException If the specified object argument
1.582 + * is null
1.583 + * @exception IllegalArgumentException If the specified object argument
1.584 + * is not an array, or if the specified value cannot be converted
1.585 + * to the underlying array's component type by an identity or a
1.586 + * primitive widening conversion
1.587 + * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
1.588 + * argument is negative, or if it is greater than or equal to
1.589 + * the length of the specified array
1.590 + * @see Array#set
1.591 + */
1.592 + public static void setFloat(Object array, int index, float f)
1.593 + throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
1.594 + Class<?> t = array.getClass().getComponentType();
1.595 + if (t == Float.TYPE) {
1.596 + float[] arr = (float[])array;
1.597 + arr[index] = f;
1.598 + } else {
1.599 + setDouble(array, index, f);
1.600 + }
1.601 + }
1.602 +
1.603 + /**
1.604 + * Sets the value of the indexed component of the specified array
1.605 + * object to the specified {@code double} value.
1.606 + * @param array the array
1.607 + * @param index the index into the array
1.608 + * @param d the new value of the indexed component
1.609 + * @exception NullPointerException If the specified object argument
1.610 + * is null
1.611 + * @exception IllegalArgumentException If the specified object argument
1.612 + * is not an array, or if the specified value cannot be converted
1.613 + * to the underlying array's component type by an identity or a
1.614 + * primitive widening conversion
1.615 + * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
1.616 + * argument is negative, or if it is greater than or equal to
1.617 + * the length of the specified array
1.618 + * @see Array#set
1.619 + */
1.620 + public static void setDouble(Object array, int index, double d)
1.621 + throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
1.622 + Class<?> t = array.getClass().getComponentType();
1.623 + if (t == Double.TYPE) {
1.624 + double[] arr = (double[])array;
1.625 + arr[index] = d;
1.626 + } else {
1.627 + throw new IllegalArgumentException("argument type mismatch");
1.628 + }
1.629 + }
1.630 +
1.631 + /*
1.632 + * Private
1.633 + */
1.634 +
1.635 + @JavaScriptBody(args = { "primitive", "sig", "length" }, body =
1.636 + "var arr = new Array(length);\n"
1.637 + + "var value = primitive ? 0 : null;\n"
1.638 + + "for(var i = 0; i < length; i++) arr[i] = value;\n"
1.639 + + "arr.jvmName = sig;\n"
1.640 + + "return arr;"
1.641 + )
1.642 + private static native Object newArray(boolean primitive, String sig, int length);
1.643 +
1.644 + private static Object multiNewArray(String sig, int[] dims, int index)
1.645 + throws IllegalArgumentException, NegativeArraySizeException {
1.646 + if (dims.length == index + 1) {
1.647 + return newArray(sig.length() == 2, sig, dims[index]);
1.648 + }
1.649 + Object arr = newArray(false, sig, dims[index]);
1.650 + String compsig = sig.substring(1);
1.651 + int len = getLength(arr);
1.652 + for (int i = 0; i < len; i++) {
1.653 + setArray(arr, i, multiNewArray(compsig, dims, index + 1));
1.654 + }
1.655 + return arr;
1.656 + }
1.657 + private static Object fromPrimitive(Class<?> t, Object array, int index) {
1.658 + return Method.fromPrimitive(t, atArray(array, index));
1.659 + }
1.660 +
1.661 + @JavaScriptBody(args = { "array", "index" }, body = "return array[index];")
1.662 + private static native Object atArray(Object array, int index);
1.663 +
1.664 + @JavaScriptBody(args = { "array", "index", "v" }, body = "array[index] = v;")
1.665 + private static native Object setArray(Object array, int index, Object v);
1.666 +}