rt/emul/mini/src/main/java/java/lang/reflect/Array.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 23 Sep 2014 21:52:27 +0200
changeset 1702 228f26fc1159
parent 1541 3471d74a6b99
permissions -rw-r--r--
for (var x in array) should return only expected values
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@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@1541
    77
        String sig = Method.findArraySignature(componentType);
jaroslav@1532
    78
        return newArray(componentType.isPrimitive(), sig, null, length);
jaroslav@475
    79
    }
jaroslav@477
    80
    
jaroslav@475
    81
    /**
jaroslav@475
    82
     * Creates a new array
jaroslav@475
    83
     * with the specified component type and dimensions.
jaroslav@475
    84
     * If {@code componentType}
jaroslav@475
    85
     * represents a non-array class or interface, the new array
jaroslav@475
    86
     * has {@code dimensions.length} dimensions and
jaroslav@475
    87
     * {@code componentType} as its component type. If
jaroslav@475
    88
     * {@code componentType} represents an array class, the
jaroslav@475
    89
     * number of dimensions of the new array is equal to the sum
jaroslav@475
    90
     * of {@code dimensions.length} and the number of
jaroslav@475
    91
     * dimensions of {@code componentType}. In this case, the
jaroslav@475
    92
     * component type of the new array is the component type of
jaroslav@475
    93
     * {@code componentType}.
jaroslav@475
    94
     *
jaroslav@475
    95
     * <p>The number of dimensions of the new array must not
jaroslav@475
    96
     * exceed the number of array dimensions supported by the
jaroslav@475
    97
     * implementation (typically 255).
jaroslav@475
    98
     *
jaroslav@475
    99
     * @param componentType the {@code Class} object representing the component
jaroslav@475
   100
     * type of the new array
jaroslav@475
   101
     * @param dimensions an array of {@code int} representing the dimensions of
jaroslav@475
   102
     * the new array
jaroslav@475
   103
     * @return the new array
jaroslav@475
   104
     * @exception NullPointerException if the specified
jaroslav@475
   105
     * {@code componentType} argument is null
jaroslav@475
   106
     * @exception IllegalArgumentException if the specified {@code dimensions}
jaroslav@475
   107
     * argument is a zero-dimensional array, or if the number of
jaroslav@475
   108
     * requested dimensions exceeds the limit on the number of array dimensions
jaroslav@475
   109
     * supported by the implementation (typically 255), or if componentType
jaroslav@475
   110
     * is {@link Void#TYPE}.
jaroslav@475
   111
     * @exception NegativeArraySizeException if any of the components in
jaroslav@475
   112
     * the specified {@code dimensions} argument is negative.
jaroslav@475
   113
     */
jaroslav@475
   114
    public static Object newInstance(Class<?> componentType, int... dimensions)
jaroslav@475
   115
        throws IllegalArgumentException, NegativeArraySizeException {
jaroslav@480
   116
        StringBuilder sig = new StringBuilder();
jaroslav@480
   117
        for (int i = 1; i < dimensions.length; i++) {
jaroslav@480
   118
            sig.append('[');
jaroslav@480
   119
        }
jaroslav@1541
   120
        sig.append(Method.findArraySignature(componentType));
jaroslav@480
   121
        return multiNewArray(sig.toString(), dimensions, 0);
jaroslav@475
   122
    }
jaroslav@475
   123
jaroslav@475
   124
    /**
jaroslav@475
   125
     * Returns the length of the specified array object, as an {@code int}.
jaroslav@475
   126
     *
jaroslav@475
   127
     * @param array the array
jaroslav@475
   128
     * @return the length of the array
jaroslav@475
   129
     * @exception IllegalArgumentException if the object argument is not
jaroslav@475
   130
     * an array
jaroslav@475
   131
     */
jaroslav@482
   132
    public static int getLength(Object array)
