1 /*
2 * Copyright (c) 2019, 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 import jdk.internal.vm.annotation.LooselyConsistentValue;
26 import jdk.internal.vm.annotation.NullRestricted;
27
28 public class HelloInlineClassApp {
29
30 public HelloInlineClassApp() {
31 point = new Point(0, 0);
32 super();
33 }
34
35 @LooselyConsistentValue
36 static value class Point {
37 int x, y;
38
39 public String toString() {
40 return "(" + x + ", " + y + ")";
41 }
42
43 public Point(int x, int y) {
44 this.x = x;
45 this.y = y;
46 }
47
48 Point add(Point p1) {
49 return new Point(x + p1.x, y + p1.y);
50 }
51
52 Point add(Point p1, Point p2) {
53 return new Point(x + p1.x + p2.x, y + p1.y + p2.y);
54 }
55
56 Point add(Point p1, int x2, int y2, Point p3) {
57 return new Point(x + p1.x + x2 + p3.x, y + p1.y + y2 + p3.y);
58 }
59 }
60
61 @LooselyConsistentValue
62 static value class Rectangle {
63 @NullRestricted
64 Point p0 = new Point(0,0);
65 @NullRestricted
66 Point p1 = new Point(1,1);
67 }
68
69 @NullRestricted
70 Point point;
71
72 @NullRestricted
73 static Rectangle rectangle = new Rectangle();
74
75 static value record ValueRecord(int i, String name) {}
76
77 public static void main(String[] args) throws Exception {
78 Point p = new Point(0, 123);
79 System.out.println("Point = " + p);
80 String req = "(0, 123)";
81 if (!p.toString().equals(req)) {
82 throw new RuntimeException("Expected " + req + " but got " + p);
83 }
84
85 Point p1 = new Point(1, 1);
86 Point p2 = new Point(2, 2);
87 Point p3 = new Point(3, 3);
88 int x2 = 200;
89 int y2 = 200;
90
91 int loops = 100000;
92 for (int i=0; i<loops; i++) {
93 p = p.add(p1);
94 p = p.add(p1, p2);
95 p = p.add(p1, x2, y2, p3);
96 }
97
98 int expectedX = 0 +
99 loops * p1.x +
100 loops * (p1.x + p2.x) +
101 loops * (p1.x + x2 + p3.x);
102
103 int expectedY = 123 +
104 loops * p1.y +
105 loops * (p1.y + p2.y) +
106 loops * (p1.y + y2 + p3.y);
107
108 System.out.println("Point (2) = " + p);
109
110 if (p.x != expectedX || p.y != expectedY) {
111 throw new RuntimeException("Expected (" + expectedX + ", " + expectedY + " but got " + p);
112 }
113
114 Point pzero = new Point(0,0);
115 Point pone = new Point(1, 1);
116 if (HelloInlineClassApp.rectangle.p0 != pzero || HelloInlineClassApp.rectangle.p1 != pone) {
117 throw new RuntimeException("Static field rectangle not as expected");
118 }
119
120 HelloInlineClassApp app = new HelloInlineClassApp();
121 if (app.point != pzero) {
122 throw new RuntimeException("Non-static field point not as expected");
123 }
124
125 ValueRecord valueRec = new ValueRecord(30, "thirty");
126 if (!valueRec.toString().equals("ValueRecord[i=30, name=thirty]")) {
127 throw new RuntimeException("ValueRecord toString unexpected value: " + valueRec.toString());
128 }
129 }
130 }