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