emul/src/main/java/java/lang/reflect/Array.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 18 Jan 2013 21:04:10 +0100
branchArrayReflect
changeset 483 7e39f344e4a0
parent 482 05a87bc23192
child 484 7ca6bd52b668
permissions -rw-r--r--
Getters for basic primitive types
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
jaroslav@477
    28
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@479
    29
import org.apidesign.bck2brwsr.core.JavaScriptPrototype;
jaroslav@477
    30
jaroslav@475
    31
/**
jaroslav@475
    32
 * The {@code Array} class provides static methods to dynamically create and
jaroslav@475
    33
 * access Java arrays.
jaroslav@475
    34
 *
jaroslav@475
    35
 * <p>{@code Array} permits widening conversions to occur during a get or set
jaroslav@475
    36
 * operation, but throws an {@code IllegalArgumentException} if a narrowing
jaroslav@475
    37
 * conversion would occur.
jaroslav@475
    38
 *
jaroslav@475
    39
 * @author Nakul Saraiya
jaroslav@475
    40
 */
jaroslav@479
    41
@JavaScriptPrototype(prototype = "new Array", container = "Array.prototype")
jaroslav@475
    42
public final
jaroslav@475
    43
class Array {
jaroslav@475
    44
jaroslav@475
    45
    /**
jaroslav@475
    46
     * Constructor.  Class Array is not instantiable.
jaroslav@475
    47
     */
jaroslav@475
    48
    private Array() {}
jaroslav@475
    49
jaroslav@475
    50
    /**
jaroslav@475
    51
     * Creates a new array with the specified component type and
jaroslav@475
    52
     * length.
jaroslav@475
    53
     * Invoking this method is equivalent to creating an array
jaroslav@475
    54
     * as follows:
jaroslav@475
    55
     * <blockquote>
jaroslav@475
    56
     * <pre>
jaroslav@475
    57
     * int[] x = {length};
jaroslav@475
    58
     * Array.newInstance(componentType, x);
jaroslav@475
    59
     * </pre>
jaroslav@475
    60
     * </blockquote>
jaroslav@475
    61
     *
jaroslav@475
    62
     * @param componentType the {@code Class} object representing the
jaroslav@475
    63
     * component type of the new array
jaroslav@475
    64
     * @param length the length of the new array
jaroslav@475
    65
     * @return the new array
jaroslav@475
    66
     * @exception NullPointerException if the specified
jaroslav@475
    67
     * {@code componentType} parameter is null
jaroslav@475
    68
     * @exception IllegalArgumentException if componentType is {@link Void#TYPE}
jaroslav@475
    69
     * @exception NegativeArraySizeException if the specified {@code length}
jaroslav@475
    70
     * is negative
jaroslav@475
    71
     */
jaroslav@475
    72
    public static Object newInstance(Class<?> componentType, int length)
jaroslav@477
    73
    throws NegativeArraySizeException {
jaroslav@477
    74
        if (length < 0) {
jaroslav@477
    75
            throw new NegativeArraySizeException();
jaroslav@477
    76
        }
jaroslav@477
    77
        String sig = findSignature(componentType);
jaroslav@477
    78
        return newArray(componentType.isPrimitive(), sig, length);
jaroslav@475
    79
    }
jaroslav@477
    80
    
jaroslav@477
    81
    private static String findSignature(Class<?> type) {
jaroslav@477
    82
        if (type == Integer.TYPE) {
jaroslav@477
    83
            return "[I";
jaroslav@477
    84
        }
jaroslav@477
    85
        if (type == Long.TYPE) {
jaroslav@477
    86
            return "[J";
jaroslav@477
    87
        }
jaroslav@477
    88
        if (type == Double.TYPE) {
jaroslav@477
    89
            return "[D";
jaroslav@477
    90
        }
jaroslav@477
    91
        if (type == Float.TYPE) {
jaroslav@477
    92
            return "[F";
jaroslav@477
    93
        }
jaroslav@477
    94
        if (type == Byte.TYPE) {
jaroslav@477
    95
            return "[B";
jaroslav@477
    96
        }
jaroslav@477
    97
        if (type == Boolean.TYPE) {
jaroslav@477
    98
            return "[Z";
jaroslav@477
    99
        }
jaroslav@477
   100
        if (type == Short.TYPE) {
jaroslav@477
   101
            return "[S";
jaroslav@477
   102
        }
jaroslav@477
   103
        if (type == Character.TYPE) {
jaroslav@477
   104
            return "[C";
jaroslav@477
   105
        }
jaroslav@477
   106
        if (type.getName().equals("void")) {
jaroslav@477
   107
            throw new IllegalStateException("Can't create array for " + type);
jaroslav@477
   108
        }
jaroslav@477
   109
        return "[L" + type.getName() + ";";
jaroslav@477
   110
    }
jaroslav@475
   111
    /**
jaroslav@475
   112
     * Creates a new array
jaroslav@475
   113
     * with the specified component type and dimensions.
jaroslav@475
   114
     * If {@code componentType}
jaroslav@475
   115
     * represents a non-array class or interface, the new array
jaroslav@475
   116
     * has {@code dimensions.length} dimensions and
jaroslav@475
   117
     * {@code componentType} as its component type. If
jaroslav@475
   118
     * {@code componentType} represents an array class, the
jaroslav@475
   119
     * number of dimensions of the new array is equal to the sum
jaroslav@475
   120
     * of {@code dimensions.length} and the number of
jaroslav@475
   121
     * dimensions of {@code componentType}. In this case, the
jaroslav@475
   122
     * component type of the new array is the component type of
jaroslav@475
   123
     * {@code componentType}.
jaroslav@475
   124
     *
jaroslav@475
   125
     * <p>The number of dimensions of the new array must not
jaroslav@475
   126
     * exceed the number of array dimensions supported by the
jaroslav@475
   127
     * implementation (typically 255).
jaroslav@475
   128
     *
jaroslav@475
   129
     * @param componentType the {@code Class} object representing the component
jaroslav@475
   130
     * type of the new array
jaroslav@475
   131
     * @param dimensions an array of {@code int} representing the dimensions of
jaroslav@475
   132
     * the new array
jaroslav@475
   133
     * @return the new array
jaroslav@475
   134
     * @exception NullPointerException if the specified
jaroslav@475
   135
     * {@code componentType} argument is null
jaroslav@475
   136
     * @exception IllegalArgumentException if the specified {@code dimensions}
jaroslav@475
   137
     * argument is a zero-dimensional array, or if the number of
jaroslav@475
   138
     * requested dimensions exceeds the limit on the number of array dimensions
jaroslav@475
   139
     * supported by the implementation (typically 255), or if componentType
jaroslav@475
   140
     * is {@link Void#TYPE}.
jaroslav@475
   141
     * @exception NegativeArraySizeException if any of the components in
jaroslav@475
   142
     * the specified {@code dimensions} argument is negative.
jaroslav@475
   143
     */
jaroslav@475
   144
    public static Object newInstance(Class<?> componentType, int... dimensions)
jaroslav@475
   145
        throws IllegalArgumentException, NegativeArraySizeException {
jaroslav@480
   146
        StringBuilder sig = new StringBuilder();
jaroslav@480
   147
        for (int i = 1; i < dimensions.length; i++) {
jaroslav@480
   148
            sig.append('[');
jaroslav@480
   149
        }
jaroslav@480
   150
        sig.append(findSignature(componentType));
jaroslav@480
   151
        return multiNewArray(sig.toString(), dimensions, 0);
jaroslav@475
   152
    }
jaroslav@475
   153
jaroslav@475
   154
    /**
jaroslav@475
   155
     * Returns the length of the specified array object, as an {@code int}.
jaroslav@475
   156
     *
jaroslav@475
   157
     * @param array the array
jaroslav@475
   158
     * @return the length of the array
jaroslav@475
   159
     * @exception IllegalArgumentException if the object argument is not
jaroslav@475
   160
     * an array
jaroslav@475
   161
     */
jaroslav@482
   162
    public static int getLength(Object array)
jaroslav@482
   163
    throws IllegalArgumentException {
jaroslav@482
   164
        if (!array.getClass().isArray()) {
jaroslav@483
   165
            throw new IllegalArgumentException("Argument is not an array");
jaroslav@482
   166
        }
jaroslav@482
   167
        return length(array);
jaroslav@482
   168
    }
jaroslav@482
   169
    
jaroslav@482
   170
    @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
jaroslav@482
   171
    private static native int length(Object arr);
jaroslav@475
   172
jaroslav@475
   173
    /**
jaroslav@475
   174
     * Returns the value of the indexed component in the specified
jaroslav@475
   175
     * array object.  The value is automatically wrapped in an object
jaroslav@475
   176
     * if it has a primitive type.
jaroslav@475
   177
     *
jaroslav@475
   178
     * @param array the array
jaroslav@475
   179
     * @param index the index
jaroslav@475
   180
     * @return the (possibly wrapped) value of the indexed component in
jaroslav@475
   181
     * the specified array
jaroslav@475
   182
     * @exception NullPointerException If the specified object is null
jaroslav@475
   183
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   184
     * an array
jaroslav@475
   185
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   186
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   187
     * length of the specified array
jaroslav@475
   188
     */
jaroslav@475
   189
    public static native Object get(Object array, int index)
jaroslav@475
   190
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
jaroslav@475
   191
jaroslav@475
   192
    /**
jaroslav@475
   193
     * Returns the value of the indexed component in the specified
jaroslav@475
   194
     * array object, as a {@code boolean}.
jaroslav@475
   195
     *
jaroslav@475
   196
     * @param array the array
jaroslav@475
   197
     * @param index the index
jaroslav@475
   198
     * @return the value of the indexed component in the specified array
jaroslav@475
   199
     * @exception NullPointerException If the specified object is null
jaroslav@475
   200
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   201
     * an array, or if the indexed element cannot be converted to the
jaroslav@475
   202
     * return type by an identity or widening conversion
jaroslav@475
   203
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   204
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   205
     * length of the specified array
jaroslav@475
   206
     * @see Array#get
jaroslav@475
   207
     */
jaroslav@475
   208
    public static native boolean getBoolean(Object array, int index)
jaroslav@475
   209
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
jaroslav@475
   210
jaroslav@475
   211
    /**
jaroslav@475
   212
     * Returns the value of the indexed component in the specified
jaroslav@475
   213
     * array object, as a {@code byte}.
jaroslav@475
   214
     *
jaroslav@475
   215
     * @param array the array
jaroslav@475
   216
     * @param index the index
jaroslav@475
   217
     * @return the value of the indexed component in the specified array
jaroslav@475
   218
     * @exception NullPointerException If the specified object is null
jaroslav@475
   219
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   220
     * an array, or if the indexed element cannot be converted to the
jaroslav@475
   221
     * return type by an identity or widening conversion
jaroslav@475
   222
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   223
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   224
     * length of the specified array
jaroslav@475
   225
     * @see Array#get
jaroslav@475
   226
     */
jaroslav@483
   227
    public static byte getByte(Object array, int index)
jaroslav@483
   228
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@483
   229
        if (array.getClass().getComponentType() != Byte.TYPE) {
jaroslav@483
   230
            throw new IllegalArgumentException();
jaroslav@483
   231
        }
jaroslav@483
   232
        byte[] arr = (byte[]) array;
jaroslav@483
   233
        return arr[index];
jaroslav@483
   234
    }
