1 /*
   2  * Copyright (c) 2018, 2019, 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.valuetypes;
  24 
  25 import java.lang.invoke.*;
  26 
  27 import jdk.experimental.value.MethodHandleBuilder;
  28 
  29 import jdk.test.lib.Asserts;
  30 
  31 /*
  32  * @test
  33  * @summary Flattenable field semantic test
  34  * @modules java.base/jdk.experimental.bytecode
  35  *          java.base/jdk.experimental.value
  36  * @library /test/lib
  37  * @compile -XDemitQtypes -XDenableValueTypes -XDallowWithFieldOperator Point.java JumboValue.java
  38  * @compile -XDemitQtypes -XDenableValueTypes -XDallowWithFieldOperator FlattenableSemanticTest.java
  39  * @run main/othervm -Xint -XX:ValueFieldMaxFlatSize=64 runtime.valhalla.valuetypes.FlattenableSemanticTest
  40  * @run main/othervm -Xcomp -XX:ValueFieldMaxFlatSize=64 runtime.valhalla.valuetypes.FlattenableSemanticTest
  41  * // debug: -XX:+PrintValueLayout -XX:-ShowMessageBoxOnError
  42  */
  43 public class FlattenableSemanticTest {
  44 
  45     static Point? nfsp;
  46     static Point fsp;
  47 
  48     Point? nfip;
  49     Point fip;
  50 
  51     static JumboValue? nfsj;
  52     static JumboValue fsj;
  53 
  54     JumboValue? nfij;
  55     JumboValue fij;
  56 
  57     static Object getNull() {
  58         return null;
  59     }
  60 
  61     FlattenableSemanticTest() { }
  62 
  63     public static void main(String[] args) {
  64         FlattenableSemanticTest test = new FlattenableSemanticTest();
  65 
  66         // Uninitialized inline fields must be null for non flattenable fields
  67         Asserts.assertNull(nfsp, "Invalid non null value for unitialized non flattenable field");
  68         Asserts.assertNull(nfsj, "Invalid non null value for unitialized non flattenable field");
  69         Asserts.assertNull(test.nfip, "Invalid non null value for unitialized non flattenable field");
  70         Asserts.assertNull(test.nfij, "Invalid non null value for unitialized non flattenable field");
  71 
  72         // fsp.equals(null);
  73 
  74         // Uninitialized inline fields must be non null for flattenable fields
  75         Asserts.assertNotNull(fsp, "Invalid null value for unitialized flattenable field");
  76         Asserts.assertNotNull(fsj, "Invalid null value for unitialized flattenable field");
  77         Asserts.assertNotNull(test.fip, "Invalid null value for unitialized flattenable field");
  78         Asserts.assertNotNull(test.fij, "Invalid null value for unitialized flattenable field");
  79 
  80         // Assigning null must be allowed for non flattenable inline fields
  81         boolean exception = true;
  82         try {
  83             nfsp = (Point?)getNull();
  84             nfsp = null;
  85             exception = false;
  86         } catch (NullPointerException e) {
  87             exception = true;
  88         }
  89         Asserts.assertFalse(exception, "Invalid NPE when assigning null to a non flattenable field");
  90 
  91         try {
  92             nfsj = (JumboValue?)getNull();
  93             nfsj = null;
  94             exception = false;
  95         } catch (NullPointerException e) {
  96             exception = true;
  97         }
  98         Asserts.assertFalse(exception, "Invalid NPE when assigning null to a non flattenable field");
  99 
 100         try {
 101             test.nfip = (Point?)getNull();
 102             test.nfip = null;
 103             exception = false;
 104         } catch (NullPointerException e) {
 105             exception = true;
 106         }
 107         Asserts.assertFalse(exception, "Invalid NPE when assigning null to a non flattenable field");
 108 
 109         try {
 110             test.nfij = (JumboValue?)getNull();
 111             test.nfij = null;
 112             exception = false;
 113         } catch (NullPointerException e) {
 114             exception = true;
 115         }
 116         Asserts.assertFalse(exception, "Invalid NPE when assigning null to a non flattenable field");
 117 
 118         // Assigning null to a flattenable inline field must trigger a NPE
 119         exception = false;
 120         try {
 121             fsp = (Point)getNull();
 122         } catch(NullPointerException e) {
 123             exception = true;
 124         }
 125         Asserts.assertTrue(exception, "NPE not thrown when assigning null to a flattenable field");
 126         exception = false;
 127         try {
 128             fsj = (JumboValue)getNull();
 129         } catch(NullPointerException e) {
 130             exception = true;
 131         }
 132         Asserts.assertTrue(exception, "NPE not thrown when assigning null to a flattenable field");
 133         exception = false;
 134         try {
 135             test.fip = (Point)getNull();
 136         } catch(NullPointerException e) {
 137             exception = true;
 138         }
 139         Asserts.assertTrue(exception, "NPE not thrown when assigning null to a flattenable field");
 140         exception = false;
 141         try {
 142             test.fij = (JumboValue)getNull();
 143         } catch(NullPointerException e) {
 144             exception = true;
 145         }
 146         Asserts.assertTrue(exception, "NPE not thrown when assigning null to a flattenable field");
 147         exception = false;
 148     }
 149 
 150 }