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