< prev index next >

test/jdk/valhalla/valuetypes/ValueArray.java

Print this page
rev 55119 : 8223387: [lworld] reflection support for LW2 arrays

@@ -28,19 +28,24 @@
  * @compile -XDallowWithFieldOperator ValueArray.java
  * @run testng/othervm -XX:+EnableValhalla -XX:ValueArrayElemMaxFlatSize=-1 ValueArray
  * @run testng/othervm -XX:+EnableValhalla -XX:ValueArrayElemMaxFlatSize=0  ValueArray
  */
 
-import java.lang.reflect.*;
+import java.lang.reflect.Array;
 import java.util.Arrays;
 
 import org.testng.annotations.BeforeTest;
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 import static org.testng.Assert.*;
 
 public class ValueArray {
+    private static Class<?> nullablePointArrayClass() {
+        Object a = new Point?[0];
+        return a.getClass();
+    }
+
     @DataProvider(name="arrayTypes")
     static Object[][] arrayTypes() {
         return new Object[][] {
             new Object[] { Object[].class,
                            new Object[] { new Object(), new Object()}},

@@ -49,24 +54,29 @@
                                          Point.makePoint(10, 20),
                                          Point.makePoint(100, 200)}},
             new Object[] { Point[][].class,
                            new Point[][] { new Point[] { Point.makePoint(1, 2),
                                                          Point.makePoint(10, 20)}}},
-
+            new Object[] { nullablePointArrayClass(),
+                           new Point?[] { Point.makePoint(11, 22),
+                                          Point.makePoint(110, 220),
+                                          null}},
             new Object[] { NonFlattenValue[].class,
                            new NonFlattenValue[] { NonFlattenValue.make(1, 2),
                                                    NonFlattenValue.make(10, 20),
                                                    NonFlattenValue.make(100, 200)}},
         };
     }
 
+
     @Test(dataProvider="arrayTypes")
     public static void test(Class<?> c, Object[] elements) {
         ValueArray test = new ValueArray(c, elements.length);
         test.run(elements);
-        if (c.getComponentType().isValue()) {
-            test.ensureNonNullable();
+        Class<?> compType = c.getComponentType();
+        if (compType.isValue()) {
+            test.testSetNullElement(compType == compType.asBoxType());
         }
      }
 
     @Test
     public static void testPointArray() {

@@ -74,10 +84,21 @@
         ValueArray test = new ValueArray(array.points);
         test.run(Point.makePoint(3, 4), Point.makePoint(30, 40));
     }
 
     @Test
+    public static void testNullablePointArray() {
+        Point ?[]array = new Point ?[3];
+        array[0] = Point.makePoint(1, 2);
+        array[1] = null;
+        array[2] = Point.makePoint(3, 4);
+
+        ValueArray test = new ValueArray(array);
+        test.run(null, Point.makePoint(3, 4), null);
+    }
+
+    @Test
     public static void testIntArray() {
         int[] array = new int[] { 1, 2, 3};
         for (int i=0; i < array.length; i++) {
             Array.set(array, i, Integer.valueOf(i*10));
         }

@@ -101,15 +122,57 @@
             Array.set(o, 0, o);
             throw new AssertionError("IAE not thrown");
         } catch (IllegalArgumentException e) {}
 
     }
+
+    @Test()
+    static void testArrayCovariance() {
+        Point[] lArray = new Point[0];
+        Point?[] qArray = new Point?[0];
+
+        // language instanceof
+        assertTrue(lArray instanceof Point[]);
+        assertTrue(qArray instanceof Point?[]);
+
+        // Class.instanceOf (self)
+        assertTrue(lArray.getClass().isInstance(lArray));
+        assertTrue(qArray.getClass().isInstance(qArray));
+
+        // Class.instanceof inline vs indirect
+        assertFalse(lArray.getClass().isInstance(qArray));
+        assertTrue(qArray.getClass().isInstance(lArray));
+
+        // Class.isAssignableFrom (self)
+        assertTrue(lArray.getClass().isAssignableFrom(lArray.getClass()));
+        assertTrue(qArray.getClass().isAssignableFrom(qArray.getClass()));
+
+        // Class.isAssignableFrom inline vs indirect
+        assertTrue(qArray.getClass().isAssignableFrom(lArray.getClass()));
+        assertFalse(lArray.getClass().isAssignableFrom(qArray.getClass()));
+
+        // Class.cast (self)
+        lArray.getClass().cast(lArray);
+        qArray.getClass().cast(qArray);
+
+        // Class.cast inline vs indirect
+        qArray.getClass().cast(lArray);
+        try {
+            lArray.getClass().cast(qArray);
+            fail("cast of Point? to Point should not succeed");
+        } catch (ClassCastException cce) {
+            // expected
+        }
+    }
+
     private final Object[] array;
+
     ValueArray(Class<?> arrayClass, int len) {
         this((Object[])Array.newInstance(arrayClass.getComponentType(), len));
         assertTrue(array.getClass() == arrayClass);
     }
+
     ValueArray(Object[] array) {
         this.array = array;
     }
 
     void run(Object... elements) {

@@ -123,31 +186,34 @@
         }
 
         Arrays.setAll(array, i -> elements[i]);
     }
 
-    void ensureNonNullable() {
+    void testSetNullElement(boolean nullable) {
         assert(array.getClass().getComponentType().isValue());
         for (int i=0; i < array.length; i++) {
             try {
                 Array.set(array, i, null);
+                if (!nullable)
                 throw new AssertionError("NPE not thrown");
-            } catch (NullPointerException e) {}
+            } catch (NullPointerException e) {
+                assertFalse(nullable);
+            }
         }
     }
 
     static inline class PointArray {
-        public Point?[] points;
+        public Point[] points;
         PointArray() {
-            points = new Point?[0];
+            points = new Point[0];
         }
         public static PointArray makeArray(Point... points) {
             PointArray a = PointArray.default;
-            Point?[] boxArray = new Point?[points.length];
+            Point[] array = new Point[points.length];
             for (int i=0; i < points.length; i++) {
-                boxArray[i] = points[i];
+                array[i] = points[i];
             }
-            a = __WithField(a.points, boxArray);
+            a = __WithField(a.points, array);
             return a;
         }
     }
 }
< prev index next >