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