jaroslav@482
   133
    throws IllegalArgumentException {
jaroslav@482
   134
        if (!array.getClass().isArray()) {
jaroslav@483
   135
            throw new IllegalArgumentException("Argument is not an array");
jaroslav@482
   136
        }
jaroslav@1702
   137
        return Method.arrayLength(array);
jaroslav@482
   138
    }
jaroslav@482
   139
    
jaroslav@475
   140
    /**
jaroslav@475
   141
     * Returns the value of the indexed component in the specified
jaroslav@475
   142
     * array object.  The value is automatically wrapped in an object
jaroslav@475
   143
     * if it has a primitive type.
jaroslav@475
   144
     *
jaroslav@475
   145
     * @param array the array
jaroslav@475
   146
     * @param index the index
jaroslav@475
   147
     * @return the (possibly wrapped) value of the indexed component in
jaroslav@475
   148
     * the specified array
jaroslav@475
   149
     * @exception NullPointerException If the specified object is null
jaroslav@475
   150
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   151
     * an array
jaroslav@475
   152
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   153
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   154
     * length of the specified array
jaroslav@475
   155
     */
jaroslav@485
   156
    public static Object get(Object array, int index)
jaroslav@485
   157
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@486
   158
        final Class<?> t = array.getClass().getComponentType();
jaroslav@486
   159
        if (t.isPrimitive()) {
jaroslav@571
   160
            return fromPrimitive(t, array, index);
jaroslav@485
   161
        } else {
jaroslav@485
   162
            return ((Object[])array)[index];
jaroslav@485
   163
        }
jaroslav@485
   164
    }
jaroslav@475
   165
jaroslav@475
   166
    /**
jaroslav@475
   167
     * Returns the value of the indexed component in the specified
jaroslav@475
   168
     * array object, as a {@code boolean}.
jaroslav@475
   169
     *
jaroslav@475
   170
     * @param array the array
jaroslav@475
   171
     * @param index the index
jaroslav@475
   172
     * @return the value of the indexed component in the specified array
jaroslav@475
   173
     * @exception NullPointerException If the specified object is null
jaroslav@475
   174
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   175
     * an array, or if the indexed element cannot be converted to the
jaroslav@475
   176
     * return type by an identity or widening conversion
jaroslav@475
   177
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   178
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   179
     * length of the specified array
jaroslav@475
   180
     * @see Array#get
jaroslav@475
   181
     */
jaroslav@475
   182
    public static native boolean getBoolean(Object array, int index)
jaroslav@475
   183
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
jaroslav@475
   184
jaroslav@475
   185
    /**
jaroslav@475
   186
     * Returns the value of the indexed component in the specified
jaroslav@475
   187
     * array object, as a {@code byte}.
jaroslav@475
   188
     *
jaroslav@475
   189
     * @param array the array
jaroslav@475
   190
     * @param index the index
jaroslav@475
   191
     * @return the value of the indexed component in the specified array
jaroslav@475
   192
     * @exception NullPointerException If the specified object is null
jaroslav@475
   193
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   194
     * an array, or if the indexed element cannot be converted to the
jaroslav@475
   195
     * return type by an identity or widening conversion
jaroslav@475
   196
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   197
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   198
     * length of the specified array
jaroslav@475
   199
     * @see Array#get
jaroslav@475
   200
     */
jaroslav@483
   201
    public static byte getByte(Object array, int index)
jaroslav@483
   202
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@1541
   203
        if (!Method.samePrimitive(array.getClass().getComponentType(), Byte.TYPE)) {
jaroslav@483
   204
            throw new IllegalArgumentException();
jaroslav@483
   205
        }
jaroslav@483
   206
        byte[] arr = (byte[]) array;
jaroslav@483
   207
        return arr[index];
jaroslav@483
   208
    }
jaroslav@475
   209
