< prev index next >

src/java.base/share/classes/java/lang/reflect/Array.java

Print this page




   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 jdk.internal.HotSpotIntrinsicCandidate;
  29 
  30 /**
  31  * The {@code Array} class provides static methods to dynamically create and
  32  * access Java arrays.
  33  *
  34  * <p>{@code Array} permits widening conversions to occur during a get or set
  35  * operation, but throws an {@code IllegalArgumentException} if a narrowing
  36  * conversion would occur.
  37  *
  38  * @author Nakul Saraiya
  39  * @since 1.1
  40  */
  41 public final
  42 class Array {
  43 
  44     /**
  45      * Constructor.  Class Array is not instantiable.
  46      */
  47     private Array() {}


 125     @HotSpotIntrinsicCandidate
 126     public static native int getLength(Object array)
 127         throws IllegalArgumentException;
 128 
 129     /**
 130      * Returns the value of the indexed component in the specified
 131      * array object.  The value is automatically wrapped in an object
 132      * if it has a primitive type.
 133      *
 134      * @param array the array
 135      * @param index the index
 136      * @return the (possibly wrapped) value of the indexed component in
 137      * the specified array
 138      * @exception NullPointerException If the specified object is null
 139      * @exception IllegalArgumentException If the specified object is not
 140      * an array
 141      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
 142      * argument is negative, or if it is greater than or equal to the
 143      * length of the specified array
 144      */
 145     public static native Object get(Object array, int index)
 146         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;










 147 
 148     /**
 149      * Returns the value of the indexed component in the specified
 150      * array object, as a {@code boolean}.
 151      *
 152      * @param array the array
 153      * @param index the index
 154      * @return the value of the indexed component in the specified array
 155      * @exception NullPointerException If the specified object is null
 156      * @exception IllegalArgumentException If the specified object is not
 157      * an array, or if the indexed element cannot be converted to the
 158      * return type by an identity or widening conversion
 159      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
 160      * argument is negative, or if it is greater than or equal to the
 161      * length of the specified array
 162      * @see Array#get
 163      */
 164     public static native boolean getBoolean(Object array, int index)
 165         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
 166 


 297     public static native double getDouble(Object array, int index)
 298         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
 299 
 300     /**
 301      * Sets the value of the indexed component of the specified array
 302      * object to the specified new value.  The new value is first
 303      * automatically unwrapped if the array has a primitive component
 304      * type.
 305      * @param array the array
 306      * @param index the index into the array
 307      * @param value the new value of the indexed component
 308      * @exception NullPointerException If the specified object argument
 309      * is null
 310      * @exception IllegalArgumentException If the specified object argument
 311      * is not an array, or if the array component type is primitive and
 312      * an unwrapping conversion fails
 313      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
 314      * argument is negative, or if it is greater than or equal to
 315      * the length of the specified array
 316      */
 317     public static native void set(Object array, int index, Object value)
 318         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;










 319 
 320     /**
 321      * Sets the value of the indexed component of the specified array
 322      * object to the specified {@code boolean} value.
 323      * @param array the array
 324      * @param index the index into the array
 325      * @param z the new value of the indexed component
 326      * @exception NullPointerException If the specified object argument
 327      * is null
 328      * @exception IllegalArgumentException If the specified object argument
 329      * is not an array, or if the specified value cannot be converted
 330      * to the underlying array's component type by an identity or a
 331      * primitive widening conversion
 332      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
 333      * argument is negative, or if it is greater than or equal to
 334      * the length of the specified array
 335      * @see Array#set
 336      */
 337     public static native void setBoolean(Object array, int index, boolean z)
 338         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;




   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 java.util.Objects;
  29 import jdk.internal.HotSpotIntrinsicCandidate;
  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  * @since 1.1
  41  */
  42 public final
  43 class Array {
  44 
  45     /**
  46      * Constructor.  Class Array is not instantiable.
  47      */
  48     private Array() {}


 126     @HotSpotIntrinsicCandidate
 127     public static native int getLength(Object array)
 128         throws IllegalArgumentException;
 129 
 130     /**
 131      * Returns the value of the indexed component in the specified
 132      * array object.  The value is automatically wrapped in an object
 133      * if it has a primitive type.
 134      *
 135      * @param array the array
 136      * @param index the index
 137      * @return the (possibly wrapped) value of the indexed component in
 138      * the specified array
 139      * @exception NullPointerException If the specified object is null
 140      * @exception IllegalArgumentException If the specified object is not
 141      * an array
 142      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
 143      * argument is negative, or if it is greater than or equal to the
 144      * length of the specified array
 145      */
 146     public static Object get(Object array, int index)
 147         throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
 148         Class<?> componentType = array.getClass().getComponentType();
 149         if (componentType != null && !componentType.isPrimitive()) {
 150             Object[] objArray = (Object[]) array.getClass().cast(array);
 151             return objArray[index];
 152         } else {
 153             return getReferenceOrPrimitive(array, index);
 154         }
 155     }
 156 
 157     private static native Object getReferenceOrPrimitive(Object array, int index);
 158 
 159     /**
 160      * Returns the value of the indexed component in the specified
 161      * array object, as a {@code boolean}.
 162      *
 163      * @param array the array
 164      * @param index the index
 165      * @return the value of the indexed component in the specified array
 166      * @exception NullPointerException If the specified object is null
 167      * @exception IllegalArgumentException If the specified object is not
 168      * an array, or if the indexed element cannot be converted to the
 169      * return type by an identity or widening conversion
 170      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
 171      * argument is negative, or if it is greater than or equal to the
 172      * length of the specified array
 173      * @see Array#get
 174      */
 175     public static native boolean getBoolean(Object array, int index)
 176         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
 177 


 308     public static native double getDouble(Object array, int index)
 309         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
 310 
 311     /**
 312      * Sets the value of the indexed component of the specified array
 313      * object to the specified new value.  The new value is first
 314      * automatically unwrapped if the array has a primitive component
 315      * type.
 316      * @param array the array
 317      * @param index the index into the array
 318      * @param value the new value of the indexed component
 319      * @exception NullPointerException If the specified object argument
 320      * is null
 321      * @exception IllegalArgumentException If the specified object argument
 322      * is not an array, or if the array component type is primitive and
 323      * an unwrapping conversion fails
 324      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
 325      * argument is negative, or if it is greater than or equal to
 326      * the length of the specified array
 327      */
 328     public static void set(Object array, int index, Object value)
 329         throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
 330         Class<?> componentType = array.getClass().getComponentType();
 331         if (componentType != null && !componentType.isPrimitive()) {
 332             Object[] objArray = (Object[]) array.getClass().cast(array);
 333             objArray[index] = componentType.cast(value);
 334         } else {
 335             setReferenceOrPrimitive(array, index, value);
 336         }
 337     }
 338 
 339     private static native void setReferenceOrPrimitive(Object array, int index, Object value);
 340 
 341     /**
 342      * Sets the value of the indexed component of the specified array
 343      * object to the specified {@code boolean} value.
 344      * @param array the array
 345      * @param index the index into the array
 346      * @param z the new value of the indexed component
 347      * @exception NullPointerException If the specified object argument
 348      * is null
 349      * @exception IllegalArgumentException If the specified object argument
 350      * is not an array, or if the specified value cannot be converted
 351      * to the underlying array's component type by an identity or a
 352      * primitive widening conversion
 353      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
 354      * argument is negative, or if it is greater than or equal to
 355      * the length of the specified array
 356      * @see Array#set
 357      */
 358     public static native void setBoolean(Object array, int index, boolean z)
 359         throws IllegalArgumentException, ArrayIndexOutOfBoundsException;


< prev index next >