jaroslav@475
   235
jaroslav@475
   236
    /**
jaroslav@475
   237
     * Returns the value of the indexed component in the specified
jaroslav@475
   238
     * array object, as a {@code char}.
jaroslav@475
   239
     *
jaroslav@475
   240
     * @param array the array
jaroslav@475
   241
     * @param index the index
jaroslav@475
   242
     * @return the value of the indexed component in the specified array
jaroslav@475
   243
     * @exception NullPointerException If the specified object is null
jaroslav@475
   244
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   245
     * an array, or if the indexed element cannot be converted to the
jaroslav@475
   246
     * return type by an identity or widening conversion
jaroslav@475
   247
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   248
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   249
     * length of the specified array
jaroslav@475
   250
     * @see Array#get
jaroslav@475
   251
     */
jaroslav@475
   252
    public static native char getChar(Object array, int index)
jaroslav@475
   253
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
jaroslav@475
   254
jaroslav@475
   255
    /**
jaroslav@475
   256
     * Returns the value of the indexed component in the specified
jaroslav@475
   257
     * array object, as a {@code short}.
jaroslav@475
   258
     *
jaroslav@475
   259
     * @param array the array
jaroslav@475
   260
     * @param index the index
jaroslav@475
   261
     * @return the value of the indexed component in the specified array
jaroslav@475
   262
     * @exception NullPointerException If the specified object is null
jaroslav@475
   263
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   264
     * an array, or if the indexed element cannot be converted to the
jaroslav@475
   265
     * return type by an identity or widening conversion
jaroslav@475
   266
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   267
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   268
     * length of the specified array
jaroslav@475
   269
     * @see Array#get
jaroslav@475
   270
     */
