1 /*
   2  * Copyright (c) 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * @test
  28  * @bug 8222402
  29  * @summary LW2 array support in javac
  30  * @run main/othervm -XX:+EnableValhalla ArrayRelationsTest
  31  */
  32 
  33 public value class ArrayRelationsTest {
  34 
  35     int x = 42;
  36 
  37     public static void main(String [] args) {
  38         ArrayRelationsTest? [] la = new ArrayRelationsTest?[10];
  39         ArrayRelationsTest [] qa = new ArrayRelationsTest[10];
  40         boolean cce = false;
  41         try {
  42             qa = (ArrayRelationsTest[]) (Object []) (new String [10]);
  43         } catch (ClassCastException e) {
  44             cce = true;
  45         }
  46         if (!cce) {
  47             throw new AssertionError("Missing CCE");
  48         }
  49         la = qa;
  50         ArrayRelationsTest?[] la2 = qa;
  51         ArrayRelationsTest [] qa2 = (ArrayRelationsTest []) la2;
  52         boolean npe = false;
  53         try {
  54             la2[0] = null;
  55         } catch (NullPointerException e) {
  56             npe = true;
  57         }
  58         if (!npe) {
  59             throw new AssertionError("Missing NPE");
  60         }
  61         npe = false;
  62         Object [] oa = qa;
  63         try {
  64             oa[0] = null;
  65         } catch (NullPointerException e) {
  66             npe = true;
  67         }
  68         if (!npe) {
  69             throw new AssertionError("Missing NPE");
  70         }
  71 
  72         // round trip;
  73         Object o = oa = la = qa;
  74         qa = (ArrayRelationsTest[]) (la = (ArrayRelationsTest? []) (oa = (Object []) o));
  75         qa [0] = new ArrayRelationsTest();
  76 
  77         npe = false;
  78         try {
  79             la[0] = null;
  80         } catch (NullPointerException e) {
  81             npe = true;
  82         }
  83         if (!npe) {
  84             throw new AssertionError("Missing NPE");
  85         }
  86 
  87         la = new ArrayRelationsTest? [10];
  88 
  89         cce = false;
  90         try {
  91             qa = (ArrayRelationsTest[]) la;
  92         } catch (ClassCastException c) {
  93             cce = true;
  94         }
  95         if (!cce) {
  96             throw new AssertionError("Unexpected CCE behavior");
  97         }
  98     }
  99 }