1 /*
 2  * Copyright (c) 2025, 2026, 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 8348743
27  * @summary Tests ValueGetObjectHashCodeTest functionality for value objects.
28  * @requires vm.jvmti
29  * @modules java.base/jdk.internal.vm.annotation
30  * @enablePreview
31  * @run main/othervm/native -agentlib:ValueGetObjectHashCodeTest
32  *                          -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlineLayout
33  *                          ValueGetObjectHashCodeTest
34  */
35 
36 import jdk.internal.vm.annotation.NullRestricted;
37 
38 public class ValueGetObjectHashCodeTest {
39 
40     private static value class ValueClass {
41         public int f1;
42         public ValueClass(int v1) { f1 = v1; }
43         public String toString() {
44             return "value(" + String.valueOf(f1) + ")";
45         }
46     }
47 
48     private static value class ValueHolder {
49         public ValueClass f1;
50         @NullRestricted
51         public ValueClass f2;
52 
53         public ValueHolder(int v1, int v2) {
54             f1 = new ValueClass(v1);
55             f2 = new ValueClass(v2);
56         }
57         public String toString() {
58             return "holder{" + f1 + ", " + f2 + "}";
59         }
60     }
61 
62     private static native int getHash0(Object object);
63 
64     private static int getHash(Object object) {
65         int hash = getHash0(object);
66         System.out.println("hash (" + object + "): " + hash);
67         return hash;
68     }
69 
70     public static void main(String[] args) {
71         System.loadLibrary("ValueGetObjectHashCodeTest");
72         ValueClass v1 = new ValueClass(8);
73         ValueHolder h1 = new ValueHolder(8, 8);
74 
75         // expect the same hash code for equal value objects
76         int hash1 = getHash(v1);
77         int hash2 = getHash(h1.f1);
78         int hash3 = getHash(h1.f2);
79 
80         if (hash1 != hash2 || hash2 != hash3) {
81             throw new RuntimeException("Hash should be equal");
82         }
83     }
84 }