jaroslav@483
   271
    public static short getShort(Object array, int index)
jaroslav@483
   272
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@483
   273
        final Class<?> t = array.getClass().getComponentType();
jaroslav@483
   274
        if (t == Short.TYPE) {
jaroslav@483
   275
            short[] arr = (short[]) array;
jaroslav@483
   276
            return arr[index];
jaroslav@483
   277
        }
jaroslav@483
   278
        return getByte(array, index);
jaroslav@483
   279
    }
jaroslav@475
   280
jaroslav@475
   281
    /**
jaroslav@475
   282
     * Returns the value of the indexed component in the specified
jaroslav@475
   283
     * array object, as an {@code int}.
jaroslav@475
   284
     *
jaroslav@475
   285
     * @param array the array
jaroslav@475
   286
     * @param index the index
jaroslav@475
   287
     * @return the value of the indexed component in the specified array
jaroslav@475
   288
     * @exception NullPointerException If the specified object is null
jaroslav@475
   289
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   290
     * an array, or if the indexed element cannot be converted to the
jaroslav@475
   291
     * return type by an identity or widening conversion
jaroslav@475
   292
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   293
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   294
     * length of the specified array
jaroslav@475
   295
     * @see Array#get
jaroslav@475
   296
     */
jaroslav@483
   297
    public static int getInt(Object array, int index) 
