1 /* 2 * Copyright (c) 2013, 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.lang.reflect.Array; 28 29 import org.junit.Test; 30 31 import org.graalvm.compiler.core.test.GraalCompilerTest; 32 33 import jdk.vm.ci.meta.ResolvedJavaMethod; 34 35 /** 36 * Tests the implementation of Array.createInstance. 37 */ 38 public class DynamicNewArrayTest extends GraalCompilerTest { 39 40 private class Element { 41 } 42 43 @Test test1()44 public void test1() { 45 test("test1snippet"); 46 } 47 48 @Test test2()49 public void test2() { 50 test("test2snippet"); 51 } 52 53 @Test test3()54 public void test3() { 55 test("dynamic", Long.class, 7); 56 } 57 58 @Test test4()59 public void test4() { 60 test("dynamic", Boolean.class, -7); 61 test("dynamicSynchronized", Boolean.class, -7); 62 } 63 64 @Test test5()65 public void test5() { 66 test("dynamic", byte.class, 7); 67 } 68 69 @Test test6()70 public void test6() { 71 test("dynamic", null, 5); 72 } 73 74 @Test test7()75 public void test7() { 76 test("dynamic", void.class, 5); 77 } 78 79 @Test testStub()80 public void testStub() { 81 ResolvedJavaMethod method = getResolvedJavaMethod("dynamic"); 82 // this will use the stub call because Element[] is not loaded 83 Result actual1 = executeActual(method, null, Element.class, 7); 84 // this call will use the fast path 85 Result actual2 = executeActual(method, null, Element.class, 7); 86 Result expected = executeExpected(method, null, Element.class, 7); 87 assertEquals(actual1, expected); 88 assertEquals(actual2, expected); 89 } 90 test1snippet()91 public static Object test1snippet() { 92 return Array.newInstance(Integer.class, 7); 93 } 94 test2snippet()95 public static Object test2snippet() { 96 return Array.newInstance(char.class, 7); 97 } 98 dynamic(Class<?> elementType, int length)99 public static Object dynamic(Class<?> elementType, int length) { 100 return Array.newInstance(elementType, length); 101 } 102 dynamicSynchronized(Class<?> elementType, int length)103 public static synchronized Object dynamicSynchronized(Class<?> elementType, int length) { 104 return Array.newInstance(elementType, length); 105 } 106 } 107