1 /*
2 * Copyright (c) 2024, 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 * @summary Sanity test for GetClassFields with value classes.
27 * @requires vm.jvmti
28 * @modules java.base/jdk.internal.vm.annotation
29 * @enablePreview
30 * @run main/othervm/native -agentlib:ValueGetClassFields -XX:+EnableValhalla ValueGetClassFields
31 */
32
33 import jdk.internal.vm.annotation.LooselyConsistentValue;
34 import jdk.internal.vm.annotation.NullRestricted;
35
36 public class ValueGetClassFields {
37
38 private static final String agentLib = "ValueGetClassFields";
39
40 @LooselyConsistentValue
41 private static value class ValueClass {
42 public int f1;
43 public int f2;
44
45 public ValueClass(int v1, int v2) { f1 = v1; f2 = v2; }
46 }
47
48 private static value class ValueHolder {
49 public ValueClass f1;
50 @NullRestricted
51 public ValueClass f2;
52
53 public static ValueClass s1 = new ValueClass(0, 1);
54
55 public ValueHolder(int v) {
56 f1 = new ValueClass(v, v + 100);
57 f2 = new ValueClass(v + 1, v + 200);
58 }
59 }
60
61 public static void main(String[] args) throws Exception {
62 try {
63 System.loadLibrary(agentLib);
64 } catch (UnsatisfiedLinkError ex) {
65 System.err.println("Failed to load " + agentLib + " lib");
66 System.err.println("java.library.path: " + System.getProperty("java.library.path"));
67 throw ex;
68 }
69
70 testGetClassFields(ValueClass.class, 2);
71 testGetClassFields(ValueHolder.class, 3);
72 }
73
74 private static void testGetClassFields(Class cls, int fieldNum) throws Exception {
75 String className = cls.getName();
76 // Ensure the class is prepared.
77 cls = Class.forName(className);
78 log(">> Testing GetClassFields for " + className);
79 if (!nTestGetClassFields(cls, fieldNum)) {
80 throw new RuntimeException("ERROR: " + className);
81 }
82 log("<< Testing " + className + " - OK");
83 log("");
84 }
85
86 private static void log(String msg) {
87 System.out.println(msg);
88 }
89
90 private static native boolean nTestGetClassFields(Class cls, int fieldNum);
91 }