1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  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  * @test
  26  * @summary basic test for reflection on MVT
  27  * @run testng/othervm -XX:+EnableMVT MVTReflectionTest
  28  */
  29 
  30 import jdk.incubator.mvt.ValueType;
  31 
  32 import org.testng.annotations.Test;
  33 import static org.testng.Assert.*;
  34 
  35 @Test
  36 public class MVTReflectionTest {
  37     private static Class<?> VCC = Point.class;
  38     private static ValueType<?> VT = ValueType.forClass(VCC);
  39     private static Class<?> DVT = VT.valueClass();
  40 
  41     public void testValueCapableClass() throws Exception {
  42         Class<?> pointClass = Class.forName("Point");
  43         assertEquals(pointClass, VCC);
  44 
  45         // test getSuperClass
  46         assertEquals(pointClass.getSuperclass(), Object.class);
  47 
  48         // test getFields and getDeclaredFields
  49         assertTrue(pointClass.getFields().length == 3);
  50         assertTrue(pointClass.getDeclaredFields().length == 3);
  51     }
  52 
  53     public void testDerivedValueType() throws Exception {
  54         // test Class.forName
  55         try {
  56             Class<?> c = Class.forName(DVT.getName());
  57             throw new RuntimeException("should fail to load " + c);
  58         } catch (ClassNotFoundException e) {}
  59 
  60         Module m = Point.class.getClassLoader().getUnnamedModule();
  61         Class<?> c = Class.forName(m, DVT.getName());
  62         assertEquals(c, null);
  63 
  64         // test getSuperClass
  65         assertEquals(DVT.getSuperclass(), __Value.class);
  66 
  67         // test getFields and getDeclaredFields
  68         try {
  69             DVT.getFields();
  70             throw new RuntimeException("should fail to getFields on " + DVT);
  71         } catch (UnsupportedOperationException e) {}
  72 
  73         try {
  74             DVT.newInstance();
  75             throw new RuntimeException("should fail to newInstance on " + DVT);
  76         } catch (UnsupportedOperationException e) {}
  77     }
  78 
  79     public void testValueTypeArray() throws Exception {
  80         Point[] points = new Point[0];
  81 
  82         Class<?> c1 = Class.forName("[LPoint;");
  83         Class<?> c2 = VT.arrayValueClass();
  84         assertEquals(c1, points.getClass());
  85 
  86         String name = "[Q" + DVT.getName() + ";";
  87         assertEquals(name, c2.getName());
  88 
  89         // cannot load an array DVT class
  90         try {
  91             Class.forName(name);
  92             throw new RuntimeException("should fail to load " + name);
  93         } catch (ClassNotFoundException e) {}
  94     }
  95 }