jaroslav@483
   298
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@483
   299
        final Class<?> t = array.getClass().getComponentType();
jaroslav@483
   300
        if (t == Integer.TYPE) {
jaroslav@483
   301
            int[] arr = (int[]) array;
jaroslav@483
   302
            return arr[index];
jaroslav@483
   303
        }
jaroslav@483
   304
        return getShort(array, index);
jaroslav@483
   305
    }
jaroslav@475
   306
jaroslav@475
   307
    /**
jaroslav@475
   308
     * Returns the value of the indexed component in the specified
jaroslav@475
   309
     * array object, as a {@code long}.
jaroslav@475
   310
     *
jaroslav@475
   311
     * @param array the array
jaroslav@475
   312
     * @param index the index
jaroslav@475
   313
     * @return the value of the indexed component in the specified array
jaroslav@475
   314
     * @exception NullPointerException If the specified object is null
jaroslav@475
   315
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   316
     * an array, or if the indexed element cannot be converted to the
jaroslav@475
   317
     * return type by an identity or widening conversion
jaroslav@475
   318
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   319
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   320
     * length of the specified array
jaroslav@475
   321
     * @see Array#get
jaroslav@475
   322
     */
jaroslav@483
   323
    public static long getLong(Object array, int index)
jaroslav@483
   324
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@483
   325
        final Class<?> t = array.getClass().getComponentType();
jaroslav@483
   326
        if (t == Long.TYPE) {
jaroslav@483
   327
            long[] arr = (long[]) array;
jaroslav@483
   328
            return arr[index];
jaroslav@483
   329
        }
