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 package runtime.valhalla.inlinetypes; 25 26 import jdk.test.lib.Asserts; 27 28 /* 29 * @test CheckcastTest 30 * @summary checkcast bytecode test 31 * @library /test/lib 32 * @compile VDefaultTest.java 33 * @run main runtime.valhalla.inlinetypes.CheckcastTest 34 */ 35 36 public class CheckcastTest { 37 38 static primitive class Point { 39 int x; 40 int y; 41 42 public Point() { 43 x = 0; 44 y = 0; 45 } 46 47 public Point(int x, int y) { 48 this.x = x; 49 this.y = y; 50 } 51 } 52 53 54 static void testCastingFromObjectToVal(Object o) { 55 boolean npe = false; 56 try { 57 Point pv = (Point)o; 58 } catch(NullPointerException e) { 59 npe = true; 60 } 61 Asserts.assertTrue(npe == false || o == null, "Casting null to val should throw a NPE"); 62 } 63 64 static void testCastingFromValToBox(Point p) { 65 boolean npe = false; 66 try { 67 Point.ref pb = p; 68 } catch(NullPointerException e) { 69 npe = true; 70 } 71 Asserts.assertFalse(npe, "Casting from val to box should not throw an NPE"); 72 } 73 74 static void testCastingFromBoxToVal(Point.ref p) { 75 boolean npe = false; 76 try { 77 Point pv = (Point) p; 78 } catch(NullPointerException e) { 79 npe = true; 80 } 81 if (npe) { 82 Asserts.assertEquals(p, null, "NPE must be thrown only if p is null"); 83 } else { 84 Asserts.assertNotEquals(p, null, "Casting null to val must thrown a NPE"); 85 } 86 87 } 88 89 public static void main(String[] args) { 90 // Testing casting from box to val 91 // First invocation: casting null to Point with an unresolved class entry 92 testCastingFromBoxToVal(null); 93 // Second invocation: casting non-null to val, will trigger resolution of the class entry 94 testCastingFromBoxToVal(new Point(3,4)); 95 // Third invocation: casting null to Point with a resolved class entry 96 testCastingFromBoxToVal(null); 97 98 // Testing casting from val to box 99 testCastingFromBoxToVal(new Point(3,4)); 100 101 // Testing casting from object to val 102 // First invocation: casting null to Point with an unresolved class entry 103 testCastingFromObjectToVal(null); 104 // Second invocation: casting non-null to al, will trigger resolution of the class entry 105 testCastingFromObjectToVal(new Point(3,4)); 106 // Third invocation: casting null to Point with a resolved class entry"); 107 testCastingFromObjectToVal(null); 108 // Fourth invocation: with something not the right type 109 boolean cce = false; 110 try { 111 testCastingFromObjectToVal(new String("NotPoint")); 112 } catch(ClassCastException e) { 113 cce = true; 114 } 115 Asserts.assertTrue(cce,"casting invalid type to val should throw CCE"); 116 } 117 }