rt/emul/mini/src/main/java/java/lang/reflect/Array.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 06 May 2014 08:38:34 +0200
branchclosure
changeset 1533 9e1af8aae380
parent 1532 10d26626c426
child 1534 ca538fb33f48
permissions -rw-r--r--
No need for the newArray0 method
jaroslav@475
     1
/*
jaroslav@475
     2
 * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
jaroslav@475
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@475
     4
 *
jaroslav@475
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@475
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@475
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@475
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@475
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@475
    10
 *
jaroslav@475
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@475
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@475
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@475
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@475
    15
 * accompanied this code).
jaroslav@475
    16
 *
jaroslav@475
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@475
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@475
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@475
    20
 *
jaroslav@475
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@475
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@475
    23
 * questions.
jaroslav@475
    24
 */
jaroslav@475
    25
jaroslav@475
    26
package java.lang.reflect;
jaroslav@475
    27
lubomir@967
    28
import org.apidesign.bck2brwsr.core.Exported;
jaroslav@477
    29
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@479
    30
import org.apidesign.bck2brwsr.core.JavaScriptPrototype;
jaroslav@477
    31
jaroslav@475
    32
/**
jaroslav@475
    33
 * The {@code Array} class provides static methods to dynamically create and
jaroslav@475
    34
 * access Java arrays.
jaroslav@475
    35
 *
jaroslav@475
    36
 * <p>{@code Array} permits widening conversions to occur during a get or set
jaroslav@475
    37
 * operation, but throws an {@code IllegalArgumentException} if a narrowing
jaroslav@475
    38
 * conversion would occur.
jaroslav@475
    39
 *
jaroslav@475
    40
 * @author Nakul Saraiya
jaroslav@475
    41
 */
jaroslav@479
    42
@JavaScriptPrototype(prototype = "new Array", container = "Array.prototype")
jaroslav@475
    43
public final
jaroslav@475
    44
