< prev index next >

test/jdk/valhalla/valuetypes/Reflection.java

Print this page
rev 55688 : 8223349: [lworld] Reflection support on static <init> factory methods for inline types
Reviewed-by: jrose

@@ -27,12 +27,18 @@
  * @summary test reflection on inline types
  * @compile -XDallowWithFieldOperator Point.java Line.java NonFlattenValue.java
  * @run main/othervm -XX:+EnableValhalla Reflection
  */
 
-import java.lang.reflect.*;
+import java.lang.reflect.Array;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.InaccessibleObjectException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
 import java.util.Arrays;
+import java.util.List;
 import java.util.stream.Collectors;
 
 public class Reflection {
     public static void main(String... args) throws Exception {
         testPointClass();

@@ -45,32 +51,39 @@
     static void testPointClass() throws Exception  {
         Point o = Point.makePoint(10, 20);
         Reflection test = new Reflection(Point.class, "Point", o);
         test.newInstance();
         test.constructor();
+        test.constructors("public Point(int,int)", "Point()");
         test.accessFieldX(o.x);
         test.setAccessible();
         test.trySetAccessible();
         test.staticField();
+        test.initFactoryNotMethods();
     }
 
     static void testLineClass() throws Exception {
         Line l = Line.makeLine(10, 20, 30, 40);
         Reflection test = new Reflection(Line.class, "Line", l);
         test.checkField("public final Point Line.p1", "p1", Point.class);
         test.checkField("public final Point Line.p2", "p2", Point.class);
         test.checkMethod("public Point Line.p1()",           "p1", Point.class);
         test.checkMethod("public Point Line.p2()",           "p2", Point.class);
+        test.initFactoryNotMethods();
+        test.constructors("Line()");
     }
 
     static void testNonFlattenValue() throws Exception {
         NonFlattenValue nfv = NonFlattenValue.make(10, 20);
         Reflection test = new Reflection(NonFlattenValue.class, "NonFlattenValue", nfv);
         test.checkField("final Point? NonFlattenValue.nfp", "nfp", Point.class.asNullableType());
         test.checkMethod("public Point NonFlattenValue.pointValue()", "pointValue", Point.class);
         test.checkMethod("public Point? NonFlattenValue.point()", "point", Point.class.asNullableType());
         test.checkMethod("public boolean NonFlattenValue.has(Point,Point?)", "has", boolean.class, Point.class, Point.class.asNullableType());
+        test.initFactoryNotMethods();
+        test.constructors("NonFlattenValue()");
+
     }
 
     /*
      * Tests reflection APIs with the primary and nullable mirror
      */

@@ -171,40 +184,47 @@
             field.setInt(o, 100);
             throw new RuntimeException("IllegalAccessException not thrown");
         } catch (IllegalAccessException e) {}
     }
 
+    @SuppressWarnings("deprecation")
     void newInstance() throws Exception {
-        try {
             Object o = c.newInstance();
-            throw new RuntimeException("newInstance expected to be unsupported on inline class");
-        } catch (IllegalAccessException e) {}
+        assertEquals(o.getClass(), c);
     }
 
     void constructor() throws Exception {
-        try {
-            ctor.newInstance();
-            throw new RuntimeException("IllegalAccessException not thrown");
-        } catch (IllegalAccessException e) { }
+        Object o = ctor.newInstance();
+        assertEquals(o.getClass(), c);
+    }
+
+    // Check that the class has the expected Constructors
+    void constructors(String... expected) throws Exception {
+        Constructor<? extends Object>[] cons = c.getDeclaredConstructors();
+        List<String> declaredSig = Arrays.stream(cons).map( Constructor::toString).collect(Collectors.toList());
+        List<String> expectedSig = List.of(expected);
+        boolean ok = expectedSig.equals(declaredSig);
+        if (!ok) {
+            System.out.printf("expected: %s%n", expectedSig);
+            System.out.printf("declared: %s%n", declaredSig);
+            assertTrue(ok);
+        }
     }
 
     void setAccessible() throws Exception {
-        try {
             ctor.setAccessible(true);
-            throw new RuntimeException("InaccessibleObjectException not thrown");
-        } catch (InaccessibleObjectException e) { e.printStackTrace(); }
         Field field = c.getField("x");
         try {
             field.setAccessible(true);
             throw new RuntimeException("InaccessibleObjectException not thrown");
-        } catch (InaccessibleObjectException e) { e.printStackTrace(); }
+        } catch (InaccessibleObjectException e) {
+            System.out.println("as expected: " + e.toString());
+        }
     }
 
     void trySetAccessible() throws Exception {
-        if (ctor.trySetAccessible()) {
-            throw new RuntimeException("trySetAccessible should not succeed");
-        }
+        ctor.trySetAccessible();
         Field field = c.getField("x");
         if (field.trySetAccessible()) {
             throw new RuntimeException("trySetAccessible should not succeed");
         }
     }

@@ -229,10 +249,20 @@
     void checkMethod(String source, String name, Class<?> returnType, Class<?>... params) throws Exception {
         Method m = c.getDeclaredMethod(name, params);
         assertEquals(m.toString(), source);
     }
 
+    // Check that the class does not have a static method with the name <init>
+    void initFactoryNotMethods() throws Exception {
+        Method[] methods = c.getDeclaredMethods();
+        for (Method m : methods) {
+            if (Modifier.isStatic(m.getModifiers())) {
+                assertFalse(m.getName().equals("<init>"));
+            }
+        }
+    }
+
     static void assertEquals(Object o1, Object o2) {
         if (o1 == o2 || o1.equals(o2))
             return;
 
         throw new AssertionError(o1 + " != " + o2);
< prev index next >