jaroslav@475
   210
    /**
jaroslav@475
   211
     * Returns the value of the indexed component in the specified
jaroslav@475
   212
     * array object, as a {@code char}.
jaroslav@475
   213
     *
jaroslav@475
   214
     * @param array the array
jaroslav@475
   215
     * @param index the index
jaroslav@475
   216
     * @return the value of the indexed component in the specified array
jaroslav@475
   217
     * @exception NullPointerException If the specified object is null
jaroslav@475
   218
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   219
     * an array, or if the indexed element cannot be converted to the
jaroslav@475
   220
     * return type by an identity or widening conversion
jaroslav@475
   221
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   222
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   223
     * length of the specified array
jaroslav@475
   224
     * @see Array#get
jaroslav@475
   225
     */
jaroslav@475
   226
    public static native char getChar(Object array, int index)
jaroslav@475
   227
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
jaroslav@475
   228
jaroslav@475
   229
    /**
jaroslav@475
   230
     * Returns the value of the indexed component in the specified
jaroslav@475
   231
     * array object, as a {@code short}.
jaroslav@475
   232
     *
jaroslav@475
   233
     * @param array the array
jaroslav@475
   234
     * @param index the index
jaroslav@475
   235
     * @return the value of the indexed component in the specified array
jaroslav@475
   236
     * @exception NullPointerException If the specified object is null
jaroslav@475
   237
     * @exception IllegalArgumentException If the specified object is not
jaroslav@475
   238
     * an array, or if the indexed element cannot be converted to the
jaroslav@475
   239
     * return type by an identity or widening conversion
jaroslav@475
   240
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   241
     * argument is negative, or if it is greater than or equal to the
jaroslav@475
   242
     * length of the specified array
jaroslav@475
   243
     * @see Array#get
jaroslav@475
   244
     */
jaroslav@483
   245
    public static short getShort(Object array, int index)
jaroslav@483
   246
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@483
   247
        final Class<?> t = array.getClass().getComponentType();
jaroslav@1541
   248
        if (Method.samePrimitive(t, Short.TYPE)) {
jaroslav@483
   249
            short[] arr = (short[]) array;
jaroslav@483
   250
            return arr[index];
jaroslav@483
   251
        }
jaroslav@483
   252
        return getByte(array, index);
jaroslav@483
   253
    }
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 an {@code int}.
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 int getInt(Object array, int index) 
jaroslav@483
   272
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@483
   273
        final Class<?> t = array.getClass().getComponentType();
jaroslav@1541
   274
        if (Method.samePrimitive(t, Integer.TYPE)) {
jaroslav@483
   275
            int[] arr = (int[]) array;
jaroslav@483
   276
            return arr[index];
jaroslav@483
   277
        }
jaroslav@483
   278
        return getShort(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 a {@code long}.
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 long getLong(Object array, int index)
jaroslav@483
   298
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@483
   299
        final Class<?> t = array.getClass().getComponentType();
jaroslav@1541
   300
        if (Method.samePrimitive(t, Long.TYPE)) {
jaroslav@483
   301
            long[] arr = (long[]) array;
jaroslav@483
   302
            return arr[index];
jaroslav@483
   303
        }
jaroslav@483
   304
        return getInt(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 float}.
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 float getFloat(Object array, int index)
jaroslav@483
   324
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@483
   325
        final Class<?> t = array.getClass().getComponentType();
jaroslav@1541
   326
        if (Method.samePrimitive(t, Float.TYPE)) {
jaroslav@483
   327
            float[] arr = (float[]) array;
jaroslav@483
   328
            return arr[index];
jaroslav@483
   329
        }
jaroslav@483
   330
        return getLong(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 double}.
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 double getDouble(Object array, int index)
jaroslav@483
   350
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@483
   351
        final Class<?> t = array.getClass().getComponentType();
jaroslav@1541
   352
        if (Method.samePrimitive(t, Double.TYPE)) {
jaroslav@483
   353
            double[] arr = (double[]) array;
jaroslav@483
   354
            return arr[index];
jaroslav@483
   355
        }
jaroslav@483
   356
        return getFloat(array, index);
jaroslav@483
   357
    }
