1 /* 2 * Copyright (c) 2024, 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 import org.testng.Assert; 25 import org.testng.annotations.Test; 26 27 import jdk.incubator.code.OpTransformer; 28 import jdk.incubator.code.dialect.core.CoreOp; 29 import jdk.incubator.code.Op; 30 import jdk.incubator.code.bytecode.BytecodeGenerator; 31 32 import java.lang.invoke.MethodHandle; 33 import java.lang.invoke.MethodHandles; 34 import java.lang.reflect.Method; 35 import jdk.incubator.code.CodeReflection; 36 import java.util.Optional; 37 import java.util.stream.Stream; 38 39 /* 40 * @test 41 * @modules jdk.incubator.code 42 * @run testng TestArrayCreation 43 */ 44 45 public class TestArrayCreation { 46 @CodeReflection 47 public static String[] f() { 48 return new String[10]; 49 } 50 51 @Test 52 public void testf() throws Throwable { 53 CoreOp.FuncOp f = getFuncOp("f"); 54 55 MethodHandle mh = generate(f); 56 57 Assert.assertEquals((String[]) mh.invoke(), f()); 58 } 59 60 @CodeReflection 61 public static String[][] f2() { 62 return new String[10][]; 63 } 64 65 @Test 66 public void testf2() throws Throwable { 67 CoreOp.FuncOp f = getFuncOp("f2"); 68 69 MethodHandle mh = generate(f); 70 71 Assert.assertEquals((String[][]) mh.invoke(), f2()); 72 } 73 74 @CodeReflection 75 public static String[][] f3() { 76 return new String[10][10]; 77 } 78 79 @Test 80 public void testf3() throws Throwable { 81 CoreOp.FuncOp f = getFuncOp("f3"); 82 83 MethodHandle mh = generate(f); 84 85 Assert.assertEquals((String[][]) mh.invoke(), f3()); 86 } 87 88 @CodeReflection 89 public static String[][] f4() { 90 return new String[][]{{"one", "two"}, {"three"}}; 91 } 92 93 @Test 94 public void testf4() throws Throwable { 95 CoreOp.FuncOp f = getFuncOp("f4"); 96 97 MethodHandle mh = generate(f); 98 99 Assert.assertEquals((String[][]) mh.invoke(), f4()); 100 } 101 102 static MethodHandle generate(CoreOp.FuncOp f) { 103 System.out.println(f.toText()); 104 105 CoreOp.FuncOp lf = f.transform(OpTransformer.LOWERING_TRANSFORMER); 106 System.out.println(lf.toText()); 107 108 return BytecodeGenerator.generate(MethodHandles.lookup(), lf); 109 } 110 111 static CoreOp.FuncOp getFuncOp(String name) { 112 Optional<Method> om = Stream.of(TestArrayCreation.class.getDeclaredMethods()) 113 .filter(m -> m.getName().equals(name)) 114 .findFirst(); 115 116 Method m = om.get(); 117 return Op.ofMethod(m).get(); 118 } 119 }