/* * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test * @run main/othervm -Xint -Xverify:none -XX:+EnableMVT -XX:+ValueArrayFlatten MVTAccessCheck * @run main/othervm -Xint -Xverify:none -XX:+EnableMVT -XX:-ValueArrayFlatten MVTAccessCheck * @run main/othervm -Xint -Xverify:none -XX:+EnableMVT -Dvalhalla.enableValueLambdaForms=true MVTAccessCheck * @run main/othervm -Xint -Xverify:none -XX:+EnableMVT -Dvalhalla.enableValueLambdaForms=true -Dvalhalla.enablePoolPatches=true MVTAccessCheck */ import jdk.experimental.value.ValueType; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; public class MVTAccessCheck { public static void main(String[] args) throws Throwable { MethodHandles.Lookup lookup = MethodHandles.lookup(); Class VCC = PrivatePoint.class; ValueType VT = ValueType.forClass(VCC); String[] fieldNames = {"x", "y", "z"}; Class[] fieldTypes = {int.class, short.class, short.class}; for (int i = 0; i < fieldNames.length; i++) { final int offset = i; assertThrow(() -> VT.findGetter(lookup, fieldNames[offset], fieldTypes[offset]),IllegalAccessException.class); } for (int i = 0; i < fieldNames.length; i++) { final int offset = i; assertThrow(() -> VT.findWither(lookup, fieldNames[offset], fieldTypes[offset]), IllegalAccessException.class); } PrivatePoint[] pts = {new PrivatePoint(1, (short) 6, (short) 3), new PrivatePoint(1, (short) 2, (short) 3)}; MethodHandle substTest = VT.substitutabilityTest(); for (PrivatePoint p1 : pts) { for (PrivatePoint p2 : pts) { boolean b1 = (boolean) substTest.invoke(p1, p2); boolean b2 = p1.equals(p2); assertTrue(b1 == b2); } } MethodHandle hash = VT.substitutabilityHashCode(); for (PrivatePoint p1 : pts) { for (PrivatePoint p2 : pts) { boolean vHashEq = (int) hash.invoke(p1) == (int) hash.invoke(p2); boolean rHashEq = p1.hashCode() == p2.hashCode(); assertTrue(vHashEq == rHashEq); } } } static int assertCount = 0; interface MHAction { void run() throws Throwable; } static void assertThrow(MHAction action, Class ex) { try { action.run(); assertTrue(false); } catch (Throwable t) { assertTrue(ex.isAssignableFrom(t.getClass())); } } static void assertTrue(boolean cond) { assertCount++; if (!cond) { throw new AssertionError(); } } }