1 /*
  2  * Copyright (c) 2019, 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 import jdk.internal.vm.annotation.ImplicitlyConstructible;
 26 import jdk.internal.vm.annotation.LooselyConsistentValue;
 27 import jdk.internal.vm.annotation.NullRestricted;
 28 
 29 public class HelloInlineClassApp {
 30 
 31     @ImplicitlyConstructible
 32     @LooselyConsistentValue
 33     static value class Point {
 34         int x, y;
 35 
 36         public String toString() {
 37             return "(" + x + ", " + y + ")";
 38         }
 39 
 40         public Point(int x, int y) {
 41             this.x = x;
 42             this.y = y;
 43         }
 44 
 45         Point add(Point p1) {
 46             return new Point(x + p1.x, y + p1.y);
 47         }
 48 
 49         Point add(Point p1, Point p2) {
 50             return new Point(x + p1.x + p2.x, y + p1.y + p2.y);
 51         }
 52 
 53         Point add(Point p1, int x2, int y2, Point p3) {
 54             return new Point(x + p1.x + x2 + p3.x, y + p1.y + y2 + p3.y);
 55         }
 56     }
 57 
 58     @ImplicitlyConstructible
 59     @LooselyConsistentValue
 60     static value class Rectangle {
 61         @NullRestricted
 62         Point p0 = new Point(0,0);
 63         @NullRestricted
 64         Point p1 = new Point(1,1);
 65     }
 66 
 67     @NullRestricted
 68     Point point;
 69 
 70     @NullRestricted
 71     static Rectangle 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         if (HelloInlineClassApp.rectangle.p0 != pzero || HelloInlineClassApp.rectangle.p1 != pzero) {
114             throw new RuntimeException("Static field rectangle not as expected");
115         }
116 
117         HelloInlineClassApp app = new HelloInlineClassApp();
118         if (app.point != pzero) {
119             throw new RuntimeException("Non-static field point not as expected");
120         }
121 
122         ValueRecord valueRec = new ValueRecord(30, "thirty");
123         if (!valueRec.toString().equals("ValueRecord[i=30, name=thirty]")) {
124             throw new RuntimeException("ValueRecord toString unexpected value: " + valueRec.toString());
125         }
126     }
127 }