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