jaroslav@475
   358
jaroslav@475
   359
    /**
jaroslav@475
   360
     * Sets the value of the indexed component of the specified array
jaroslav@475
   361
     * object to the specified new value.  The new value is first
jaroslav@475
   362
     * automatically unwrapped if the array has a primitive component
jaroslav@475
   363
     * type.
jaroslav@475
   364
     * @param array the array
jaroslav@475
   365
     * @param index the index into the array
jaroslav@475
   366
     * @param value the new value of the indexed component
jaroslav@475
   367
     * @exception NullPointerException If the specified object argument
jaroslav@475
   368
     * is null
jaroslav@475
   369
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   370
     * is not an array, or if the array component type is primitive and
jaroslav@475
   371
     * an unwrapping conversion fails
jaroslav@475
   372
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   373
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   374
     * the length of the specified array
jaroslav@475
   375
     */
jaroslav@485
   376
    public static void set(Object array, int index, Object value)
jaroslav@485
   377
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@485
   378
        if (array.getClass().getComponentType().isPrimitive()) {
jaroslav@485
   379
            throw new IllegalArgumentException();
jaroslav@485
   380
        } else {
jaroslav@485
   381
            Object[] arr = (Object[])array;
jaroslav@485
   382
            arr[index] = value;
jaroslav@485
   383
        }
jaroslav@485
   384
    }
jaroslav@475
   385
jaroslav@475
   386
    /**
jaroslav@475
   387
     * Sets the value of the indexed component of the specified array
jaroslav@475
   388
     * object to the specified {@code boolean} value.
jaroslav@475
   389
     * @param array the array
jaroslav@475
   390
     * @param index the index into the array
jaroslav@475
   391
     * @param z the new value of the indexed component
jaroslav@475
   392
     * @exception NullPointerException If the specified object argument
jaroslav@475
   393
     * is null
jaroslav@475
   394
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   395
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   396
     * to the underlying array's component type by an identity or a
jaroslav@475
   397
     * primitive widening conversion
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
     * @see Array#set
jaroslav@475
   402
     */
jaroslav@475
   403
    public static native void setBoolean(Object array, int index, boolean z)
jaroslav@475
   404
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
jaroslav@475
   405
jaroslav@475
   406
    /**
jaroslav@475
   407
     * Sets the value of the indexed component of the specified array
jaroslav@475
   408
     * object to the specified {@code byte} value.
jaroslav@475
   409
     * @param array the array
jaroslav@475
   410
     * @param index the index into the array
jaroslav@475
   411
     * @param b the new value of the indexed component
jaroslav@475
   412
     * @exception NullPointerException If the specified object argument
jaroslav@475
   413
     * is null
jaroslav@475
   414
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   415
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   416
     * to the underlying array's component type by an identity or a
jaroslav@475
   417
     * primitive widening conversion
jaroslav@475
   418
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   419
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   420
     * the length of the specified array
jaroslav@475
   421
     * @see Array#set
jaroslav@475
   422
     */
jaroslav@484
   423
    public static void setByte(Object array, int index, byte b)
jaroslav@484
   424
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@484
   425
        Class<?> t = array.getClass().getComponentType();
jaroslav@1541
   426
        if (Method.samePrimitive(t, Byte.TYPE)) {
jaroslav@484
   427
            byte[] arr = (byte[]) array;
jaroslav@484
   428
            arr[index] = b;
jaroslav@484
   429
        } else {
jaroslav@484
   430
            setShort(array, index, b);
jaroslav@484
   431
        }
jaroslav@484
   432
    }
jaroslav@475
   433