class Array {
jaroslav@475
    45
jaroslav@475
    46
    /**
jaroslav@475
    47
     * Constructor.  Class Array is not instantiable.
jaroslav@475
    48
     */
jaroslav@475
    49
    private Array() {}
jaroslav@475
    50
jaroslav@475
    51
    /**
jaroslav@475
    52
     * Creates a new array with the specified component type and
jaroslav@475
    53
     * length.
jaroslav@475
    54
     * Invoking this method is equivalent to creating an array
jaroslav@475
    55
     * as follows:
jaroslav@475
    56
     * <blockquote>
jaroslav@475
    57
     * <pre>
jaroslav@475
    58
     * int[] x = {length};
jaroslav@475
    59
     * Array.newInstance(componentType, x);
jaroslav@475
    60
     * </pre>
jaroslav@475
    61
     * </blockquote>
jaroslav@475
    62
     *
jaroslav@475
    63
     * @param componentType the {@code Class} object representing the
jaroslav@475
    64
     * component type of the new array
jaroslav@475
    65
     * @param length the length of the new array
jaroslav@475
    66
     * @return the new array
jaroslav@475
    67
     * @exception NullPointerException if the specified
jaroslav@475
    68
     * {@code componentType} parameter is null
jaroslav@475
    69
     * @exception IllegalArgumentException if componentType is {@link Void#TYPE}
jaroslav@475
    70
     * @exception NegativeArraySizeException if the specified {@code length}
jaroslav@475
    71
     * is negative
jaroslav@475
    72
     */
jaroslav@475
    73
    public static Object newInstance(Class<?> componentType, int length)
jaroslav@477
    74
    throws NegativeArraySizeException {
jaroslav@477
    75
        if (length < 0) {
jaroslav@477
    76
            throw new NegativeArraySizeException();
jaroslav@477
    77
        }
jaroslav@477
    78
        String sig = findSignature(componentType);
jaroslav@1532
    79
        return newArray(componentType.isPrimitive(), sig, null, length);
jaroslav@475
    80
    }
jaroslav@477
    81
    
jaroslav@477
    82
    private static String findSignature(Class<?> type) {
jaroslav@477
    83
        if (type == Integer.TYPE) {
jaroslav@477
    84
            return "[I";
jaroslav@477
    85
        }
jaroslav@477
    86
        if (type == Long.TYPE) {
jaroslav@477
    87
            return "[J";
jaroslav@477
    88
        }
jaroslav@477
    89
        if (type == Double.TYPE) {
jaroslav@477
    90
            return "[D";
jaroslav@477
    91
        }
jaroslav@477
    92
        if (type == Float.TYPE) {
jaroslav@477
    93
            return "[F";
jaroslav@477
    94
        }
jaroslav@477
    95
        if (type == Byte.TYPE) {
jaroslav@477
    96
            return "[B";
jaroslav@477
    97
        }
jaroslav@477
    98
        if (type == Boolean.TYPE) {
jaroslav@477
    99
            return "[Z";
jaroslav@477
   100
        }
jaroslav@477
   101
        if (type == Short.TYPE) {
jaroslav@477
   102
            return "[S";
jaroslav@477
   103
        }
jaroslav@477
   104
        if (type == Character.TYPE) {
jaroslav@477
   105
            return "[C";
jaroslav@477
   106
        }
jaroslav@477
   107
        if (type.getName().equals("void")) {
jaroslav@477
   108
            throw new IllegalStateException("Can't create array for " + type);
jaroslav@477
   109
        }
jaroslav@1376
   110
        return "[L" + type.getName().replace('.', '/') + ";";
jaroslav@477
   111
    }
jaroslav@475
   112
    /**
jaroslav@475
   113
     * Creates a new array
jaroslav@475
   114
     * with the specified component type and dimensions.
jaroslav@475
   115
     * If {@code componentType}
jaroslav@475
   116
     * represents a non-array class or interface, the new array
jaroslav@475
   117
     * has {@code dimensions.length} dimensions and
jaroslav@475
   118
     * {@code componentType} as its component type. If
jaroslav@475
   119
     * {@code componentType} represents an array class, the
jaroslav@475
   120
     * number of dimensions of the new array is equal to the sum
jaroslav@475
   121
     * of {@code dimensions.length} and the number of
jaroslav@475
   122
     * dimensions of {@code componentType}. In this case, the
jaroslav@475
   123
     * component type of the new array is the component type of
jaroslav@475
   124
     * {@code componentType}.
jaroslav@475
   125
     *
jaroslav@475
   126
     * <p>The number of dimensions of the new array must not
jaroslav@475
   127
     * exceed the number of array dimensions supported by the
jaroslav@475
   128
     * implementation (typically 255).
jaroslav@475
   129
     *
jaroslav@475
   130
     * @param componentType the {@code Class} object representing the component
jaroslav@475
   131
     * type of the new array
jaroslav@475
   132
     * @param dimensions an array of {@code int} representing the dimensions of
jaroslav@475
   133
     * the new array
jaroslav@475
   134
     * @return the new array
jaroslav@475
   135
     * @exception NullPointerException if the specified
jaroslav@475
   136
     * {@code componentType} argument is null
jaroslav@475
   137
     * @exception IllegalArgumentException if the specified {@code dimensions}
jaroslav@475
   138
     * argument is a zero-dimensional array, or if the number of
jaroslav@475
   139
     * requested dimensions exceeds the limit on the number of array dimensions
jaroslav@475
   140
     * supported by the implementation (typically 255), or if componentType
jaroslav@475
   141
     * is {@link Void#TYPE}.
jaroslav@475
   142
     * @exception NegativeArraySizeException if any of the components in
jaroslav@475
   143
     * the specified {@code dimensions} argument is negative.
jaroslav@475
   144
     */
jaroslav@475
   145
    public static Object newInstance(Class<?> componentType, int... dimensions)
jaroslav@475
   146
        throws IllegalArgumentException, NegativeArraySizeException {
jaroslav@480
   147
        StringBuilder sig = new StringBuilder();
jaroslav@480
   148
        for (int i = 1; i < dimensions.length; i++) {
jaroslav@480
   149
            sig.append('[');
jaroslav@480
   150
        }
jaroslav@480
   151
        sig.append(findSignature(componentType));
jaroslav@480
   152
        return multiNewArray(sig.toString(), dimensions, 0);
jaroslav@475
   153
    }
jaroslav@475
   154
jaroslav@475
   155
    /**
jaroslav@475
   156
     * Returns the length of the specified array object, as an {@code int}.
jaroslav@475
   157
     *
jaroslav@475
   158
     * @param array the array
jaroslav@475
   159
     * @return the length of the array
jaroslav@475
   160
     * @exception IllegalArgumentException if the object argument is not
jaroslav@475
   161
     * an array
jaroslav@475
   162
     */
jaroslav@482
   163
    public static int getLength(Object array)
jaroslav@482
   164
    throws IllegalArgumentException {
jaroslav@482
   165
        if (!array.getClass().isArray()) {
jaroslav@483
   166
            throw new IllegalArgumentException("Argument is not an array");
jaroslav@482
   167
        }
jaroslav@482
   168
        return length(array);
jaroslav@482
   169
    }
jaroslav@482
   170
    
jaroslav@482
   171
    @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
jaroslav@482
   172
    private static native int length(Object arr);
jaroslav@475
   173
jaroslav@475
   174
    /**
jaroslav@475
   175
     * Returns the value of the indexed component in the specified
jaroslav@475
   176
     * array object.  The value is automatically wrapped in an object
jaroslav@475
   177
     * if it has a primitive type.
jaroslav@475
   178
     *
jaroslav@475
   179
     * @param array the array
jaroslav@475
   180
     * @param index the index
jaroslav@475
   181
     * @return the (possibly wrapped) value of the indexed component in
jaroslav@475
   182
     * the specified array
jaroslav@475
   183
     * @exception NullPointerException If the specified object is null
jaroslav@475
   184
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   185
     * an array
jaroslav@475
   186
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   187
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   188
     * length of the specified array
jaroslav@475
   189
     */
jaroslav@485
   190
    public static Object get(Object array, int index)
jaroslav@485
   191
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@486
   192
        final Class<?> t = array.getClass().getComponentType();
jaroslav@486
   193
        if (t.isPrimitive()) {
jaroslav@571
   194
            return fromPrimitive(t, array, index);
jaroslav@485
   195
        } else {
jaroslav@485
   196
            return ((Object[])array)[index];
jaroslav@485
   197
        }
jaroslav@485
   198
    }
