1 /*
  2  * Copyright (c) 2018, 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  * @test TestFieldNullability
 26  * @library /test/lib
 27  * @modules java.base/jdk.internal.vm.annotation
 28  * @enablePreview
 29  * @compile TestFieldNullability.java
 30  * @run main/othervm -Xmx128m -XX:InlineFieldMaxFlatSize=32
 31  *                   runtime.valhalla.inlinetypes.TestFieldNullability
 32  */
 33 
 34 package runtime.valhalla.inlinetypes;
 35 
 36 import jdk.internal.vm.annotation.ImplicitlyConstructible;
 37 import jdk.internal.vm.annotation.LooselyConsistentValue;
 38 import jdk.internal.vm.annotation.NullRestricted;
 39 import jdk.test.lib.Asserts;
 40 
 41 
 42 public class TestFieldNullability {
 43     @ImplicitlyConstructible
 44     @LooselyConsistentValue
 45     static value class MyValue {
 46         int x;
 47 
 48         public MyValue() {
 49             x = 314;
 50         }
 51     }
 52 
 53     @ImplicitlyConstructible
 54     @LooselyConsistentValue
 55     static value class MyBigValue {
 56         long l0, l1, l2, l3, l4, l5, l6, l7, l8, l9;
 57         long l10, l11, l12, l13, l14, l15, l16, l17, l18, l19;
 58 
 59         public MyBigValue() {
 60             l0 = l1 = l2 = l3 = l4 = l5 = l6 = l7 = l8 = l9 = 271;
 61             l10 = l11 = l12 = l13 = l14 = l15 = l16 = l17 = l18 = l19 = 271;
 62         }
 63     }
 64 
 65     static class TestIdentityClass {
 66         MyValue nullableField;
 67         @NullRestricted
 68         MyValue nullfreeField;       // flattened
 69         MyValue nullField;
 70         @NullRestricted
 71         MyBigValue nullfreeBigField; // not flattened
 72         MyBigValue nullBigField;
 73     }
 74 
 75     public static void main(String[] args) {
 76         TestIdentityClass that = new TestIdentityClass();
 77         Asserts.assertNull(that.nullField, "Invalid non null value for uninitialized non flattenable field");
 78         Asserts.assertNull(that.nullBigField, "Invalid non null value for uninitialized non flattenable field");
 79         boolean NPE = false;
 80         try {
 81             that.nullableField = that.nullField;
 82         } catch(NullPointerException e) {
 83             NPE = true;
 84         }
 85         Asserts.assertFalse(NPE, "Invalid NPE when assigning null to a non flattenable field");
 86         try {
 87             that.nullfreeField = (MyValue) that.nullField;
 88         } catch(NullPointerException e) {
 89             NPE = true;
 90         }
 91         Asserts.assertTrue(NPE, "Missing NPE when assigning null to a flattened field");
 92         try {
 93             that.nullfreeBigField = (MyBigValue) that.nullBigField;
 94         } catch(NullPointerException e) {
 95             NPE = true;
 96         }
 97         Asserts.assertTrue(NPE, "Missing NPE when assigning null to a flattenable field");
 98     }
 99 
100 }