jaroslav@483
   330
        return getInt(array, index);
jaroslav@483
   331
    }
jaroslav@475
   332
jaroslav@475
   333
    /**
jaroslav@475
   334
     * Returns the value of the indexed component in the specified
jaroslav@475
   335
     * array object, as a {@code float}.
jaroslav@475
   336
     *
jaroslav@475
   337
     * @param array the array
jaroslav@475
   338
     * @param index the index
jaroslav@475
   339
     * @return the value of the indexed component in the specified array
jaroslav@475
   340
     * @exception NullPointerException If the specified object is null
jaroslav@475
   341
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   342
     * an array, or if the indexed element cannot be converted to the
jaroslav@475
   343
     * return type by an identity or widening conversion
jaroslav@475
   344
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   345
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   346
     * length of the specified array
jaroslav@475
   347
     * @see Array#get
jaroslav@475
   348
     */
jaroslav@483
   349
    public static float getFloat(Object array, int index)
jaroslav@483
   350
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@483
   351
        final Class<?> t = array.getClass().getComponentType();
jaroslav@483
   352
        if (t == Float.TYPE) {
jaroslav@483
   353
            float[] arr = (float[]) array;
jaroslav@483
   354
            return arr[index];
jaroslav@483
   355
        }
jaroslav@483
   356
        return getLong(array, index);
jaroslav@483
   357
    }
jaroslav@475
   358
jaroslav@475
   359
    /**
jaroslav@475
   360
     * Returns the value of the indexed component in the specified
jaroslav@475
   361
     * array object, as a {@code double}.
jaroslav@475
   362
     *
jaroslav@475
   363
     * @param array the array
jaroslav@475
   364
     * @param index the index
jaroslav@475
   365
     * @return the value of the indexed component in the specified array
jaroslav@475
   366
     * @exception NullPointerException If the specified object is null
jaroslav@475
   367
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   368
     * an array, or if the indexed element cannot be converted to the
jaroslav@475
   369
     * return type by an identity or widening conversion
jaroslav@475
   370
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   371
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   372
     * length of the specified array
jaroslav@475
   373
     * @see Array#get
jaroslav@475
   374
     */
jaroslav@483
   375
    public static double getDouble(Object array, int index)
jaroslav@483
   376
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@483
   377
        final Class<?> t = array.getClass().getComponentType();
jaroslav@483
   378
        if (t == Double.TYPE) {
jaroslav@483
   379
            double[] arr = (double[]) array;
jaroslav@483
   380
            return arr[index];
jaroslav@483
   381
        }
jaroslav@483
   382
        return getFloat(array, index);
jaroslav@483
   383
    }
jaroslav@475
   384
jaroslav@475
   385
    /**
jaroslav@475
   386
     * Sets the value of the indexed component of the specified array
jaroslav@475
   387
     * object to the specified new value.  The new value is first
jaroslav@475
   388
     * automatically unwrapped if the array has a primitive component
jaroslav@475
   389
     * type.
jaroslav@475
   390
     * @param array the array
jaroslav@475
   391
     * @param index the index into the array
jaroslav@475
   392
     * @param value the new value of the indexed component
jaroslav@475
   393
     * @exception NullPointerException If the specified object argument
jaroslav@475
   394
     * is null
jaroslav@475
   395
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   396
     * is not an array, or if the array component type is primitive and
jaroslav@475
   397
     * an unwrapping conversion fails
jaroslav@475
   398
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   399
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   400
     * the length of the specified array
jaroslav@475
   401
     */
jaroslav@475
   402
    public static native void set(Object array, int index, Object value)
jaroslav@475
   403
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
jaroslav@475
   404
