1 /*
 2  * Copyright (c) 2017, 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 package runtime.valhalla.inlinetypes;
24 
25 import jdk.internal.vm.annotation.NullRestricted;
26 import jdk.test.lib.Asserts;
27 
28 /*
29  * @test InlineTypeGetField
30  * @summary Inline Type get field test
31  * @library /test/lib
32  * @modules java.base/jdk.internal.vm.annotation
33  * @enablePreview
34  * @compile Point.java InlineTypeGetField.java
35  * @run main/othervm runtime.valhalla.inlinetypes.InlineTypeGetField
36  */
37 public class InlineTypeGetField {
38 
39     @NullRestricted
40     static Point staticPoint0;
41     @NullRestricted
42     static Point staticPoint1;
43     @NullRestricted
44     Point instancePoint0;
45     @NullRestricted
46     Point instancePoint1;
47 
48     static {
49         staticPoint0 = new Point(358, 406);
50         staticPoint1 = new Point(101, 2653);
51     }
52 
53     InlineTypeGetField() {
54         instancePoint0 = new Point(1890, 1918);
55         instancePoint1 = new Point(91, 102);
56     }
57 
58     public static void main(String[] args) {
59         InlineTypeGetField inlineTypeGetField = new InlineTypeGetField();
60         System.gc(); // check that VTs survive GC
61         inlineTypeGetField.run();
62     }
63 
64     public void run() {
65         // testing initial configuration
66         checkPoint(staticPoint0, 358, 406);
67         checkPoint(staticPoint1, 101, 2653);
68         checkPoint(instancePoint0, 1890, 1918);
69         checkPoint(instancePoint1, 91, 102);
70         // swapping static fields
71         Point p = staticPoint1;
72         staticPoint1 = staticPoint0;
73         staticPoint0 = p;
74         System.gc();
75         checkPoint(staticPoint0, 101, 2653);
76         checkPoint(staticPoint1, 358, 406);
77         //swapping instance fields
78         p = instancePoint1;
79         instancePoint1 = instancePoint0;
80         instancePoint0 = p;
81         System.gc();
82         checkPoint(instancePoint0, 91, 102);
83         checkPoint(instancePoint1, 1890, 1918);
84         // instance to static
85         staticPoint0 = instancePoint0;
86         System.gc();
87         checkPoint(staticPoint0, 91, 102);
88         // static to instance
89         instancePoint1 = staticPoint1;
90         System.gc();
91         checkPoint(instancePoint1, 358, 406);
92     }
93 
94     static void checkPoint(Point p , int x, int y) {
95         Asserts.assertEquals(p.x, x, "invalid x value");
96         Asserts.assertEquals(p.y, y, "invalid y value");
97     }
98 }