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  * @run main/othervm -Xint -Xverify:none -XX:+EnableMVT -XX:+ValueArrayFlatten MVTAccessCheck
  27  * @run main/othervm -Xint -Xverify:none -XX:+EnableMVT -XX:-ValueArrayFlatten MVTAccessCheck
  28  * @run main/othervm -Xint -Xverify:none -XX:+EnableMVT -Dvalhalla.enableValueLambdaForms=true MVTAccessCheck
  29  * @run main/othervm -Xint -Xverify:none -XX:+EnableMVT -Dvalhalla.enableValueLambdaForms=true -Dvalhalla.enablePoolPatches=true MVTAccessCheck
  30  */
  31 
  32 import jdk.experimental.value.ValueType;
  33 
  34 import java.lang.invoke.MethodHandle;
  35 import java.lang.invoke.MethodHandles;
  36 
  37 public class MVTAccessCheck {
  38 
  39     public static void main(String[] args) throws Throwable {
  40         MethodHandles.Lookup lookup = MethodHandles.lookup();
  41         Class<?> VCC = PrivatePoint.class;
  42         ValueType<?> VT = ValueType.forClass(VCC);
  43         String[] fieldNames = {"x", "y", "z"};
  44         Class<?>[] fieldTypes = {int.class, short.class, short.class};
  45 
  46         for (int i = 0; i < fieldNames.length; i++) {
  47             final int offset = i;
  48             assertThrow(() -> VT.findGetter(lookup, fieldNames[offset], fieldTypes[offset]),IllegalAccessException.class);
  49         }
  50 
  51         for (int i = 0; i < fieldNames.length; i++) {
  52             final int offset = i;
  53             assertThrow(() -> VT.findWither(lookup, fieldNames[offset], fieldTypes[offset]), IllegalAccessException.class);
  54         }
  55 
  56         PrivatePoint[] pts = {new PrivatePoint(1, (short) 6, (short) 3), new PrivatePoint(1, (short) 2, (short) 3)};
  57 
  58         MethodHandle substTest = VT.substitutabilityTest();
  59         for (PrivatePoint p1 : pts) {
  60             for (PrivatePoint p2 : pts) {
  61                 boolean b1 = (boolean) substTest.invoke(p1, p2);
  62                 boolean b2 = p1.equals(p2);
  63                 assertTrue(b1 == b2);
  64             }
  65         }
  66 
  67         MethodHandle hash = VT.substitutabilityHashCode();
  68         for (PrivatePoint p1 : pts) {
  69             for (PrivatePoint p2 : pts) {
  70                 boolean vHashEq = (int) hash.invoke(p1) == (int) hash.invoke(p2);
  71                 boolean rHashEq = p1.hashCode() == p2.hashCode();
  72                 assertTrue(vHashEq == rHashEq);
  73             }
  74         }
  75     }
  76 
  77     static int assertCount = 0;
  78 
  79     interface MHAction {
  80         void run() throws Throwable;
  81     }
  82 
  83     static void assertThrow(MHAction action, Class<? extends Throwable> ex) {
  84         try {
  85             action.run();
  86             assertTrue(false);
  87         } catch (Throwable t) {
  88             assertTrue(ex.isAssignableFrom(t.getClass()));
  89         }
  90     }
  91 
  92     static void assertTrue(boolean cond) {
  93         assertCount++;
  94         if (!cond) {
  95             throw new AssertionError();
  96         }
  97     }
  98 }