jaroslav@475
   405
    /**
jaroslav@475
   406
     * Sets the value of the indexed component of the specified array
jaroslav@475
   407
     * object to the specified {@code boolean} value.
jaroslav@475
   408
     * @param array the array
jaroslav@475
   409
     * @param index the index into the array
jaroslav@475
   410
     * @param z the new value of the indexed component
jaroslav@475
   411
     * @exception NullPointerException If the specified object argument
jaroslav@475
   412
     * is null
jaroslav@475
   413
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   414
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   415
     * to the underlying array's component type by an identity or a
jaroslav@475
   416
     * primitive widening conversion
jaroslav@475
   417
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   418
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   419
     * the length of the specified array
jaroslav@475
   420
     * @see Array#set
jaroslav@475
   421
     */
jaroslav@475
   422
    public static native void setBoolean(Object array, int index, boolean z)
jaroslav@475
   423
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
jaroslav@475
   424
jaroslav@475
   425
    /**
jaroslav@475
   426
     * Sets the value of the indexed component of the specified array
jaroslav@475
   427
     * object to the specified {@code byte} value.
jaroslav@475
   428
     * @param array the array
jaroslav@475
   429
     * @param index the index into the array
jaroslav@475
   430
     * @param b the new value of the indexed component
jaroslav@475
   431
     * @exception NullPointerException If the specified object argument
jaroslav@475
   432
     * is null
jaroslav@475
   433
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   434
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   435
     * to the underlying array's component type by an identity or a
jaroslav@475
   436
     * primitive widening conversion
jaroslav@475
   437
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   438
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   439
     * the length of the specified array
jaroslav@475
   440
     * @see Array#set
jaroslav@475
   441
     */
jaroslav@475
   442
    public static native void setByte(Object array, int index, byte b)
jaroslav@475
   443
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
jaroslav@475
   444
jaroslav@475
   445
    /**
jaroslav@475
   446
     * Sets the value of the indexed component of the specified array
jaroslav@475
   447
     * object to the specified {@code char} value.
jaroslav@475
   448
     * @param array the array
jaroslav@475
   449
     * @param index the index into the array
jaroslav@475
   450
     * @param c the new value of the indexed component
jaroslav@475
   451
     * @exception NullPointerException If the specified object argument
jaroslav@475
   452
     * is null
jaroslav@475
   453
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   454
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   455
     * to the underlying array's component type by an identity or a
jaroslav@475
   456
     * primitive widening conversion
jaroslav@475
   457
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   458
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   459
     * the length of the specified array
jaroslav@475
   460
     * @see Array#set
jaroslav@475
   461
     */
jaroslav@475
   462
    public static native void setChar(Object array, int index, char c)
jaroslav@475
   463
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
jaroslav@475
   464
jaroslav@475
   465
    /**
jaroslav@475
   466
     * Sets the value of the indexed component of the specified array
jaroslav@475
   467
     * object to the specified {@code short} value.
jaroslav@475
   468
     * @param array the array
jaroslav@475
   469
     * @param index the index into the array
jaroslav@475
   470
     * @param s the new value of the indexed component
jaroslav@475
   471
     * @exception NullPointerException If the specified object argument
jaroslav@475
   472
     * is null
jaroslav@475
   473
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   474
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   475
     * to the underlying array's component type by an identity or a
jaroslav@475
   476
     * primitive widening conversion
jaroslav@475
   477
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   478
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   479
     * the length of the specified array
jaroslav@475
   480
     * @see Array#set
jaroslav@475
   481
     */
jaroslav@475
   482
    public static native void setShort(Object array, int index, short s)
jaroslav@475
   483
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
jaroslav@475
   484
jaroslav@475
   485
    /**
jaroslav@475
   486
     * Sets the value of the indexed component of the specified array
jaroslav@475
   487
     * object to the specified {@code int} value.
jaroslav@475
   488
     * @param array the array
jaroslav@475
   489
     * @param index the index into the array
jaroslav@475
   490
     * @param i the new value of the indexed component
jaroslav@475
   491
     * @exception NullPointerException If the specified object argument
jaroslav@475
   492
     * is null
jaroslav@475
   493
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   494
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   495
     * to the underlying array's component type by an identity or a
jaroslav@475
   496
     * primitive widening conversion
jaroslav@475
   497
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   498
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   499
     * the length of the specified array
jaroslav@475
   500
     * @see Array#set
jaroslav@475
   501
     */
