emul/src/main/java/java/lang/reflect/Array.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 18 Jan 2013 19:22:49 +0100
branchArrayReflect
changeset 482 05a87bc23192
parent 480 dfebf4fbb711
child 483 7e39f344e4a0
permissions -rw-r--r--
Array.getLength and few more (failing) tests for reflection
     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();
   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 native byte getByte(Object array, int index)
   228         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   229 
   230     /**
   231      * Returns the value of the indexed component in the specified
   232      * array object, as a {@code char}.
   233      *
   234      * @param array the array
   235      * @param index the index
   236      * @return the value of the indexed component in the specified array
   237      * @exception NullPointerException If the specified object is null
   238      * @exception IllegalArgumentException If the specified object is not
   239      * an array, or if the indexed element cannot be converted to the
   240      * return type by an identity or widening conversion
   241      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   242      * argument is negative, or if it is greater than or equal to the
   243      * length of the specified array
   244      * @see Array#get
   245      */
   246     public static native char getChar(Object array, int index)
   247         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   248 
   249     /**
   250      * Returns the value of the indexed component in the specified
   251      * array object, as a {@code short}.
   252      *
   253      * @param array the array
   254      * @param index the index
   255      * @return the value of the indexed component in the specified array
   256      * @exception NullPointerException If the specified object is null
   257      * @exception IllegalArgumentException If the specified object is not
   258      * an array, or if the indexed element cannot be converted to the
   259      * return type by an identity or widening conversion
   260      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   261      * argument is negative, or if it is greater than or equal to the
   262      * length of the specified array
   263      * @see Array#get
   264      */
   265     public static native short getShort(Object array, int index)
   266         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   267 
   268     /**
   269      * Returns the value of the indexed component in the specified
   270      * array object, as an {@code int}.
   271      *
   272      * @param array the array
   273      * @param index the index
   274      * @return the value of the indexed component in the specified array
   275      * @exception NullPointerException If the specified object is null
   276      * @exception IllegalArgumentException If the specified object is not
   277      * an array, or if the indexed element cannot be converted to the
   278      * return type by an identity or widening conversion
   279      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   280      * argument is negative, or if it is greater than or equal to the
   281      * length of the specified array
   282      * @see Array#get
   283      */
   284     public static native int getInt(Object array, int index)
   285         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   286 
   287     /**
   288      * Returns the value of the indexed component in the specified
   289      * array object, as a {@code long}.
   290      *
   291      * @param array the array
   292      * @param index the index
   293      * @return the value of the indexed component in the specified array
   294      * @exception NullPointerException If the specified object is null
   295      * @exception IllegalArgumentException If the specified object is not
   296      * an array, or if the indexed element cannot be converted to the
   297      * return type by an identity or widening conversion
   298      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   299      * argument is negative, or if it is greater than or equal to the
   300      * length of the specified array
   301      * @see Array#get
   302      */
   303     public static native long getLong(Object array, int index)
   304         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   305 
   306     /**
   307      * Returns the value of the indexed component in the specified
   308      * array object, as a {@code float}.
   309      *
   310      * @param array the array
   311      * @param index the index
   312      * @return the value of the indexed component in the specified array
   313      * @exception NullPointerException If the specified object is null
   314      * @exception IllegalArgumentException If the specified object is not
   315      * an array, or if the indexed element cannot be converted to the
   316      * return type by an identity or widening conversion
   317      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   318      * argument is negative, or if it is greater than or equal to the
   319      * length of the specified array
   320      * @see Array#get
   321      */
   322     public static native float getFloat(Object array, int index)
   323         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   324 
   325     /**
   326      * Returns the value of the indexed component in the specified
   327      * array object, as a {@code double}.
   328      *
   329      * @param array the array
   330      * @param index the index
   331      * @return the value of the indexed component in the specified array
   332      * @exception NullPointerException If the specified object is null
   333      * @exception IllegalArgumentException If the specified object is not
   334      * an array, or if the indexed element cannot be converted to the
   335      * return type by an identity or widening conversion
   336      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   337      * argument is negative, or if it is greater than or equal to the
   338      * length of the specified array
   339      * @see Array#get
   340      */
   341     public static native double getDouble(Object array, int index)
   342         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   343 
   344     /**
   345      * Sets the value of the indexed component of the specified array
   346      * object to the specified new value.  The new value is first
   347      * automatically unwrapped if the array has a primitive component
   348      * type.
   349      * @param array the array
   350      * @param index the index into the array
   351      * @param value the new value of the indexed component
   352      * @exception NullPointerException If the specified object argument
   353      * is null
   354      * @exception IllegalArgumentException If the specified object argument
   355      * is not an array, or if the array component type is primitive and
   356      * an unwrapping conversion fails
   357      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   358      * argument is negative, or if it is greater than or equal to
   359      * the length of the specified array
   360      */
   361     public static native void set(Object array, int index, Object value)
   362         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   363 
   364     /**
   365      * Sets the value of the indexed component of the specified array
   366      * object to the specified {@code boolean} value.
   367      * @param array the array
   368      * @param index the index into the array
   369      * @param z the new value of the indexed component
   370      * @exception NullPointerException If the specified object argument
   371      * is null
   372      * @exception IllegalArgumentException If the specified object argument
   373      * is not an array, or if the specified value cannot be converted
   374      * to the underlying array's component type by an identity or a
   375      * primitive widening conversion
   376      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   377      * argument is negative, or if it is greater than or equal to
   378      * the length of the specified array
   379      * @see Array#set
   380      */
   381     public static native void setBoolean(Object array, int index, boolean z)
   382         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   383 
   384     /**
   385      * Sets the value of the indexed component of the specified array
   386      * object to the specified {@code byte} value.
   387      * @param array the array
   388      * @param index the index into the array
   389      * @param b the new value of the indexed component
   390      * @exception NullPointerException If the specified object argument
   391      * is null
   392      * @exception IllegalArgumentException If the specified object argument
   393      * is not an array, or if the specified value cannot be converted
   394      * to the underlying array's component type by an identity or a
   395      * primitive widening conversion
   396      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   397      * argument is negative, or if it is greater than or equal to
   398      * the length of the specified array
   399      * @see Array#set
   400      */
   401     public static native void setByte(Object array, int index, byte b)
   402         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   403 
   404     /**
   405      * Sets the value of the indexed component of the specified array
   406      * object to the specified {@code char} value.
   407      * @param array the array
   408      * @param index the index into the array
   409      * @param c the new value of the indexed component
   410      * @exception NullPointerException If the specified object argument
   411      * is null
   412      * @exception IllegalArgumentException If the specified object argument
   413      * is not an array, or if the specified value cannot be converted
   414      * to the underlying array's component type by an identity or a
   415      * primitive widening conversion
   416      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   417      * argument is negative, or if it is greater than or equal to
   418      * the length of the specified array
   419      * @see Array#set
   420      */
   421     public static native void setChar(Object array, int index, char c)
   422         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   423 
   424     /**
   425      * Sets the value of the indexed component of the specified array
   426      * object to the specified {@code short} value.
   427      * @param array the array
   428      * @param index the index into the array
   429      * @param s the new value of the indexed component
   430      * @exception NullPointerException If the specified object argument
   431      * is null
   432      * @exception IllegalArgumentException If the specified object argument
   433      * is not an array, or if the specified value cannot be converted
   434      * to the underlying array's component type by an identity or a
   435      * primitive widening conversion
   436      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   437      * argument is negative, or if it is greater than or equal to
   438      * the length of the specified array
   439      * @see Array#set
   440      */
   441     public static native void setShort(Object array, int index, short s)
   442         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   443 
   444     /**
   445      * Sets the value of the indexed component of the specified array
   446      * object to the specified {@code int} value.
   447      * @param array the array
   448      * @param index the index into the array
   449      * @param i the new value of the indexed component
   450      * @exception NullPointerException If the specified object argument
   451      * is null
   452      * @exception IllegalArgumentException If the specified object argument
   453      * is not an array, or if the specified value cannot be converted
   454      * to the underlying array's component type by an identity or a
   455      * primitive widening conversion
   456      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   457      * argument is negative, or if it is greater than or equal to
   458      * the length of the specified array
   459      * @see Array#set
   460      */
   461     public static native void setInt(Object array, int index, int i)
   462         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   463 
   464     /**
   465      * Sets the value of the indexed component of the specified array
   466      * object to the specified {@code long} value.
   467      * @param array the array
   468      * @param index the index into the array
   469      * @param l the new value of the indexed component
   470      * @exception NullPointerException If the specified object argument
   471      * is null
   472      * @exception IllegalArgumentException If the specified object argument
   473      * is not an array, or if the specified value cannot be converted
   474      * to the underlying array's component type by an identity or a
   475      * primitive widening conversion
   476      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   477      * argument is negative, or if it is greater than or equal to
   478      * the length of the specified array
   479      * @see Array#set
   480      */
   481     public static native void setLong(Object array, int index, long l)
   482         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   483 
   484     /**
   485      * Sets the value of the indexed component of the specified array
   486      * object to the specified {@code float} value.
   487      * @param array the array
   488      * @param index the index into the array
   489      * @param f the new value of the indexed component
   490      * @exception NullPointerException If the specified object argument
   491      * is null
   492      * @exception IllegalArgumentException If the specified object argument
   493      * is not an array, or if the specified value cannot be converted
   494      * to the underlying array's component type by an identity or a
   495      * primitive widening conversion
   496      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   497      * argument is negative, or if it is greater than or equal to
   498      * the length of the specified array
   499      * @see Array#set
   500      */
   501     public static native void setFloat(Object array, int index, float f)
   502         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   503 
   504     /**
   505      * Sets the value of the indexed component of the specified array
   506      * object to the specified {@code double} value.
   507      * @param array the array
   508      * @param index the index into the array
   509      * @param d the new value of the indexed component
   510      * @exception NullPointerException If the specified object argument
   511      * is null
   512      * @exception IllegalArgumentException If the specified object argument
   513      * is not an array, or if the specified value cannot be converted
   514      * to the underlying array's component type by an identity or a
   515      * primitive widening conversion
   516      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
   517      * argument is negative, or if it is greater than or equal to
   518      * the length of the specified array
   519      * @see Array#set
   520      */
   521     public static native void setDouble(Object array, int index, double d)
   522         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
   523 
   524     /*
   525      * Private
   526      */
   527 
   528     @JavaScriptBody(args = { "primitive", "sig", "length" }, body =
   529           "var arr = new Array(length);\n"
   530         + "var value = primitive ? 0 : null;\n"
   531         + "for(var i = 0; i < length; i++) arr[i] = value;\n"
   532         + "arr.jvmName = sig;\n"
   533         + "return arr;"
   534     )
   535     private static native Object newArray(boolean primitive, String sig, int length);
   536 
   537     private static Object multiNewArray(String sig, int[] dims, int index)
   538     throws IllegalArgumentException, NegativeArraySizeException {
   539         if (dims.length == index + 1) {
   540             return newArray(sig.length() == 2, sig, dims[index]);
   541         }
   542         Object[] arr = (Object[]) newArray(false, sig, dims[index]);
   543         String compsig = sig.substring(1);
   544         for (int i = 0; i < arr.length; i++) {
   545             arr[i] = multiNewArray(compsig, dims, index + 1);
   546         }
   547         return arr;
   548     }
   549 
   550 
   551     
   552 }