< prev index next >

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

Print this page
@@ -138,12 +138,22 @@
       * an array
       * @throws    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}.
       *

@@ -310,12 +320,22 @@
       * an unwrapping conversion fails
       * @throws    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 >