jaroslav@475
   434
    /**
jaroslav@475
   435
     * Sets the value of the indexed component of the specified array
jaroslav@475
   436
     * object to the specified {@code char} value.
jaroslav@475
   437
     * @param array the array
jaroslav@475
   438
     * @param index the index into the array
jaroslav@475
   439
     * @param c the new value of the indexed component
jaroslav@475
   440
     * @exception NullPointerException If the specified object argument
jaroslav@475
   441
     * is null
jaroslav@475
   442
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   443
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   444
     * to the underlying array's component type by an identity or a
jaroslav@475
   445
     * primitive widening conversion
jaroslav@475
   446
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   447
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   448
     * the length of the specified array
jaroslav@475
   449
     * @see Array#set
jaroslav@475
   450
     */
jaroslav@475
   451
    public static native void setChar(Object array, int index, char c)
jaroslav@475
   452
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
jaroslav@475
   453
jaroslav@475
   454
    /**
jaroslav@475
   455
     * Sets the value of the indexed component of the specified array
jaroslav@475
   456
     * object to the specified {@code short} value.
jaroslav@475
   457
     * @param array the array
jaroslav@475
   458
     * @param index the index into the array
jaroslav@475
   459
     * @param s the new value of the indexed component
jaroslav@475
   460
     * @exception NullPointerException If the specified object argument
jaroslav@475
   461
     * is null
jaroslav@475
   462
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   463
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   464
     * to the underlying array's component type by an identity or a
jaroslav@475
   465
     * primitive widening conversion
jaroslav@475
   466
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   467
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   468
     * the length of the specified array
jaroslav@475
   469
     * @see Array#set
jaroslav@475
   470
     */
jaroslav@484
   471
    public static void setShort(Object array, int index, short s)
jaroslav@484
   472
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@484
   473
        Class<?> t = array.getClass().getComponentType();
jaroslav@1541
   474
        if (Method.samePrimitive(t, Short.TYPE)) {
jaroslav@484
   475
            short[] arr = (short[]) array;
jaroslav@484
   476
            arr[index] = s;
jaroslav@484
   477
        } else {
jaroslav@484
   478
            setInt(array, index, s);
jaroslav@484
   479
        }
jaroslav@484
   480
        
jaroslav@484
   481
    }
jaroslav@571
   482
    
jaroslav@475
   483
    /**
jaroslav@475
   484
     * Sets the value of the indexed component of the specified array
jaroslav@475
   485
     * object to the specified {@code int} value.
jaroslav@475
   486
     * @param array the array
jaroslav@475
   487
     * @param index the index into the array
jaroslav@475
   488
     * @param i the new value of the indexed component
jaroslav@475
   489
     * @exception NullPointerException If the specified object argument
jaroslav@475
   490
     * is null
jaroslav@475
   491
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   492
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   493
     * to the underlying array's component type by an identity or a
jaroslav@475
   494
     * primitive widening conversion
jaroslav@475
   495
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   496
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   497
     * the length of the specified array
jaroslav@475
   498
     * @see Array#set
jaroslav@475
   499
     */
jaroslav@484
   500
    public static void setInt(Object array, int index, int i)
jaroslav@484
   501
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@484
   502
        Class<?> t = array.getClass().getComponentType();
jaroslav@1541
   503
        if (Method.samePrimitive(t, Integer.TYPE)) {
jaroslav@571
   504
            int[] arr = (int[]) array;
jaroslav@484
   505
            arr[index] = i;
jaroslav@484
   506
        } else {
jaroslav@484
   507
            setLong(array, index, i);
jaroslav@484
   508
        }
jaroslav@484
   509
    }
jaroslav@475
   510
jaroslav@475
   511
    /**
jaroslav@475
   512
     * Sets the value of the indexed component of the specified array
jaroslav@475
   513
     * object to the specified {@code long} value.
jaroslav@475
   514
     * @param array the array
jaroslav@475
   515
     * @param index the index into the array
jaroslav@475
   516
     * @param l the new value of the indexed component
jaroslav@475
   517
     * @exception NullPointerException If the specified object argument
jaroslav@475
   518
     * is null
jaroslav@475
   519
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   520
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   521
     * to the underlying array's component type by an identity or a
jaroslav@475
   522
     * primitive widening conversion
jaroslav@475
   523
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   524
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   525
     * the length of the specified array
jaroslav@475
   526
     * @see Array#set
jaroslav@475
   527
     */
