1 /**
   2  * @test TestFieldNullability
   3  * @library /test/lib
   4  * @compile -XDemitQtypes -XDenableValueTypes -XDallowWithFieldOperator TestFieldNullability.java
   5  * @run main/othervm -Xint -Xmx128m -XX:+EnableValhalla -XX:-ShowMessageBoxOnError -XX:ValueFieldMaxFlatSize=32
   6  *                   runtime.valhalla.valuetypes.TestFieldNullability
   7  */
   8 
   9 package runtime.valhalla.valuetypes;
  10 
  11 import jdk.test.lib.Asserts;
  12 
  13 public class TestFieldNullability {
  14     static value class MyValue {
  15         int x;
  16 
  17         public MyValue() {
  18             x = 314;
  19         }
  20     }
  21 
  22     static value class MyBigValue {
  23         long l0, l1, l2, l3, l4, l5, l6, l7, l8, l9;
  24         long l10, l11, l12, l13, l14, l15, l16, l17, l18, l19;
  25 
  26         public MyBigValue() {
  27             l0 = l1 = l2 = l3 = l4 = l5 = l6 = l7 = l8 = l9 = 271;
  28             l10 = l11 = l12 = l13 = l14 = l15 = l16 = l17 = l18 = l19 = 271;
  29         }
  30     }
  31 
  32     static value class TestValue {
  33         final MyValue.box nullableField;
  34         final MyValue.val nullfreeField;       // flattened
  35         final MyValue.box nullField;           // src of null
  36         final MyBigValue.val nullfreeBigField; // not flattened
  37         final MyBigValue.box nullBigField;     // src of null
  38 
  39         public void test() {
  40             Asserts.assertNull(nullField, "Invalid non null value for for unitialized non flattenable field");
  41             Asserts.assertNull(nullBigField, "Invalid non null value for for unitialized non flattenable field");
  42             boolean NPE = false;
  43             try {
  44                 TestValue tv = __WithField(this.nullableField, nullField);
  45             } catch(NullPointerException e) {
  46                 NPE = true;
  47             }
  48             Asserts.assertFalse(NPE, "Invalid NPE when assigning null to a non flattenable field");
  49             try {
  50                 TestValue tv = __WithField(this.nullfreeField, nullField);
  51             } catch(NullPointerException e) {
  52                 NPE = true;
  53             }
  54             Asserts.assertTrue(NPE, "Missing NPE when assigning null to a flattened field");
  55             try {
  56                 TestValue tv = __WithField(this.nullfreeBigField, nullBigField);
  57             } catch(NullPointerException e) {
  58                 NPE = true;
  59             }
  60             Asserts.assertTrue(NPE, "Missing NPE when assigning null to a flattenable field");
  61         }
  62 
  63         public TestValue() {
  64             nullableField = MyValue.default;
  65             nullfreeField = MyValue.default;
  66             nullField = MyValue.default;         // fake assignment
  67             nullfreeBigField = MyBigValue.default;
  68             nullBigField = MyBigValue.default;      // fake assignment
  69             
  70         }
  71     }
  72 
  73     static class TestClass {
  74         MyValue.box nullableField;
  75         MyValue.val nullfreeField;       // flattened
  76         MyValue.box nullField;
  77         MyBigValue.val nullfreeBigField; // not flattened
  78         MyBigValue.box nullBigField;
  79 
  80         public void test() {
  81             Asserts.assertNull(nullField, "Invalid non null value for for unitialized non flattenable field");
  82             Asserts.assertNull(nullBigField, "Invalid non null value for for unitialized non flattenable field");
  83             boolean NPE = false;
  84             try {
  85                 nullableField = nullField;
  86             } catch(NullPointerException e) {
  87                 NPE = true;
  88             }
  89             Asserts.assertFalse(NPE, "Invalid NPE when assigning null to a non flattenable field");
  90             try {
  91                 this.nullfreeField = nullField;
  92             } catch(NullPointerException e) {
  93                 NPE = true;
  94             }       
  95             Asserts.assertTrue(NPE, "Missing NPE when assigning null to a flattened field");
  96             try {
  97                 this.nullfreeBigField = nullBigField;
  98             } catch(NullPointerException e) {
  99                 NPE = true;
 100             }       
 101             Asserts.assertTrue(NPE, "Missing NPE when assigning null to a flattenable field");
 102         }
 103     }
 104 
 105     public static void main(String[] args) {
 106         TestClass tc = new TestClass();
 107         tc.test();
 108         TestValue tv =
 109             TestValue.default;
 110         tv.test();
 111     }
 112     
 113 }