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