jaroslav@475
   199
jaroslav@475
   200
    /**
jaroslav@475
   201
     * Returns the value of the indexed component in the specified
jaroslav@475
   202
     * array object, as a {@code boolean}.
jaroslav@475
   203
     *
jaroslav@475
   204
     * @param array the array
jaroslav@475
   205
     * @param index the index
jaroslav@475
   206
     * @return the value of the indexed component in the specified array
jaroslav@475
   207
     * @exception NullPointerException If the specified object is null
jaroslav@475
   208
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   209
     * an array, or if the indexed element cannot be converted to the
jaroslav@475
   210
     * return type by an identity or widening conversion
jaroslav@475
   211
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   212
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   213
     * length of the specified array
jaroslav@475
   214
     * @see Array#get
jaroslav@475
   215
     */
jaroslav@475
   216
    public static native boolean getBoolean(Object array, int index)
jaroslav@475
   217
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
jaroslav@475
   218
jaroslav@475
   219
    /**
jaroslav@475
   220
     * Returns the value of the indexed component in the specified
jaroslav@475
   221
     * array object, as a {@code byte}.
jaroslav@475
   222
     *
jaroslav@475
   223
     * @param array the array
jaroslav@475
   224
     * @param index the index
jaroslav@475
   225
     * @return the value of the indexed component in the specified array
jaroslav@475
   226
     * @exception NullPointerException If the specified object is null
jaroslav@475
   227
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   228
     * an array, or if the indexed element cannot be converted to the
jaroslav@475
   229
     * return type by an identity or widening conversion
jaroslav@475
   230
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   231
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   232
     * length of the specified array
jaroslav@475
   233
     * @see Array#get
jaroslav@475
   234
     */
jaroslav@483
   235
    public static byte getByte(Object array, int index)
jaroslav@483
   236
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@483
   237
        if (array.getClass().getComponentType() != Byte.TYPE) {
jaroslav@483
   238
            throw new IllegalArgumentException();
jaroslav@483
   239
        }
jaroslav@483
   240
        byte[] arr = (byte[]) array;
jaroslav@483
   241
        return arr[index];
jaroslav@483
   242
    }
jaroslav@475
   243
