1 /*
 2  * Copyright (c) 2024, 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  * @summary Sanity tests for GetLocalObject/SetLocalObject/GetLocalInstance with value classes.
27  * @requires vm.jvmti
28  * @modules java.base/jdk.internal.vm.annotation
29  * @enablePreview
30  * @run main/othervm/native -agentlib:ValueGetSetLocal ValueGetSetLocal
31  */
32 
33 import java.util.Objects;
34 import jdk.internal.vm.annotation.LooselyConsistentValue;
35 import jdk.internal.vm.annotation.NullRestricted;
36 
37 public class ValueGetSetLocal {
38 
39     @LooselyConsistentValue
40     private static value class ValueClass {
41         public int f1;
42         public int f2;
43 
44         public ValueClass(int v1, int v2) { f1 = v1; f2 = v2; }
45     }
46 
47     private static value class ValueHolder {
48         public ValueClass f1;
49         @NullRestricted
50         public ValueClass f2;
51 
52         public static ValueClass s1 = new ValueClass(0, 1);
53 
54         public ValueHolder(int v) {
55             f1 = new ValueClass(v, v + 100);
56             f2 = new ValueClass(v + 1, v + 200);
57         }
58 
59         // slot 0 is "this"
60         public void meth(ValueClass obj1,       // slot 1
61                          ValueHolder obj2) {    // slot 2
62             Object obj3 = obj2;                 // slot 3
63             // SetLocalObject can only set locals for top frame of virtual threads.
64             boolean testSetLocal = !Thread.currentThread().isVirtual();
65             testLocals(Thread.currentThread(), testSetLocal);
66         }
67     }
68 
69     public static void main(String[] args) throws Exception {
70         ValueClass testObj1 = new ValueClass(7, 8);
71         ValueHolder testObj2 = new ValueHolder(9);
72         testObj2.meth(testObj1, testObj2);
73     }
74 
75     private static native void testLocals(Thread thread, boolean testSetLocal);
76 }