jaroslav@484
   528
    public static void setLong(Object array, int index, long l)
jaroslav@484
   529
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@484
   530
        Class<?> t = array.getClass().getComponentType();
jaroslav@1541
   531
        if (Method.samePrimitive(t, Long.TYPE)) {
jaroslav@484
   532
            long[] arr = (long[]) array;
jaroslav@484
   533
            arr[index] = l;
jaroslav@484
   534
        } else {
jaroslav@484
   535
            setFloat(array, index, l);
jaroslav@484
   536
        }
jaroslav@484
   537
    }
jaroslav@475
   538
jaroslav@475
   539
    /**
jaroslav@475
   540
     * Sets the value of the indexed component of the specified array
jaroslav@475
   541
     * object to the specified {@code float} value.
jaroslav@475
   542
     * @param array the array
jaroslav@475
   543
     * @param index the index into the array
jaroslav@475
   544
     * @param f the new value of the indexed component
jaroslav@475
   545
     * @exception NullPointerException If the specified object argument
jaroslav@475
   546
     * is null
jaroslav@475
   547
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   548
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   549
     * to the underlying array's component type by an identity or a
jaroslav@475
   550
     * primitive widening conversion
jaroslav@475
   551
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   552
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   553
     * the length of the specified array
jaroslav@475
   554
     * @see Array#set
jaroslav@475
   555
     */
jaroslav@484
   556
    public static void setFloat(Object array, int index, float f)
jaroslav@484
   557
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@484
   558
        Class<?> t = array.getClass().getComponentType();
jaroslav@1541
   559
        if (Method.samePrimitive(t, Float.TYPE)) {
jaroslav@484
   560
            float[] arr = (float[])array;
jaroslav@484
   561
            arr[index] = f;
jaroslav@484
   562
        } else {
jaroslav@484
   563
            setDouble(array, index, f);
jaroslav@484
   564
        }
jaroslav@484
   565
    }
jaroslav@475
   566
jaroslav@475
   567
    /**
jaroslav@475
   568
     * Sets the value of the indexed component of the specified array
jaroslav@475
   569
     * object to the specified {@code double} value.
jaroslav@475
   570
     * @param array the array
jaroslav@475
   571
     * @param index the index into the array
jaroslav@475
   572
     * @param d the new value of the indexed component
jaroslav@475
   573
     * @exception NullPointerException If the specified object argument
jaroslav@475
   574
     * is null
jaroslav@475
   575
     * @exception IllegalArgumentException If the specified object argument
jaroslav@475
   576
     * is not an array, or if the specified value cannot be converted
jaroslav@475
   577
     * to the underlying array's component type by an identity or a
jaroslav@475
   578
     * primitive widening conversion
jaroslav@475
   579
     * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
jaroslav@475
   580
     * argument is negative, or if it is greater than or equal to
jaroslav@475
   581
     * the length of the specified array
jaroslav@475
   582
     * @see Array#set
jaroslav@475
   583
     */
jaroslav@484
   584
    public static void setDouble(Object array, int index, double d)
jaroslav@484
   585
    throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
jaroslav@484
   586
        Class<?> t = array.getClass().getComponentType();
jaroslav@1541
   587
        if (Method.samePrimitive(t, Double.TYPE)) {
jaroslav@484
   588
            double[] arr = (double[])array;
jaroslav@484
   589
            arr[index] = d;
jaroslav@484
   590
        } else {
jaroslav@484
   591
            throw new IllegalArgumentException("argument type mismatch");
jaroslav@484
   592
        }
