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
     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         StringBuilder sig = new StringBuilder();
   147         for (int i = 1; i < dimensions.length; i++) {
   148             sig.append('[');
   149         }
   150         sig.append(findSignature(componentType));
   151         return multiNewArray(sig.toString(), dimensions, 0);
   152     }
   153 
   154     /**
   155      * Returns the length of the specified array object, as an {@code int}.
   156      *
   157      * @param array the array
   158      * @return the length of the array
   159      * @exception IllegalArgumentException if the object argument is not
   160      * an array
   161      */
   162     public static int getLength(Object array)
   163     throws IllegalArgumentException {
   164         if (!array.getClass().isArray()) {
   165             throw new IllegalArgumentException("Argument is not an array");
   166         }
   167         return length(array);
   168     }
   169     
   170     @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
   171     private static native int length(Object arr);
   172 
   173     /**
   174      * Returns the value of the indexed component in the specified
   175      * array object.  The value is automatically wrapped in an object
   176      * if it has a primitive type.
   177      *
   178      * @param array the array
   179      * @param index the index
   180      * @return the (possibly wrapped) value of the indexed component in
   181      * the specified array
   182      * @exception NullPointerException If the specified object is null
   183      * @exception IllegalArgumentException If the specified object is not
   184      * an array
   185      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   186      * argument is negative, or if it is greater than or equal to the
   187      * length of the specified array
   188      */
   189     public static native Object get(Object array, int index)
   190         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   191 
   192     /**
   193      * Returns the value of the indexed component in the specified
   194      * array object, as a {@code boolean}.
   195      *
   196      * @param array the array
   197      * @param index the index
   198      * @return the value of the indexed component in the specified array
   199      * @exception NullPointerException If the specified object is null
   200      * @exception IllegalArgumentException If the specified object is not
   201      * an array, or if the indexed element cannot be converted to the
   202      * return type by an identity or widening conversion
   203      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   204      * argument is negative, or if it is greater than or equal to the
   205      * length of the specified array
   206      * @see Array#get
   207      */
   208     public static native boolean getBoolean(Object array, int index)
   209         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   210 
   211     /**
   212      * Returns the value of the indexed component in the specified
   213      * array object, as a {@code byte}.
   214      *
   215      * @param array the array
   216      * @param index the index
   217      * @return the value of the indexed component in the specified array
   218      * @exception NullPointerException If the specified object is null
   219      * @exception IllegalArgumentException If the specified object is not
   220      * an array, or if the indexed element cannot be converted to the
   221      * return type by an identity or widening conversion
   222      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   223      * argument is negative, or if it is greater than or equal to the
   224      * length of the specified array
   225      * @see Array#get
   226      */
   227     public static byte getByte(Object array, int index)
   228     throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
   229         if (array.getClass().getComponentType() != Byte.TYPE) {
   230             throw new IllegalArgumentException();
   231         }
   232         byte[] arr = (byte[]) array;
   233         return arr[index];
   234     }
   235 
   236     /**
   237      * Returns the value of the indexed component in the specified
   238      * array object, as a {@code char}.
   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 char getChar(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 a {@code short}.
   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 short getShort(Object array, int index)
   272     throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
   273         final Class<?> t = array.getClass().getComponentType();
   274         if (t == Short.TYPE) {
   275             short[] arr = (short[]) array;
   276             return arr[index];
   277         }
   278         return getByte(array, index);
   279     }
   280 
   281     /**
   282      * Returns the value of the indexed component in the specified
   283      * array object, as an {@code int}.
   284      *
   285      * @param array the array
   286      * @param index the index
   287      * @return the value of the indexed component in the specified array
   288      * @exception NullPointerException If the specified object is null
   289      * @exception IllegalArgumentException If the specified object is not
   290      * an array, or if the indexed element cannot be converted to the
   291      * return type by an identity or widening conversion
   292      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   293      * argument is negative, or if it is greater than or equal to the
   294      * length of the specified array
   295      * @see Array#get
   296      */
   297     public static int getInt(Object array, int index) 
   298     throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
   299         final Class<?> t = array.getClass().getComponentType();
   300         if (t == Integer.TYPE) {
   301             int[] arr = (int[]) array;
   302             return arr[index];
   303         }
   304         return getShort(array, index);
   305     }
   306 
   307     /**
   308      * Returns the value of the indexed component in the specified
   309      * array object, as a {@code long}.
   310      *
   311      * @param array the array
   312      * @param index the index
   313      * @return the value of the indexed component in the specified array
   314      * @exception NullPointerException If the specified object is null
   315      * @exception IllegalArgumentException If the specified object is not
   316      * an array, or if the indexed element cannot be converted to the
   317      * return type by an identity or widening conversion
   318      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   319      * argument is negative, or if it is greater than or equal to the
   320      * length of the specified array
   321      * @see Array#get
   322      */
   323     public static long getLong(Object array, int index)
   324     throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
   325         final Class<?> t = array.getClass().getComponentType();
   326         if (t == Long.TYPE) {
   327             long[] arr = (long[]) array;
   328             return arr[index];
   329         }
   330         return getInt(array, index);
   331     }
   332 
   333     /**
   334      * Returns the value of the indexed component in the specified
   335      * array object, as a {@code float}.
   336      *
   337      * @param array the array
   338      * @param index the index
   339      * @return the value of the indexed component in the specified array
   340      * @exception NullPointerException If the specified object is null
   341      * @exception IllegalArgumentException If the specified object is not
   342      * an array, or if the indexed element cannot be converted to the
   343      * return type by an identity or widening conversion
   344      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   345      * argument is negative, or if it is greater than or equal to the
   346      * length of the specified array
   347      * @see Array#get
   348      */
   349     public static float getFloat(Object array, int index)
   350     throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
   351         final Class<?> t = array.getClass().getComponentType();
   352         if (t == Float.TYPE) {
   353             float[] arr = (float[]) array;
   354             return arr[index];
   355         }
   356         return getLong(array, index);
   357     }
   358 
   359     /**
   360      * Returns the value of the indexed component in the specified
   361      * array object, as a {@code double}.
   362      *
   363      * @param array the array
   364      * @param index the index
   365      * @return the value of the indexed component in the specified array
   366      * @exception NullPointerException If the specified object is null
   367      * @exception IllegalArgumentException If the specified object is not
   368      * an array, or if the indexed element cannot be converted to the
   369      * return type by an identity or widening conversion
   370      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   371      * argument is negative, or if it is greater than or equal to the
   372      * length of the specified array
   373      * @see Array#get
   374      */
   375     public static double getDouble(Object array, int index)
   376     throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
   377         final Class<?> t = array.getClass().getComponentType();
   378         if (t == Double.TYPE) {
   379             double[] arr = (double[]) array;
   380             return arr[index];
   381         }
   382         return getFloat(array, index);
   383     }
   384 
   385     /**
   386      * Sets the value of the indexed component of the specified array
   387      * object to the specified new value.  The new value is first
   388      * automatically unwrapped if the array has a primitive component
   389      * type.
   390      * @param array the array
   391      * @param index the index into the array
   392      * @param value the new value of the indexed component
   393      * @exception NullPointerException If the specified object argument
   394      * is null
   395      * @exception IllegalArgumentException If the specified object argument
   396      * is not an array, or if the array component type is primitive and
   397      * an unwrapping conversion fails
   398      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   399      * argument is negative, or if it is greater than or equal to
   400      * the length of the specified array
   401      */
   402     public static native void set(Object array, int index, Object value)
   403         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   404 
   405     /**
   406      * Sets the value of the indexed component of the specified array
   407      * object to the specified {@code boolean} value.
   408      * @param array the array
   409      * @param index the index into the array
   410      * @param z the new value of the indexed component
   411      * @exception NullPointerException If the specified object argument
   412      * is null
   413      * @exception IllegalArgumentException If the specified object argument
   414      * is not an array, or if the specified value cannot be converted
   415      * to the underlying array's component type by an identity or a
   416      * primitive widening conversion
   417      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   418      * argument is negative, or if it is greater than or equal to
   419      * the length of the specified array
   420      * @see Array#set
   421      */
   422     public static native void setBoolean(Object array, int index, boolean z)
   423         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   424 
   425     /**
   426      * Sets the value of the indexed component of the specified array
   427      * object to the specified {@code byte} value.
   428      * @param array the array
   429      * @param index the index into the array
   430      * @param b the new value of the indexed component
   431      * @exception NullPointerException If the specified object argument
   432      * is null
   433      * @exception IllegalArgumentException If the specified object argument
   434      * is not an array, or if the specified value cannot be converted
   435      * to the underlying array's component type by an identity or a
   436      * primitive widening conversion
   437      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   438      * argument is negative, or if it is greater than or equal to
   439      * the length of the specified array
   440      * @see Array#set
   441      */
   442     public static native void setByte(Object array, int index, byte b)
   443         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   444 
   445     /**
   446      * Sets the value of the indexed component of the specified array
   447      * object to the specified {@code char} value.
   448      * @param array the array
   449      * @param index the index into the array
   450      * @param c the new value of the indexed component
   451      * @exception NullPointerException If the specified object argument
   452      * is null
   453      * @exception IllegalArgumentException If the specified object argument
   454      * is not an array, or if the specified value cannot be converted
   455      * to the underlying array's component type by an identity or a
   456      * primitive widening conversion
   457      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   458      * argument is negative, or if it is greater than or equal to
   459      * the length of the specified array
   460      * @see Array#set
   461      */
   462     public static native void setChar(Object array, int index, char c)
   463         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   464 
   465     /**
   466      * Sets the value of the indexed component of the specified array
   467      * object to the specified {@code short} value.
   468      * @param array the array
   469      * @param index the index into the array
   470      * @param s the new value of the indexed component
   471      * @exception NullPointerException If the specified object argument
   472      * is null
   473      * @exception IllegalArgumentException If the specified object argument
   474      * is not an array, or if the specified value cannot be converted
   475      * to the underlying array's component type by an identity or a
   476      * primitive widening conversion
   477      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   478      * argument is negative, or if it is greater than or equal to
   479      * the length of the specified array
   480      * @see Array#set
   481      */
   482     public static native void setShort(Object array, int index, short s)
   483         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   484 
   485     /**
   486      * Sets the value of the indexed component of the specified array
   487      * object to the specified {@code int} value.
   488      * @param array the array
   489      * @param index the index into the array
   490      * @param i the new value of the indexed component
   491      * @exception NullPointerException If the specified object argument
   492      * is null
   493      * @exception IllegalArgumentException If the specified object argument
   494      * is not an array, or if the specified value cannot be converted
   495      * to the underlying array's component type by an identity or a
   496      * primitive widening conversion
   497      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   498      * argument is negative, or if it is greater than or equal to
   499      * the length of the specified array
   500      * @see Array#set
   501      */
   502     public static native void setInt(Object array, int index, int i)
   503         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   504 
   505     /**
   506      * Sets the value of the indexed component of the specified array
   507      * object to the specified {@code long} value.
   508      * @param array the array
   509      * @param index the index into the array
   510      * @param l the new value of the indexed component
   511      * @exception NullPointerException If the specified object argument
   512      * is null
   513      * @exception IllegalArgumentException If the specified object argument
   514      * is not an array, or if the specified value cannot be converted
   515      * to the underlying array's component type by an identity or a
   516      * primitive widening conversion
   517      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   518      * argument is negative, or if it is greater than or equal to
   519      * the length of the specified array
   520      * @see Array#set
   521      */
   522     public static native void setLong(Object array, int index, long l)
   523         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   524 
   525     /**
   526      * Sets the value of the indexed component of the specified array
   527      * object to the specified {@code float} value.
   528      * @param array the array
   529      * @param index the index into the array
   530      * @param f the new value of the indexed component
   531      * @exception NullPointerException If the specified object argument
   532      * is null
   533      * @exception IllegalArgumentException If the specified object argument
   534      * is not an array, or if the specified value cannot be converted
   535      * to the underlying array's component type by an identity or a
   536      * primitive widening conversion
   537      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   538      * argument is negative, or if it is greater than or equal to
   539      * the length of the specified array
   540      * @see Array#set
   541      */
   542     public static native void setFloat(Object array, int index, float f)
   543         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   544 
   545     /**
   546      * Sets the value of the indexed component of the specified array
   547      * object to the specified {@code double} value.
   548      * @param array the array
   549      * @param index the index into the array
   550      * @param d the new value of the indexed component
   551      * @exception NullPointerException If the specified object argument
   552      * is null
   553      * @exception IllegalArgumentException If the specified object argument
   554      * is not an array, or if the specified value cannot be converted
   555      * to the underlying array's component type by an identity or a
   556      * primitive widening conversion
   557      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   558      * argument is negative, or if it is greater than or equal to
   559      * the length of the specified array
   560      * @see Array#set
   561      */
   562     public static native void setDouble(Object array, int index, double d)
   563         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   564 
   565     /*
   566      * Private
   567      */
   568 
   569     @JavaScriptBody(args = { "primitive", "sig", "length" }, body =
   570           "var arr = new Array(length);\n"
   571         + "var value = primitive ? 0 : null;\n"
   572         + "for(var i = 0; i < length; i++) arr[i] = value;\n"
   573         + "arr.jvmName = sig;\n"
   574         + "return arr;"
   575     )
   576     private static native Object newArray(boolean primitive, String sig, int length);
   577 
   578     private static Object multiNewArray(String sig, int[] dims, int index)
   579     throws IllegalArgumentException, NegativeArraySizeException {
   580         if (dims.length == index + 1) {
   581             return newArray(sig.length() == 2, sig, dims[index]);
   582         }
   583         Object[] arr = (Object[]) newArray(false, sig, dims[index]);
   584         String compsig = sig.substring(1);
   585         for (int i = 0; i < arr.length; i++) {
   586             arr[i] = multiNewArray(compsig, dims, index + 1);
   587         }
   588         return arr;
   589     }
   590 
   591 
   592     
   593 }