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