jaroslav@475
   244
    /**
jaroslav@475
   245
     * Returns the value of the indexed component in the specified
jaroslav@475
   246
     * array object, as a {@code char}.
jaroslav@475
   247
     *
jaroslav@475
   248
     * @param array the array
jaroslav@475
   249
     * @param index the index
jaroslav@475
   250
     * @return the value of the indexed component in the specified array
jaroslav@475
   251
     * @exception NullPointerException If the specified object is null
jaroslav@475
   252
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   253
     * an array, or if the indexed element cannot be converted to the
jaroslav@475
   254
     * return type by an identity or widening conversion
jaroslav@475
   255
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   256
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   257
     * length of the specified array
jaroslav@475
   258
     * @see Array#get
jaroslav@475
   259
     */
jaroslav@475
   260
    public static native char getChar(Object array, int index)
jaroslav@475
   261
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
jaroslav@475
   262
jaroslav@475
   263
    /**
jaroslav@475
   264
     * Returns the value of the indexed component in the specified
jaroslav@475
   265
     * array object, as a {@code short}.
jaroslav@475
   266
     *
jaroslav@475
   267
     * @param array the array
jaroslav@475
   268
     * @param index the index
jaroslav@475
   269
     * @return the value of the indexed component in the specified array
jaroslav@475
   270
     * @exception NullPointerException If the specified object is null
jaroslav@475
   271
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   272
     * an array, or if the indexed element cannot be converted to the
jaroslav@475
   273
     * return type by an identity or widening conversion
jaroslav@475
   274
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   275
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   276
     * length of the specified array
jaroslav@475
   277
     * @see Array#get
jaroslav@475
   278
     */
jaroslav@483
   279
    public static short getShort(Object array, int index)
jaroslav@483
   280
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@483
   281
        final Class<?> t = array.getClass().getComponentType();
jaroslav@483
   282
        if (t == Short.TYPE) {
jaroslav@483
   283
            short[] arr = (short[]) array;
jaroslav@483
   284
            return arr[index];
jaroslav@483
   285
        }
jaroslav@483
   286
        return getByte(array, index);
jaroslav@483
   287
    }
jaroslav@475
   288
jaroslav@475
   289
    /**
jaroslav@475
   290
     * Returns the value of the indexed component in the specified
jaroslav@475
   291
     * array object, as an {@code int}.
jaroslav@475
   292
     *
jaroslav@475
   293
     * @param array the array
jaroslav@475
   294
     * @param index the index
jaroslav@475
   295
     * @return the value of the indexed component in the specified array
jaroslav@475
   296
     * @exception NullPointerException If the specified object is null
jaroslav@475
   297
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   298
     * an array, or if the indexed element cannot be converted to the
jaroslav@475
   299
     * return type by an identity or widening conversion
jaroslav@475
   300
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   301
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   302
     * length of the specified array
jaroslav@475
   303
     * @see Array#get
jaroslav@475
   304
     */
jaroslav@483
   305
    public static int getInt(Object array, int index) 
jaroslav@483
   306
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@483
   307
        final Class<?> t = array.getClass().getComponentType();
jaroslav@483
   308
        if (t == Integer.TYPE) {
jaroslav@483
   309
            int[] arr = (int[]) array;
jaroslav@483
   310
            return arr[index];
jaroslav@483
   311
        }
jaroslav@483
   312
        return getShort(array, index);
jaroslav@483
   313
    }
jaroslav@475
   314
jaroslav@475
   315
    /**
jaroslav@475
   316
     * Returns the value of the indexed component in the specified
jaroslav@475
   317
     * array object, as a {@code long}.
jaroslav@475
   318
     *
jaroslav@475
   319
     * @param array the array
jaroslav@475
   320
     * @param index the index
jaroslav@475
   321
     * @return the value of the indexed component in the specified array
jaroslav@475
   322
     * @exception NullPointerException If the specified object is null
jaroslav@475
   323
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   324
     * an array, or if the indexed element cannot be converted to the
jaroslav@475
   325
     * return type by an identity or widening conversion
jaroslav@475
   326
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   327
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   328
     * length of the specified array
jaroslav@475
   329
     * @see Array#get
jaroslav@475
   330
     */
