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