1 /*
 2  * Copyright (c) 2019, 2024, 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 objects.
28  * @library /test/lib
29  * @modules java.base/java.lang:+open
30  * @enablePreview
31  * @run main/othervm -Xbatch -XX:-UseTypeProfile
32  *                   -XX:CompileCommand=compileonly,compiler.valhalla.inlinetypes.MyValue::*
33  *                   -XX:CompileCommand=compileonly,compiler.valhalla.inlinetypes.TestNativeClone::test*
34  *                   -XX:CompileCommand=compileonly,jdk.internal.reflect.GeneratedMethodAccessor1::invoke
35  *                   -XX:CompileCommand=dontinline,jdk.internal.reflect.GeneratedMethodAccessor1::invoke
36  *                   compiler.valhalla.inlinetypes.TestNativeClone
37  */
38 
39 package compiler.valhalla.inlinetypes;
40 
41 import java.lang.invoke.*;
42 import java.lang.reflect.InvocationTargetException;
43 import java.lang.reflect.Method;
44 import jdk.test.lib.Asserts;
45 
46 value class MyValue {
47     public int x;
48 
49     public MyValue(int x) {
50         this.x = x;
51     }
52 }
53 
54 public class TestNativeClone {
55 
56     public static void test1(Method clone, Object obj) {
57         try {
58             clone.invoke(obj);
59         } catch (InvocationTargetException e) {
60             // Expected
61             Asserts.assertTrue(e.getCause() instanceof CloneNotSupportedException, "Unexpected exception thrown");
62             return;
63         } catch (Exception e) {
64             throw new RuntimeException("Unexpected exception thrown", e);
65         }
66         throw new RuntimeException("No exception thrown");
67     }
68 
69     public static void main(String[] args) throws Throwable {
70         MyValue vt = new MyValue(42);
71         Method clone = Object.class.getDeclaredMethod("clone");
72         clone.setAccessible(true);
73         for (int i = 0; i < 20_000; ++i) {
74             test1(clone, vt);
75         }
76     }
77 }