jaroslav@483
   331
    public static long getLong(Object array, int index)
jaroslav@483
   332
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@483
   333
        final Class<?> t = array.getClass().getComponentType();
jaroslav@483
   334
        if (t == Long.TYPE) {
jaroslav@483
   335
            long[] arr = (long[]) array;
jaroslav@483
   336
            return arr[index];
jaroslav@483
   337
        }
jaroslav@483
   338
        return getInt(array, index);
jaroslav@483
   339
    }
jaroslav@475
   340
jaroslav@475
   341
    /**
jaroslav@475
   342
     * Returns the value of the indexed component in the specified
jaroslav@475
   343
     * array object, as a {@code float}.
jaroslav@475
   344
     *
jaroslav@475
   345
     * @param array the array
jaroslav@475
   346
     * @param index the index
jaroslav@475
   347
     * @return the value of the indexed component in the specified array
jaroslav@475
   348
     * @exception NullPointerException If the specified object is null
jaroslav@475
   349
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   350
     * an array, or if the indexed element cannot be converted to the
jaroslav@475
   351
     * return type by an identity or widening conversion
jaroslav@475
   352
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   353
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   354
     * length of the specified array
jaroslav@475
   355
     * @see Array#get
jaroslav@475
   356
     */
jaroslav@483
   357
    public static float getFloat(Object array, int index)
jaroslav@483
   358
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@483
   359
        final Class<?> t = array.getClass().getComponentType();
jaroslav@483
   360
        if (t == Float.TYPE) {
jaroslav@483
   361
            float[] arr = (float[]) array;
jaroslav@483
   362
            return arr[index];
jaroslav@483
   363
        }
jaroslav@483
   364
        return getLong(array, index);
jaroslav@483
   365
    }
jaroslav@475
   366
jaroslav@475
   367
    /**
jaroslav@475
   368
     * Returns the value of the indexed component in the specified
jaroslav@475
   369
     * array object, as a {@code double}.
jaroslav@475
   370
     *
jaroslav@475
   371
     * @param array the array
jaroslav@475
   372
     * @param index the index
jaroslav@475
   373
     * @return the value of the indexed component in the specified array
jaroslav@475
   374
     * @exception NullPointerException If the specified object is null
jaroslav@475
   375
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   376
     * an array, or if the indexed element cannot be converted to the
jaroslav@475
   377
     * return type by an identity or widening conversion
jaroslav@475
   378
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   379
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   380
     * length of the specified array
jaroslav@475
   381
     * @see Array#get
jaroslav@475
   382
     */
jaroslav@483
   383
    public static double getDouble(Object array, int index)
jaroslav@483
   384
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@483
   385
        final Class<?> t = array.getClass().getComponentType();
jaroslav@483
   386
        if (t == Double.TYPE) {
jaroslav@483
   387
            double[] arr = (double[]) array;
jaroslav@483
   388
            return arr[index];
jaroslav@483
   389
        }
jaroslav@483
   390
        return getFloat(array, index);
jaroslav@483
   391
    }
jaroslav@475
   392
jaroslav@475
   393
    /**
jaroslav@475
   394
     * Sets the value of the indexed component of the specified array
jaroslav@475
   395
     * object to the specified new value.  The new value is first
jaroslav@475
   396
     * automatically unwrapped if the array has a primitive component
jaroslav@475
   397
     * type.
jaroslav@475
   398
     * @param array the array
jaroslav@475
   399
     * @param index the index into the array
jaroslav@475
   400
     * @param value the new value of the indexed component
jaroslav@475
   401
     * @exception NullPointerException If the specified object argument
jaroslav@475
   402
     * is null
jaroslav@475
   403
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   404
     * is not an array, or if the array component type is primitive and
jaroslav@475
   405
     * an unwrapping conversion fails
jaroslav@475
   406
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   407
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   408
     * the length of the specified array
jaroslav@475
   409
     */
jaroslav@485
   410
    public static void set(Object array, int index, Object value)
jaroslav@485
   411
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@485
   412
        if (array.getClass().getComponentType().isPrimitive()) {
jaroslav@485
   413
            throw new IllegalArgumentException();
jaroslav@485
   414
        } else {
jaroslav@485
   415
            Object[] arr = (Object[])array;
jaroslav@485
   416
            arr[index] = value;
jaroslav@485
   417
        }
