< 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.vm.annotation.IntrinsicCandidate;
 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() {}

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










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

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










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

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

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