< prev index next >

test/jdk/valhalla/valuetypes/Reflection.java

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


  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 /*
  26  * @test
  27  * @summary test reflection on inline types
  28  * @compile -XDallowWithFieldOperator Point.java Line.java NonFlattenValue.java
  29  * @run main/othervm -XX:+EnableValhalla Reflection
  30  */
  31 
  32 import java.lang.reflect.*;
  33 import java.util.Arrays;
  34 import java.util.stream.Collectors;


  35 
  36 public class Reflection {
  37     public static void main(String... args) throws Exception {
  38         testPointClass();
  39         testLineClass();
  40         testNonFlattenValue();
  41         testMirrors();
  42         testClassName();
  43     }
  44 
  45     static void testPointClass() throws Exception  {
  46         Point o = Point.makePoint(10, 20);
  47         Reflection test = new Reflection(Point.class, "Point", o);
  48         test.newInstance();
  49         test.constructor();
  50         test.accessFieldX(o.x);
  51         test.setAccessible();
  52         test.trySetAccessible();
  53         test.staticField();
  54     }
  55 
  56     static void testLineClass() throws Exception {
  57         Line l = Line.makeLine(10, 20, 30, 40);
  58         Reflection test = new Reflection(Line.class, "Line", l);
  59         test.checkField("public final Point Line.p1", "p1", Point.class);
  60         test.checkField("public final Point Line.p2", "p2", Point.class);
  61         test.checkMethod("public Point Line.p1()",           "p1", Point.class);
  62         test.checkMethod("public Point Line.p2()",           "p2", Point.class);
  63     }
  64 
  65     static void testNonFlattenValue() throws Exception {
  66         NonFlattenValue nfv = NonFlattenValue.make(10, 20);
  67         Reflection test = new Reflection(NonFlattenValue.class, "NonFlattenValue", nfv);
  68         test.checkField("final Point? NonFlattenValue.nfp", "nfp", Point.class.asNullableType());
  69         test.checkMethod("public Point NonFlattenValue.pointValue()", "pointValue", Point.class);
  70         test.checkMethod("public Point? NonFlattenValue.point()", "point", Point.class.asNullableType());
  71         test.checkMethod("public boolean NonFlattenValue.has(Point,Point?)", "has", boolean.class, Point.class, Point.class.asNullableType());
  72     }


 156         assertEquals(arrayType.asNullableType(), arrayType);
 157         if (array[0] == null) {
 158             System.out.println("array[0] = null");
 159         } else {
 160             System.out.println("array[0] = " + array[0]);
 161         }
 162     }
 163 
 164     void accessFieldX(int x) throws Exception {
 165         Field field = c.getField("x");
 166         if (field.getInt(o) != x) {
 167             throw new RuntimeException("Unexpected Point.x value: " +  field.getInt(o));
 168         }
 169 
 170         try {
 171             field.setInt(o, 100);
 172             throw new RuntimeException("IllegalAccessException not thrown");
 173         } catch (IllegalAccessException e) {}
 174     }
 175 

 176     void newInstance() throws Exception {
 177         try {
 178             Object o = c.newInstance();
 179             throw new RuntimeException("newInstance expected to be unsupported on inline class");
 180         } catch (IllegalAccessException e) {}
 181     }
 182 
 183     void constructor() throws Exception {
 184         try {
 185             ctor.newInstance();
 186             throw new RuntimeException("IllegalAccessException not thrown");
 187         } catch (IllegalAccessException e) { }
 188     }
 189 
 190     void setAccessible() throws Exception {
 191         try {
 192             ctor.setAccessible(true);
 193             throw new RuntimeException("InaccessibleObjectException not thrown");
 194         } catch (InaccessibleObjectException e) { e.printStackTrace(); }
 195         Field field = c.getField("x");
 196         try {
 197             field.setAccessible(true);
 198             throw new RuntimeException("InaccessibleObjectException not thrown");
 199         } catch (InaccessibleObjectException e) { e.printStackTrace(); }
 200     }
 201 
 202     void trySetAccessible() throws Exception {
 203         if (ctor.trySetAccessible()) {
 204             throw new RuntimeException("trySetAccessible should not succeed");
 205         }
 206         Field field = c.getField("x");
 207         if (field.trySetAccessible()) {
 208             throw new RuntimeException("trySetAccessible should not succeed");
 209         }
 210     }
 211 
 212     void staticField() throws Exception {
 213         Field f = c.getDeclaredField("STATIC_FIELD");
 214         if (f.trySetAccessible()) {
 215             throw new RuntimeException("trySetAccessible should not succeed");
 216         }
 217         try {
 218             f.setAccessible(true);
 219             throw new RuntimeException("IllegalAccessException not thrown");
 220         } catch (InaccessibleObjectException e) { }
 221     }
 222 
 223     void checkField(String source, String name, Class<?> type) throws Exception {
 224         Field f = c.getDeclaredField(name);
 225         assertEquals(f.getType(), type);
 226         assertEquals(f.toString(), source);
 227     }
 228 
 229     void checkMethod(String source, String name, Class<?> returnType, Class<?>... params) throws Exception {




  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 /*
  26  * @test
  27  * @summary test reflection on inline types
  28  * @compile -XDallowWithFieldOperator Point.java Line.java NonFlattenValue.java
  29  * @run main/othervm -XX:+EnableValhalla Reflection
  30  */
  31 
  32 import java.lang.reflect.Array;
  33 import java.lang.reflect.Constructor;
  34 import java.lang.reflect.Field;
  35 import java.lang.reflect.InaccessibleObjectException;
  36 import java.lang.reflect.Method;
  37 
  38 public class Reflection {
  39     public static void main(String... args) throws Exception {
  40         testPointClass();
  41         testLineClass();
  42         testNonFlattenValue();
  43         testMirrors();
  44         testClassName();
  45     }
  46 
  47     static void testPointClass() throws Exception  {
  48         Point o = Point.makePoint(10, 20);
  49         Reflection test = new Reflection(Point.class, "Point", o);
  50         test.newInstance();
  51         test.constructor();
  52         test.accessFieldX(o.x);


  53         test.staticField();
  54     }
  55 
  56     static void testLineClass() throws Exception {
  57         Line l = Line.makeLine(10, 20, 30, 40);
  58         Reflection test = new Reflection(Line.class, "Line", l);
  59         test.checkField("public final Point Line.p1", "p1", Point.class);
  60         test.checkField("public final Point Line.p2", "p2", Point.class);
  61         test.checkMethod("public Point Line.p1()",           "p1", Point.class);
  62         test.checkMethod("public Point Line.p2()",           "p2", Point.class);
  63     }
  64 
  65     static void testNonFlattenValue() throws Exception {
  66         NonFlattenValue nfv = NonFlattenValue.make(10, 20);
  67         Reflection test = new Reflection(NonFlattenValue.class, "NonFlattenValue", nfv);
  68         test.checkField("final Point? NonFlattenValue.nfp", "nfp", Point.class.asNullableType());
  69         test.checkMethod("public Point NonFlattenValue.pointValue()", "pointValue", Point.class);
  70         test.checkMethod("public Point? NonFlattenValue.point()", "point", Point.class.asNullableType());
  71         test.checkMethod("public boolean NonFlattenValue.has(Point,Point?)", "has", boolean.class, Point.class, Point.class.asNullableType());
  72     }


 156         assertEquals(arrayType.asNullableType(), arrayType);
 157         if (array[0] == null) {
 158             System.out.println("array[0] = null");
 159         } else {
 160             System.out.println("array[0] = " + array[0]);
 161         }
 162     }
 163 
 164     void accessFieldX(int x) throws Exception {
 165         Field field = c.getField("x");
 166         if (field.getInt(o) != x) {
 167             throw new RuntimeException("Unexpected Point.x value: " +  field.getInt(o));
 168         }
 169 
 170         try {
 171             field.setInt(o, 100);
 172             throw new RuntimeException("IllegalAccessException not thrown");
 173         } catch (IllegalAccessException e) {}
 174     }
 175 
 176     @SuppressWarnings("deprecation")
 177     void newInstance() throws Exception {

 178         Object o = c.newInstance();
 179         assertEquals(o.getClass(), c);

 180     }
 181 
 182     void constructor() throws Exception {
 183         Object o = ctor.newInstance();
 184         assertEquals(o.getClass(), c);
























 185     }
 186 
 187     void staticField() throws Exception {
 188         Field f = c.getDeclaredField("STATIC_FIELD");
 189         if (f.trySetAccessible()) {
 190             throw new RuntimeException("trySetAccessible should not succeed");
 191         }
 192         try {
 193             f.setAccessible(true);
 194             throw new RuntimeException("IllegalAccessException not thrown");
 195         } catch (InaccessibleObjectException e) { }
 196     }
 197 
 198     void checkField(String source, String name, Class<?> type) throws Exception {
 199         Field f = c.getDeclaredField(name);
 200         assertEquals(f.getType(), type);
 201         assertEquals(f.toString(), source);
 202     }
 203 
 204     void checkMethod(String source, String name, Class<?> returnType, Class<?>... params) throws Exception {


< prev index next >