jaroslav@485
   418
    }
jaroslav@475
   419
jaroslav@475
   420
    /**
jaroslav@475
   421
     * Sets the value of the indexed component of the specified array
jaroslav@475
   422
     * object to the specified {@code boolean} value.
jaroslav@475
   423
     * @param array the array
jaroslav@475
   424
     * @param index the index into the array
jaroslav@475
   425
     * @param z the new value of the indexed component
jaroslav@475
   426
     * @exception NullPointerException If the specified object argument
jaroslav@475
   427
     * is null
jaroslav@475
   428
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   429
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   430
     * to the underlying array's component type by an identity or a
jaroslav@475
   431
     * primitive widening conversion
jaroslav@475
   432
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   433
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   434
     * the length of the specified array
jaroslav@475
   435
     * @see Array#set
jaroslav@475
   436
     */
jaroslav@475
   437
    public static native void setBoolean(Object array, int index, boolean z)
jaroslav@475
   438
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
jaroslav@475
   439
jaroslav@475
   440
    /**
jaroslav@475
   441
     * Sets the value of the indexed component of the specified array
jaroslav@475
   442
     * object to the specified {@code byte} value.
jaroslav@475
   443
     * @param array the array
jaroslav@475
   444
     * @param index the index into the array
jaroslav@475
   445
     * @param b the new value of the indexed component
jaroslav@475
   446
     * @exception NullPointerException If the specified object argument
jaroslav@475
   447
     * is null
jaroslav@475
   448
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   449
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   450
     * to the underlying array's component type by an identity or a
jaroslav@475
   451
     * primitive widening conversion
jaroslav@475
   452
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   453
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   454
     * the length of the specified array
jaroslav@475
   455
     * @see Array#set
jaroslav@475
   456
     */
jaroslav@484
   457
    public static void setByte(Object array, int index, byte b)
jaroslav@484
   458
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@484
   459
        Class<?> t = array.getClass().getComponentType();
jaroslav@484
   460
        if (t == Byte.TYPE) {
jaroslav@484
   461
            byte[] arr = (byte[]) array;
jaroslav@484
   462
            arr[index] = b;
jaroslav@484
   463
        } else {
jaroslav@484
   464
            setShort(array, index, b);
jaroslav@484
   465
        }
jaroslav@484
   466
    }
jaroslav@475
   467
jaroslav@475
   468
    /**
jaroslav@475
   469
     * Sets the value of the indexed component of the specified array
jaroslav@475
   470
     * object to the specified {@code char} value.
jaroslav@475
   471
     * @param array the array
jaroslav@475
   472
     * @param index the index into the array
jaroslav@475
   473
     * @param c the new value of the indexed component
jaroslav@475
   474
     * @exception NullPointerException If the specified object argument
jaroslav@475
   475
     * is null
jaroslav@475
   476
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   477
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   478
     * to the underlying array's component type by an identity or a
jaroslav@475
   479
     * primitive widening conversion
jaroslav@475
   480
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   481
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   482
     * the length of the specified array
jaroslav@475
   483
     * @see Array#set
jaroslav@475
   484
     */
jaroslav@475
   485
    public static native void setChar(Object array, int index, char c)
jaroslav@475
   486
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
jaroslav@475
   487
jaroslav@475
   488
    /**
jaroslav@475
   489
     * Sets the value of the indexed component of the specified array
jaroslav@475
   490
     * object to the specified {@code short} value.
jaroslav@475
   491
     * @param array the array
jaroslav@475
   492
     * @param index the index into the array
jaroslav@475
   493
     * @param s the new value of the indexed component
jaroslav@475
   494
     * @exception NullPointerException If the specified object argument
jaroslav@475
   495
     * is null
jaroslav@475
   496
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   497
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   498
     * to the underlying array's component type by an identity or a
jaroslav@475
   499
     * primitive widening conversion
jaroslav@475
   500
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   501
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   502
     * the length of the specified array
jaroslav@475
   503
     * @see Array#set
jaroslav@475
   504
     */
jaroslav@484
   505
    public static void setShort(Object array, int index, short s)
jaroslav@484
   506
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@484
   507
        Class<?> t = array.getClass().getComponentType();
