1 /* 2 * Copyright (c) 2016, 2018, 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 25 package org.graalvm.compiler.replacements.test; 26 27 import java.util.ArrayList; 28 import java.util.Collection; 29 30 import org.graalvm.compiler.api.directives.GraalDirectives; 31 import org.junit.Assert; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.junit.runners.Parameterized; 35 import org.junit.runners.Parameterized.Parameter; 36 import org.junit.runners.Parameterized.Parameters; 37 38 @RunWith(Parameterized.class) 39 public class ClassCastBytecodeExceptionTest extends BytecodeExceptionTest { 40 41 @Parameter(0) public Object object; 42 @Parameter(1) public Class<?> cls; 43 44 @Parameters(name = "{1}") data()45 public static Collection<Object[]> data() { 46 Object[] objects = {"string", 42, new int[0], new Object[0], new double[0][]}; 47 48 ArrayList<Object[]> ret = new ArrayList<>(objects.length); 49 for (Object o : objects) { 50 ret.add(new Object[]{o, o.getClass()}); 51 } 52 return ret; 53 } 54 castToDouble(Object obj)55 public static void castToDouble(Object obj) { 56 /* 57 * We don't use cls.cast(obj) here because that gives a different exception message than the 58 * checkcast bytecode. 59 */ 60 if (Double.class == Double.class) { 61 Double cast = (Double) obj; 62 GraalDirectives.blackhole(cast); 63 } else if ((Class<?>) Double.class == byte[].class) { 64 byte[] cast = (byte[]) obj; 65 GraalDirectives.blackhole(cast); 66 } else if ((Class<?>) Double.class == String[].class) { 67 String[] cast = (String[]) obj; 68 GraalDirectives.blackhole(cast); 69 } else if ((Class<?>) Double.class == Object[][].class) { 70 Object[][] cast = (Object[][]) obj; 71 GraalDirectives.blackhole(cast); 72 } else { 73 Assert.fail("unexpected class argument"); 74 } 75 } 76 77 @Test testCastToDouble()78 public void testCastToDouble() { 79 test("castToDouble", object); 80 } 81 castToByteArray(Object obj)82 public static void castToByteArray(Object obj) { 83 /* 84 * We don't use cls.cast(obj) here because that gives a different exception message than the 85 * checkcast bytecode. 86 */ 87 if ((Class<?>) byte[].class == Double.class) { 88 Double cast = (Double) obj; 89 GraalDirectives.blackhole(cast); 90 } else if (byte[].class == byte[].class) { 91 byte[] cast = (byte[]) obj; 92 GraalDirectives.blackhole(cast); 93 } else if ((Class<?>) byte[].class == String[].class) { 94 String[] cast = (String[]) obj; 95 GraalDirectives.blackhole(cast); 96 } else if ((Class<?>) byte[].class == Object[][].class) { 97 Object[][] cast = (Object[][]) obj; 98 GraalDirectives.blackhole(cast); 99 } else { 100 Assert.fail("unexpected class argument"); 101 } 102 } 103 104 @Test testCastToByteArray()105 public void testCastToByteArray() { 106 test("castToByteArray", object); 107 } 108 castToStringArray(Object obj)109 public static void castToStringArray(Object obj) { 110 /* 111 * We don't use cls.cast(obj) here because that gives a different exception message than the 112 * checkcast bytecode. 113 */ 114 if ((Class<?>) String[].class == Double.class) { 115 Double cast = (Double) obj; 116 GraalDirectives.blackhole(cast); 117 } else if ((Class<?>) String[].class == byte[].class) { 118 byte[] cast = (byte[]) obj; 119 GraalDirectives.blackhole(cast); 120 } else if (String[].class == String[].class) { 121 String[] cast = (String[]) obj; 122 GraalDirectives.blackhole(cast); 123 } else if ((Class<?>) String[].class == Object[][].class) { 124 Object[][] cast = (Object[][]) obj; 125 GraalDirectives.blackhole(cast); 126 } else { 127 Assert.fail("unexpected class argument"); 128 } 129 } 130 131 @Test testCastToStringArray()132 public void testCastToStringArray() { 133 test("castToStringArray", object); 134 } 135 castToArrayArray(Object obj)136 public static void castToArrayArray(Object obj) { 137 /* 138 * We don't use cls.cast(obj) here because that gives a different exception message than the 139 * checkcast bytecode. 140 */ 141 if ((Class<?>) Object[][].class == Double.class) { 142 Double cast = (Double) obj; 143 GraalDirectives.blackhole(cast); 144 } else if ((Class<?>) Object[][].class == byte[].class) { 145 byte[] cast = (byte[]) obj; 146 GraalDirectives.blackhole(cast); 147 } else if ((Class<?>) Object[][].class == String[].class) { 148 String[] cast = (String[]) obj; 149 GraalDirectives.blackhole(cast); 150 } else if (Object[][].class == Object[][].class) { 151 Object[][] cast = (Object[][]) obj; 152 GraalDirectives.blackhole(cast); 153 } else { 154 Assert.fail("unexpected class argument"); 155 } 156 } 157 158 @Test testCastToArrayArray()159 public void testCastToArrayArray() { 160 test("castToArrayArray", object); 161 } 162 } 163