jaroslav@484
   593
    }
jaroslav@475
   594
jaroslav@475
   595
    /*
jaroslav@475
   596
     * Private
jaroslav@475
   597
     */
jaroslav@475
   598
lubomir@967
   599
    @Exported
jaroslav@1702
   600
    private static Object newArray(boolean primitive, String sig, Object fn, int length) {
jaroslav@1702
   601
        return Method.newArray(primitive, sig, fn, length);
jaroslav@1702
   602
    }
jaroslav@475
   603
jaroslav@1534
   604
    @Exported
jaroslav@1534
   605
    private static boolean isInstance(Object arr, String sig)  {
jaroslav@1534
   606
        if (arr == null) {
jaroslav@1534
   607
            return false;
jaroslav@1534
   608
        }
jaroslav@1534
   609
        return sig.equals(arr.getClass().getName());
jaroslav@1534
   610
    }
jaroslav@1534
   611
    
jaroslav@1534
   612
    @Exported
jaroslav@1534
   613
    private static boolean isInstance(Object arr, int dimensions, Object fn) throws ClassNotFoundException {
jaroslav@1534
   614
        if (arr == null) {
jaroslav@1534
   615
            return false;
jaroslav@1534
   616
        }
jaroslav@1534
   617
//        log("isInstance for " + arr + " and " + dimensions);
jaroslav@1534
   618
        Class<?> c = arr.getClass();
jaroslav@1534
   619
        while (dimensions-- > 0) {
jaroslav@1534
   620
//            log(" class: " + c);
jaroslav@1534
   621
            c = c.getComponentType();
jaroslav@1534
   622
//            log(" next class: " + c);
jaroslav@1534
   623
            if (c == null) {
jaroslav@1534
   624
                return false;
jaroslav@1534
   625
            }
jaroslav@1534
   626
        }
jaroslav@1702
   627
        Class<?> t = Method.classFromFn(fn);
jaroslav@1534
   628
//        log(" to check: " + t);
jaroslav@1534
   629
        return t.isAssignableFrom(c);
jaroslav@1534
   630
    }
jaroslav@1534
   631
    
jaroslav@1534
   632
//    @JavaScriptBody(args = { "m" }, body = "java.lang.System.out.println(m.toString().toString());")
jaroslav@1534
   633
//    private static native void log(Object m);
jaroslav@1534
   634
    
lubomir@967
   635
    @Exported
jaroslav@1535
   636
    private static Object multiNewArray(String sig, int[] dims, Object fn)
jaroslav@1535
   637
    throws IllegalArgumentException, NegativeArraySizeException {
jaroslav@1535
   638
        return multiNewArray(sig, dims, 0, fn);
jaroslav@1535
   639
    }
jaroslav@1535
   640
    private static Object multiNewArray(String sig, int[] dims, int index, Object fn)
jaroslav@480
   641
    throws IllegalArgumentException, NegativeArraySizeException {
jaroslav@480
   642
        if (dims.length == index + 1) {
jaroslav@1535
   643
            return newArray(sig.length() == 2, sig, fn, dims[index]);
jaroslav@480
   644
        }
jaroslav@1532
   645
        Object arr = newArray(false, sig, null, dims[index]);
jaroslav@480
   646
        String compsig = sig.substring(1);
jaroslav@571
   647
        int len = getLength(arr);
jaroslav@571
   648
        for (int i = 0; i < len; i++) {
jaroslav@1702
   649
            Method.setArray(arr, i, multiNewArray(compsig, dims, index + 1, fn));
jaroslav@480
   650
        }
jaroslav@480
   651
        return arr;
jaroslav@480
   652
    }
jaroslav@486
   653
    private static Object fromPrimitive(Class<?> t, Object array, int index) {
jaroslav@1702
   654
        return Method.fromPrimitive(t, Method.atArray(array, index));
jaroslav@486
   655
    }
jaroslav@475
   656
}