jaroslav@484
   508
        if (t == Short.TYPE) {
jaroslav@484
   509
            short[] arr = (short[]) array;
jaroslav@484
   510
            arr[index] = s;
jaroslav@484
   511
        } else {
jaroslav@484
   512
            setInt(array, index, s);
jaroslav@484
   513
        }
jaroslav@484
   514
        
jaroslav@484
   515
    }
jaroslav@571
   516
    
jaroslav@475
   517
    /**
jaroslav@475
   518
     * Sets the value of the indexed component of the specified array
jaroslav@475
   519
     * object to the specified {@code int} value.
jaroslav@475
   520
     * @param array the array
jaroslav@475
   521
     * @param index the index into the array
jaroslav@475
   522
     * @param i the new value of the indexed component
jaroslav@475
   523
     * @exception NullPointerException If the specified object argument
jaroslav@475
   524
     * is null
jaroslav@475
   525
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   526
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   527
     * to the underlying array's component type by an identity or a
jaroslav@475
   528
     * primitive widening conversion
jaroslav@475
   529
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   530
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   531
     * the length of the specified array
jaroslav@475
   532
     * @see Array#set
jaroslav@475
   533
     */
jaroslav@484
   534
    public static void setInt(Object array, int index, int i)
jaroslav@484
   535
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@484
   536
        Class<?> t = array.getClass().getComponentType();
jaroslav@484
   537
        if (t == Integer.TYPE) {
jaroslav@571
   538
            int[] arr = (int[]) array;
jaroslav@484
   539
            arr[index] = i;
jaroslav@484
   540
        } else {
jaroslav@484
   541
            setLong(array, index, i);
jaroslav@484
   542
        }
jaroslav@484
   543
    }
jaroslav@475
   544
jaroslav@475
   545
    /**
jaroslav@475
   546
     * Sets the value of the indexed component of the specified array
jaroslav@475
   547
     * object to the specified {@code long} value.
jaroslav@475
   548
     * @param array the array
jaroslav@475
   549
     * @param index the index into the array
jaroslav@475
   550
     * @param l the new value of the indexed component
jaroslav@475
   551
     * @exception NullPointerException If the specified object argument
jaroslav@475
   552
     * is null
jaroslav@475
   553
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   554
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   555
     * to the underlying array's component type by an identity or a
jaroslav@475
   556
     * primitive widening conversion
jaroslav@475
   557
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   558
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   559
     * the length of the specified array
jaroslav@475
   560
     * @see Array#set
jaroslav@475
   561
     */
jaroslav@484
   562
    public static void setLong(Object array, int index, long l)
jaroslav@484
   563
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@484
   564
        Class<?> t = array.getClass().getComponentType();
jaroslav@484
   565
        if (t == Long.TYPE) {
jaroslav@484
   566
            long[] arr = (long[]) array;
jaroslav@484
   567
            arr[index] = l;
jaroslav@484
   568
        } else {
jaroslav@484
   569
            setFloat(array, index, l);
jaroslav@484
   570
        }
jaroslav@484
   571
    }
jaroslav@475
   572
jaroslav@475
   573
    /**
jaroslav@475
   574
     * Sets the value of the indexed component of the specified array
jaroslav@475
   575
     * object to the specified {@code float} value.
jaroslav@475
   576
     * @param array the array
jaroslav@475
   577
     * @param index the index into the array
jaroslav@475
   578
     * @param f the new value of the indexed component
jaroslav@475
   579
     * @exception NullPointerException If the specified object argument
jaroslav@475
   580
     * is null
jaroslav@475
   581
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   582
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   583
     * to the underlying array's component type by an identity or a
jaroslav@475
   584
     * primitive widening conversion
jaroslav@475
   585
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   586
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   587
     * the length of the specified array
jaroslav@475
   588
     * @see Array#set
jaroslav@475
   589
     */
jaroslav@484
   590
    public static void setFloat(Object array, int index, float f)
jaroslav@484
   591
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@484
   592
        Class<?> t = array.getClass().getComponentType();
jaroslav@484
   593
        if (t == Float.TYPE) {
jaroslav@484
   594
            float[] arr = (float[])array;
jaroslav@484
   595
            arr[index] = f;
jaroslav@484
   596
        } else {
jaroslav@484
   597
            setDouble(array, index, f);
jaroslav@484
   598
        }
