< prev index next >

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

Print this page

        

@@ -23,10 +23,11 @@
  * questions.
  */
 
 package java.lang.reflect;
 
+import java.util.Objects;
 import jdk.internal.HotSpotIntrinsicCandidate;
 
 /**
  * The {@code Array} class provides static methods to dynamically create and
  * access Java arrays.

@@ -140,12 +141,22 @@
      * an array
      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
      * argument is negative, or if it is greater than or equal to the
      * length of the specified array
      */
-    public static native Object get(Object array, int index)
-        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
+    public static Object get(Object array, int index)
+        throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
+        Class<?> componentType = array.getClass().getComponentType();
+        if (componentType != null && !componentType.isPrimitive()) {
+            Object[] objArray = (Object[]) array.getClass().cast(array);
+            return objArray[index];
+        } else {
+            return getReferenceOrPrimitive(array, index);
+        }
+    }
+
+    private static native Object getReferenceOrPrimitive(Object array, int index);
 
     /**
      * Returns the value of the indexed component in the specified
      * array object, as a {@code boolean}.
      *

@@ -312,12 +323,22 @@
      * an unwrapping conversion fails
      * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
      * argument is negative, or if it is greater than or equal to
      * the length of the specified array
      */
-    public static native void set(Object array, int index, Object value)
-        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
+    public static void set(Object array, int index, Object value)
+        throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
+        Class<?> componentType = array.getClass().getComponentType();
+        if (componentType != null && !componentType.isPrimitive()) {
+            Object[] objArray = (Object[]) array.getClass().cast(array);
+            objArray[index] = componentType.cast(value);
+        } else {
+            setReferenceOrPrimitive(array, index, value);
+        }
+    }
+
+    private static native void setReferenceOrPrimitive(Object array, int index, Object value);
 
     /**
      * Sets the value of the indexed component of the specified array
      * object to the specified {@code boolean} value.
      * @param array the array
< prev index next >