1 /*
 2  * Copyright (c) 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 TestCloneableValue
26  * @library /test/lib
27  * @enablePreview
28  * @compile TestCloneableValue.java
29  * @run main/othervm runtime.valhalla.inlinetypes.TestCloneableValue
30  */
31 
32  package runtime.valhalla.inlinetypes;
33 
34  import jdk.test.lib.Asserts;
35 
36  import java.util.ArrayList;
37 
38  public class TestCloneableValue  {
39 
40     static value class SimpleValue implements Cloneable {
41       int i;
42       double j;
43 
44       public SimpleValue() {
45         i = 42;
46         j = Math.E;
47       }
48 
49       @Override
50       public Object clone() throws CloneNotSupportedException {
51         return super.clone(); // delegate to Object's method performing a shallow copy
52       }
53      }
54 
55     static value class NotSoSimpleValue implements Cloneable {
56       ArrayList list;
57 
58       public NotSoSimpleValue() {
59         list = new ArrayList<>();
60       }
61 
62       private NotSoSimpleValue(ArrayList l) {
63         list = l;
64       }
65 
66       @Override
67       public Object clone() throws CloneNotSupportedException {
68         return new NotSoSimpleValue((ArrayList)list.clone());
69       }
70     }
71 
72      public static void main(String[] args) {
73       var sv = new SimpleValue();
74       try {
75         var c1 = sv.clone();
76         Asserts.assertEQ(sv, c1);
77         Asserts.assertEQ(sv.hashCode(), c1.hashCode());
78       } catch(CloneNotSupportedException e) {
79         Asserts.fail("Unexpected exception", e);
80       }
81 
82       var nssv = new NotSoSimpleValue();
83       try {
84         var c2 = nssv.clone();
85         Asserts.assertNE(nssv, c2);
86         Asserts.assertEQ(nssv.hashCode(), c2.hashCode());
87       } catch(CloneNotSupportedException e) {
88         Asserts.fail("Unexpected exception", e);
89       }
90      }
91  }