jaroslav@484
   599
    }
jaroslav@475
   600
jaroslav@475
   601
    /**
jaroslav@475
   602
     * Sets the value of the indexed component of the specified array
jaroslav@475
   603
     * object to the specified {@code double} value.
jaroslav@475
   604
     * @param array the array
jaroslav@475
   605
     * @param index the index into the array
jaroslav@475
   606
     * @param d the new value of the indexed component
jaroslav@475
   607
     * @exception NullPointerException If the specified object argument
jaroslav@475
   608
     * is null
jaroslav@475
   609
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   610
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   611
     * to the underlying array's component type by an identity or a
jaroslav@475
   612
     * primitive widening conversion
jaroslav@475
   613
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   614
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   615
     * the length of the specified array
jaroslav@475
   616
     * @see Array#set
jaroslav@475
   617
     */
jaroslav@484
   618
    public static void setDouble(Object array, int index, double d)
jaroslav@484
   619
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@484
   620
        Class<?> t = array.getClass().getComponentType();
jaroslav@484
   621
        if (t == Double.TYPE) {
jaroslav@484
   622
            double[] arr = (double[])array;
jaroslav@484
   623
            arr[index] = d;
jaroslav@484
   624
        } else {
jaroslav@484
   625
            throw new IllegalArgumentException("argument type mismatch");
jaroslav@484
   626
        }
jaroslav@484
   627
    }
jaroslav@475
   628
jaroslav@475
   629
    /*
jaroslav@475
   630
     * Private
jaroslav@475
   631
     */
jaroslav@475
   632
jaroslav@1532
   633
    @JavaScriptBody(args = { "primitive", "sig", "fn", "length" }, body =
jaroslav@477
   634
          "var arr = new Array(length);\n"
jaroslav@477
   635
        + "var value = primitive ? 0 : null;\n"
jaroslav@477
   636
        + "for(var i = 0; i < length; i++) arr[i] = value;\n"
jaroslav@477
   637
        + "arr.jvmName = sig;\n"
jaroslav@1532
   638
        + "arr.fnc = fn;\n"
jaroslav@1532
   639
//        + "java.lang.System.out.println('Assigned ' + arr.jvmName + ' fn: ' + (!!arr.fnc));\n"
jaroslav@477
   640
        + "return arr;"
jaroslav@477
   641
    )
lubomir@967
   642
    @Exported
jaroslav@1533
   643
    private static native Object newArray(boolean primitive, String sig, Object fn, int length);
jaroslav@475
   644
lubomir@967
   645
lubomir@967
   646
    @Exported
lubomir@967
   647
    private static Object multiNewArray(String sig, int[] dims, int index)
jaroslav@480
   648
    throws IllegalArgumentException, NegativeArraySizeException {
jaroslav@480
   649
        if (dims.length == index + 1) {
jaroslav@1532
   650
            return newArray(sig.length() == 2, sig, null, dims[index]);
jaroslav@480
   651
        }
jaroslav@1532
   652
        Object arr = newArray(false, sig, null, dims[index]);
jaroslav@480
   653
        String compsig = sig.substring(1);
jaroslav@571
   654
        int len = getLength(arr);
jaroslav@571
   655
        for (int i = 0; i < len; i++) {
jaroslav@571
   656
            setArray(arr, i, multiNewArray(compsig, dims, index + 1));
jaroslav@480
   657
        }
jaroslav@480
   658
        return arr;
jaroslav@480
   659
    }
jaroslav@486
   660
    private static Object fromPrimitive(Class<?> t, Object array, int index) {
jaroslav@486
   661
        return Method.fromPrimitive(t, atArray(array, index));
jaroslav@486
   662
    }
jaroslav@486
   663
    
jaroslav@571
   664
    @JavaScriptBody(args = { "array", "index" }, body = "return array[index];")
jaroslav@486
   665
    private static native Object atArray(Object array, int index);
jaroslav@571
   666
jaroslav@571
   667
    @JavaScriptBody(args = { "array", "index", "v" }, body = "array[index] = v;")
jaroslav@571
   668
    private static native Object setArray(Object array, int index, Object v);
jaroslav@475
   669
}