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.ImplicitlyConstructible;
 26 import jdk.internal.vm.annotation.LooselyConsistentValue;
 27 import jdk.internal.vm.annotation.NullRestricted;
 28 import jdk.test.lib.Asserts;
 29 
 30 /*
 31  * @test InlineTypeCreation
 32  * @summary Inline Type creation test
 33  * @library /test/lib
 34  * @modules java.base/jdk.internal.vm.annotation
 35  * @enablePreview
 36  * @compile InlineTypeCreation.java Point.java Long8Inline.java Person.java
 37  * @run main/othervm runtime.valhalla.inlinetypes.InlineTypeCreation
 38  */
 39 public class InlineTypeCreation {
 40     public static void main(String[] args) {
 41         InlineTypeCreation inlineTypeCreation = new InlineTypeCreation();
 42         inlineTypeCreation.run();
 43     }
 44 
 45     public void run() {
 46         testPoint();
 47         testLong8();
 48         testPerson();
 49         StaticSelf.test();
 50         testUnresolvedAndResolvedNew();
 51     }
 52 
 53     void testPoint() {
 54         Point p = new Point(1, 2);
 55         Asserts.assertEquals(p.x, 1, "invalid point x value");
 56         Asserts.assertEquals(p.y, 2, "invalid point y value");
 57         Point p2 = clonePoint(p);
 58         Asserts.assertEquals(p2.x, 1, "invalid point clone x value");
 59         Asserts.assertEquals(p2.y, 2, "invalid point clone y value");
 60     }
 61 
 62     static Point clonePoint(Point p) {
 63         Point q = p;
 64         return q;
 65     }
 66 
 67     void testLong8() {
 68         Long8Inline long8Inline = new Long8Inline(1, 2, 3, 4, 5, 6, 7, 8);
 69         Asserts.assertEquals(long8Inline.getLongField1(), 1L, "Field 1 incorrect");
 70         Asserts.assertEquals(long8Inline.getLongField8(), 8L, "Field 8 incorrect");
 71         Long8Inline.check(long8Inline, 1, 2, 3, 4, 5, 6, 7, 8);
 72     }
 73 
 74     void testPerson() {
 75         Person person = new Person(1, "John", "Smith");
 76         Asserts.assertEquals(person.getId(), 1, "Id field incorrect");
 77         Asserts.assertEquals(person.getFirstName(), "John", "First name incorrect");
 78         Asserts.assertEquals(person.getLastName(), "Smith", "Last name incorrect");
 79     }
 80 
 81     @ImplicitlyConstructible
 82     @LooselyConsistentValue
 83     static value class StaticSelf {
 84 
 85         static final StaticSelf DEFAULT = new StaticSelf(0);
 86         int f1;
 87 
 88         public StaticSelf(int f1) { this.f1 = f1; }
 89         public String toString() { return "StaticSelf f1=" + f1; }
 90 
 91         public static void test() {
 92             String s = DEFAULT.toString();
 93         }
 94 
 95     }
 96 
 97     static value class MyPoint {
 98          int x,y;
 99          MyPoint(int x, int y) {
100              this.x = x;
101              this.y = y;
102          }
103      }
104 
105     // Two instantiations of the same class to exercise both the unresolved and resolved paths
106     // in bytecode 'new' implementation
107     void testUnresolvedAndResolvedNew(){
108          MyPoint p1 = new MyPoint(10, 20);
109          MyPoint p2 = new MyPoint(20, 20);
110      }
111 }