1 /*
  2  * Copyright (c) 2018, 2021, 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  * @build org.openjdk.asmtools.* org.openjdk.asmtools.jasm.*
 28  * @run driver org.openjdk.asmtools.JtregDriver jasm -strict TestFieldNullabilityClasses.jasm
 29  * @run main/othervm -Xmx128m -XX:InlineFieldMaxFlatSize=32
 30  *                   runtime.valhalla.inlinetypes.TestFieldNullability
 31  */
 32 
 33 package runtime.valhalla.inlinetypes;
 34 
 35 import jdk.test.lib.Asserts;
 36 
 37 public class TestFieldNullability {
 38     static primitive class MyValue {
 39         int x;
 40 
 41         public MyValue() {
 42             x = 314;
 43         }
 44     }
 45 
 46     static primitive class MyBigValue {
 47         long l0, l1, l2, l3, l4, l5, l6, l7, l8, l9;
 48         long l10, l11, l12, l13, l14, l15, l16, l17, l18, l19;
 49 
 50         public MyBigValue() {
 51             l0 = l1 = l2 = l3 = l4 = l5 = l6 = l7 = l8 = l9 = 271;
 52             l10 = l11 = l12 = l13 = l14 = l15 = l16 = l17 = l18 = l19 = 271;
 53         }
 54     }
 55 
 56     static class TestIdentityClass {
 57         MyValue.ref nullableField;
 58         MyValue nullfreeField;       // flattened
 59         MyValue.ref nullField;
 60         MyBigValue nullfreeBigField; // not flattened
 61         MyBigValue.ref nullBigField;
 62     }
 63 
 64     static void testPrimitiveClass() {
 65         TestPrimitiveClass that = TestPrimitiveClass.default;
 66         Asserts.assertNull(that.nullField, "Invalid non null value for uninitialized non flattenable field");
 67         Asserts.assertNull(that.nullBigField, "Invalid non null value for uninitialized non flattenable field");
 68         boolean NPE = false;
 69         try {
 70             TestPrimitiveClass tv = that.withNullableField(that.nullField);
 71         } catch(NullPointerException e) {
 72             NPE = true;
 73         }
 74         Asserts.assertFalse(NPE, "Invalid NPE when assigning null to a non flattenable field");
 75         try {
 76             TestPrimitiveClass tv = that.withNullfreeField((MyValue) that.nullField);
 77         } catch(NullPointerException e) {
 78             NPE = true;
 79         }
 80         Asserts.assertTrue(NPE, "Missing NPE when assigning null to a flattened field");
 81         try {
 82             TestPrimitiveClass tv = that.withNullfreeBigField((MyBigValue) that.nullBigField);
 83         } catch(NullPointerException e) {
 84             NPE = true;
 85         }
 86         Asserts.assertTrue(NPE, "Missing NPE when assigning null to a flattenable field");
 87     }
 88 
 89     static void testIdentityClass() {
 90         TestIdentityClass that = new TestIdentityClass();
 91         Asserts.assertNull(that.nullField, "Invalid non null value for uninitialized non flattenable field");
 92         Asserts.assertNull(that.nullBigField, "Invalid non null value for uninitialized non flattenable field");
 93         boolean NPE = false;
 94         try {
 95             that.nullableField = that.nullField;
 96         } catch(NullPointerException e) {
 97             NPE = true;
 98         }
 99         Asserts.assertFalse(NPE, "Invalid NPE when assigning null to a non flattenable field");
100         try {
101             that.nullfreeField = (MyValue) that.nullField;
102         } catch(NullPointerException e) {
103             NPE = true;
104         }
105         Asserts.assertTrue(NPE, "Missing NPE when assigning null to a flattened field");
106         try {
107             that.nullfreeBigField = (MyBigValue) that.nullBigField;
108         } catch(NullPointerException e) {
109             NPE = true;
110         }
111         Asserts.assertTrue(NPE, "Missing NPE when assigning null to a flattenable field");
112     }
113 
114     public static void main(String[] args) {
115         testIdentityClass();
116         testPrimitiveClass();
117     }
118 
119 }