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:+UseFieldFlattening
 31  *                   runtime.valhalla.inlinetypes.TestFieldNullability
 32  */
 33 
 34 package runtime.valhalla.inlinetypes;
 35 
 36 import jdk.internal.vm.annotation.LooselyConsistentValue;
 37 import jdk.internal.vm.annotation.NullRestricted;
 38 import jdk.internal.vm.annotation.Strict;
 39 import jdk.test.lib.Asserts;
 40 
 41 
 42 public class TestFieldNullability {
 43     @LooselyConsistentValue
 44     static value class MyValue {
 45         int x;
 46 
 47         public MyValue() {
 48             x = 314;
 49         }
 50     }
 51 
 52     @LooselyConsistentValue
 53     static value class MyBigValue {
 54         long l0, l1, l2, l3, l4, l5, l6, l7, l8, l9;
 55         long l10, l11, l12, l13, l14, l15, l16, l17, l18, l19;
 56 
 57         public MyBigValue() {
 58             l0 = l1 = l2 = l3 = l4 = l5 = l6 = l7 = l8 = l9 = 271;
 59             l10 = l11 = l12 = l13 = l14 = l15 = l16 = l17 = l18 = l19 = 271;
 60         }
 61     }
 62 
 63     static class TestIdentityClass {
 64         MyValue nullableField;
 65         @Strict
 66         @NullRestricted
 67         MyValue nullfreeField = new MyValue();       // flattened
 68         MyValue nullField;
 69         @Strict
 70         @NullRestricted
 71         MyBigValue nullfreeBigField = new MyBigValue(); // 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 }