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 jdk.incubator.code.Op;
25 import org.testng.Assert;
26 import org.testng.annotations.Test;
27 
28 import java.lang.invoke.MethodHandles;
29 import jdk.incubator.code.op.CoreOp;
30 import jdk.incubator.code.interpreter.Interpreter;
31 import java.lang.reflect.Method;
32 import jdk.incubator.code.CodeReflection;
33 import java.util.Optional;
34 import java.util.stream.Stream;
35 
36 /*
37  * @test
38  * @modules jdk.incubator.code
39  * @run testng TestArrayCreation
40  */
41 
42 public class TestArrayCreation {
43     @CodeReflection
44     public static String[] f() {
45         return new String[10];
46     }
47 
48     @Test
49     public void testf() {
50         CoreOp.FuncOp f = getFuncOp("f");
51 
52         f.writeTo(System.out);
53 
54         Assert.assertEquals(Interpreter.invoke(MethodHandles.lookup(), f), f());
55     }
56 
57     @CodeReflection
58     public static String[][] f2() {
59         return new String[10][];
60     }
61 
62     @Test
63     public void testf2() {
64         CoreOp.FuncOp f = getFuncOp("f2");
65 
66         f.writeTo(System.out);
67 
68         Assert.assertEquals(Interpreter.invoke(MethodHandles.lookup(), f), f2());
69     }
70 
71     @CodeReflection
72     public static String[][] f3() {
73         return new String[10][10];
74     }
75 
76     @Test
77     public void testf3() {
78         CoreOp.FuncOp f = getFuncOp("f3");
79 
80         f.writeTo(System.out);
81 
82         Assert.assertEquals(Interpreter.invoke(MethodHandles.lookup(), f), f3());
83     }
84 
85     static CoreOp.FuncOp getFuncOp(String name) {
86         Optional<Method> om = Stream.of(TestArrayCreation.class.getDeclaredMethods())
87                 .filter(m -> m.getName().equals(name))
88                 .findFirst();
89 
90         Method m = om.get();
91         return Op.ofMethod(m).get();
92     }
93 }