1 /* 2 * Copyright (c) 2018, 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 * @bug 8209702 27 * @summary Verify that the native clone intrinsic handles value types. 28 * @modules java.base/jdk.experimental.bytecode 29 * java.base/jdk.experimental.value 30 * @library /test/lib 31 * @compile -XDemitQtypes -XDenableValueTypes TestNativeClone.java 32 * @run main/othervm -XX:+EnableValhalla -Xbatch -XX:-UseTypeProfile 33 * -XX:CompileCommand=compileonly,compiler.valhalla.valuetypes.MyValue::* 34 * -XX:CompileCommand=compileonly,compiler.valhalla.valuetypes.TestNativeClone::test* 35 * -XX:CompileCommand=compileonly,jdk.internal.reflect.GeneratedMethodAccessor1::invoke 36 * -XX:CompileCommand=dontinline,jdk.internal.reflect.GeneratedMethodAccessor1::invoke 37 * compiler.valhalla.valuetypes.TestNativeClone 38 */ 39 40 package compiler.valhalla.valuetypes; 41 42 import java.lang.invoke.*; 43 import java.lang.reflect.InvocationTargetException; 44 import java.lang.reflect.Method; 45 import jdk.experimental.value.MethodHandleBuilder; 46 import jdk.test.lib.Asserts; 47 48 value class MyValue { 49 public final int x; 50 51 public MyValue(int x) { 52 this.x = x; 53 } 54 } 55 56 public class TestNativeClone { 57 58 private static final MethodHandle cloneValue = MethodHandleBuilder.loadCode(MethodHandles.lookup(), 59 "MyValue", 60 MethodType.methodType(Object.class, MyValue.class.asValueType()), 61 CODE -> { 62 CODE. 63 aload_0(). 64 invokevirtual(Object.class, "clone", "()Ljava/lang/Object;", false). 65 areturn(); 66 }); 67 68 public static void test1(MyValue vt) throws Throwable { 69 try { 70 cloneValue.invoke(vt); 71 throw new RuntimeException("No exception thrown"); 72 } catch (CloneNotSupportedException e) { 73 // Expected 74 } 75 } 76 77 public static void test2(Method clone, Object obj) { 78 try { 79 clone.invoke(obj); 80 } catch (InvocationTargetException e) { 81 // Expected 82 Asserts.assertTrue(e.getCause() instanceof CloneNotSupportedException, "Unexpected exception thrown"); 83 return; 84 } catch (Exception e) { 85 throw new RuntimeException("Unexpected exception thrown", e); 86 } 87 throw new RuntimeException("No exception thrown"); 88 } 89 90 public static void main(String[] args) throws Throwable { 91 MyValue vt = new MyValue(42); 92 Method clone = Object.class.getDeclaredMethod("clone"); 93 clone.setAccessible(true); 94 for (int i = 0; i < 20_000; ++i) { 95 test1(vt); 96 test2(clone, vt); 97 } 98 } 99 }