jaroslav@475
   502
    public static native void setInt(Object array, int index, int i)
jaroslav@475
   503
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
jaroslav@475
   504
jaroslav@475
   505
    /**
jaroslav@475
   506
     * Sets the value of the indexed component of the specified array
jaroslav@475
   507
     * object to the specified {@code long} value.
jaroslav@475
   508
     * @param array the array
jaroslav@475
   509
     * @param index the index into the array
jaroslav@475
   510
     * @param l the new value of the indexed component
jaroslav@475
   511
     * @exception NullPointerException If the specified object argument
jaroslav@475
   512
     * is null
jaroslav@475
   513
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   514
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   515
     * to the underlying array's component type by an identity or a
jaroslav@475
   516
     * primitive widening conversion
jaroslav@475
   517
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   518
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   519
     * the length of the specified array
jaroslav@475
   520
     * @see Array#set
jaroslav@475
   521
     */
jaroslav@475
   522
    public static native void setLong(Object array, int index, long l)
jaroslav@475
   523
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
jaroslav@475
   524
jaroslav@475
   525
    /**
jaroslav@475
   526
     * Sets the value of the indexed component of the specified array
jaroslav@475
   527
     * object to the specified {@code float} value.
jaroslav@475
   528
     * @param array the array
jaroslav@475
   529
     * @param index the index into the array
jaroslav@475
   530
     * @param f the new value of the indexed component
jaroslav@475
   531
     * @exception NullPointerException If the specified object argument
jaroslav@475
   532
     * is null
jaroslav@475
   533
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   534
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   535
     * to the underlying array's component type by an identity or a
jaroslav@475
   536
     * primitive widening conversion
jaroslav@475
   537
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   538
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   539
     * the length of the specified array
jaroslav@475
   540
     * @see Array#set
jaroslav@475
   541
     */
jaroslav@475
   542
    public static native void setFloat(Object array, int index, float f)
jaroslav@475
   543
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
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 double} value.
jaroslav@475
   548
     * @param array the array
jaroslav@475
   549
     * @param index the index into the array
jaroslav@475
   550
     * @param d 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@475
   562
    public static native void setDouble(Object array, int index, double d)
jaroslav@475
   563
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
jaroslav@475
   564
jaroslav@475
   565
    /*
jaroslav@475
   566
     * Private
jaroslav@475
   567
     */
jaroslav@475
   568
jaroslav@477
   569
    @JavaScriptBody(args = { "primitive", "sig", "length" }, body =
jaroslav@477
   570
          "var arr = new Array(length);\n"
jaroslav@477
   571
        + "var value = primitive ? 0 : null;\n"
jaroslav@477
   572
        + "for(var i = 0; i < length; i++) arr[i] = value;\n"
jaroslav@477
   573
        + "arr.jvmName = sig;\n"
jaroslav@477
   574
        + "return arr;"
jaroslav@477
   575
    )
jaroslav@477
   576
    private static native Object newArray(boolean primitive, String sig, int length);
jaroslav@475
   577
jaroslav@480
   578
    private static Object multiNewArray(String sig, int[] dims, int index)
jaroslav@480
   579
    throws IllegalArgumentException, NegativeArraySizeException {
jaroslav@480
   580
        if (dims.length == index + 1) {
jaroslav@480
   581
            return newArray(sig.length() == 2, sig, dims[index]);
jaroslav@480
   582
        }
jaroslav@480
   583
        Object[] arr = (Object[]) newArray(false, sig, dims[index]);
jaroslav@480
   584
        String compsig = sig.substring(1);
jaroslav@480
   585
        for (int i = 0; i < arr.length; i++) {
jaroslav@480
   586
            arr[i] = multiNewArray(compsig, dims, index + 1);
jaroslav@480
   587
        }
jaroslav@480
   588
        return arr;
jaroslav@480
   589
    }
jaroslav@475
   590
jaroslav@475
   591
jaroslav@477
   592
    
jaroslav@475
   593
}