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 8222792
 29  * @summary Javac should enforce the latest relationship rules between an inline type and its nullable projection
 30  * @run main/othervm TypeRelationsTest
 31  */
 32 
 33 public primitive class TypeRelationsTest {
 34 
 35     int x = 42;
 36 
 37     static boolean foo(TypeRelationsTest x, TypeRelationsTest.ref xq, boolean nullPassed) {
 38         TypeRelationsTest xl;
 39         TypeRelationsTest.ref xql;
 40         boolean npe = false;
 41 
 42         xl = x;
 43         xl = (TypeRelationsTest) x;
 44         try {
 45             xl = (TypeRelationsTest) xq;
 46         } catch (NullPointerException e) {
 47             npe = true;
 48         }
 49 
 50         xql = x;
 51         xql = (TypeRelationsTest.ref ) x;
 52         xql = xq;
 53         xql = (TypeRelationsTest.ref) xq;
 54         return npe;
 55     }
 56 
 57     static String foo(Object o) {
 58         return "Object";
 59     }
 60 
 61     static String foo(TypeRelationsTest x) {
 62         return "TypeRelationsTest";
 63     }
 64 
 65     static String foo(TypeRelationsTest.ref xq) {
 66         return "TypeRelationsTest.ref";
 67     }
 68 
 69     public static void main(String [] args) {
 70        if (foo(new TypeRelationsTest(), new TypeRelationsTest(), false))
 71             throw new AssertionError("Unexpected NPE");
 72        if (!foo(new TypeRelationsTest(), null, true))
 73             throw new AssertionError("Missing NPE");
 74 
 75        TypeRelationsTest x = new TypeRelationsTest();
 76        TypeRelationsTest.ref xq = null;
 77        if (!foo(x).equals("TypeRelationsTest"))
 78             throw new AssertionError("Wrong overload");
 79        if (!foo(xq).equals("TypeRelationsTest.ref"))
 80             throw new AssertionError("Wrong overload");
 81        if (!foo((TypeRelationsTest.ref) x).equals("TypeRelationsTest.ref"))
 82             throw new AssertionError("Wrong overload");
 83 
 84        boolean npe = false;
 85        try  {
 86            foo((TypeRelationsTest) xq);
 87        } catch (NullPointerException e) {
 88             npe = true;
 89        }
 90        if (!npe) {
 91             throw new AssertionError("Missing NPE");
 92        }
 93        xq = x;
 94        if (!foo((TypeRelationsTest.ref) xq).equals("TypeRelationsTest.ref"))
 95             throw new AssertionError("Wrong overload");
 96        checkArrays();
 97     }
 98 
 99     static void checkArrays() {
100         TypeRelationsTest [] xa = new TypeRelationsTest[10];
101         TypeRelationsTest.ref [] xqa;
102         Object [] oa;
103         Object o;
104 
105         o = oa = xqa = xa;
106         xa = (TypeRelationsTest []) (xqa = (TypeRelationsTest.ref[]) (oa = (Object []) o));
107         xa[0] = new TypeRelationsTest(); // OK, after round trip back and forth.
108 
109 
110         xqa = (TypeRelationsTest.ref[]) xa;
111         boolean npe = false;
112         try {
113             xqa[0] = null;
114         } catch (NullPointerException e) {
115             npe = true;
116         }
117         if (!npe) {
118            throw new AssertionError("Missing NPE");
119         }
120         npe = false;
121 
122         oa = xa;
123         try {
124             oa[0] = null;
125         } catch (NullPointerException e) {
126             npe = true;
127         }
128         if (!npe) {
129            throw new AssertionError("Missing NPE");
130         }
131     }
132 }