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.CodeReflection;
 25 import java.math.BigDecimal;
 26 import java.util.List;
 27 
 28 /*
 29  * @test
 30  * @summary Smoke test for code reflection with new expressions.
 31  * @modules jdk.incubator.code
 32  * @build NewTest
 33  * @build CodeReflectionTester
 34  * @run main CodeReflectionTester NewTest
 35  */
 36 
 37 public class NewTest {
 38 
 39     @CodeReflection
 40     @IR("""
 41             func @"test0" (%0 : NewTest)void -> {
 42                 %1 : java.lang.String = constant @"1";
 43                 %2 : java.math.BigDecimal = new %1 @"func<java.math.BigDecimal, java.lang.String>";
 44                 %3 : Var<java.math.BigDecimal> = var %2 @"a";
 45                 return;
 46             };
 47             """)
 48     void test0() {
 49         BigDecimal a = new BigDecimal("1");
 50     }
 51 
 52     static class A {
 53         A() {}
 54 
 55         A(int i, int j) {}
 56     }
 57 
 58     @CodeReflection
 59     @IR("""
 60             func @"test1" (%0 : NewTest)void -> {
 61                 %1 : NewTest$A = new @"func<NewTest$A>";
 62                 %2 : Var<NewTest$A> = var %1 @"a";
 63                 return;
 64             };
 65             """)
 66     void test1() {
 67         A a = new A();
 68     }
 69 
 70     @CodeReflection
 71     @IR("""
 72             func @"test2" (%0 : NewTest)void -> {
 73                 %1 : int = constant @"1";
 74                 %2 : int = constant @"2";
 75                 %3 : NewTest$A = new %1 %2 @"func<NewTest$A, int, int>";
 76                 %4 : Var<NewTest$A> = var %3 @"a";
 77                 return;
 78             };
 79             """)
 80     void test2() {
 81         A a = new A(1, 2);
 82     }
 83 
 84     class B {
 85         B() {}
 86 
 87         B(int i, int j) {}
 88 
 89         class C {
 90         }
 91     }
 92 
 93     B f;
 94 
 95     B b() { return f; }
 96 
 97     @CodeReflection
 98     @IR("""
 99             func @"test3" (%0 : NewTest)void -> {
100                 %1 : .<NewTest, NewTest$B> = new %0 @"func<.<NewTest, NewTest$B>, NewTest>";
101                 %2 : Var<.<NewTest, NewTest$B>> = var %1 @"b";
102                 return;
103             };
104             """)
105     void test3() {
106         B b = new B();
107     }
108 
109     @CodeReflection
110     @IR("""
111             func @"test4" (%0 : NewTest)void -> {
112                 %1 : int = constant @"1";
113                 %2 : int = constant @"2";
114                 %3 : .<NewTest, NewTest$B> = new %0 %1 %2 @"func<.<NewTest, NewTest$B>, NewTest, int, int>";
115                 %4 : Var<.<NewTest, NewTest$B>> = var %3 @"b";
116                 return;
117             };
118             """)
119     void test4() {
120         B b = new B(1, 2);
121     }
122 
123     @CodeReflection
124     @IR("""
125             func @"test5" (%0 : NewTest)void -> {
126                 %1 : .<NewTest, NewTest$B> = new %0 @"func<.<NewTest, NewTest$B>, NewTest>";
127                 %2 : Var<.<NewTest, NewTest$B>> = var %1 @"b";
128                 return;
129             };
130             """)
131     void test5() {
132         B b = this.new B();
133     }
134 
135     @CodeReflection
136     @IR("""
137             func @"test6" (%0 : NewTest)void -> {
138                 %1 : .<NewTest, NewTest$B> = field.load %0 @"NewTest::f().<NewTest, NewTest$B>";
139                 %2 : .<.<NewTest, NewTest$B>, NewTest$B$C> = new %1 @"func<.<.<NewTest, NewTest$B>, NewTest$B$C>, .<NewTest, NewTest$B>>";
140                 %3 : Var<.<.<NewTest, NewTest$B>, NewTest$B$C>> = var %2 @"c";
141                 return;
142             };
143             """)
144     void test6() {
145         B.C c = f.new C();
146     }
147 
148     @CodeReflection
149     @IR("""
150             func @"test7" (%0 : NewTest)void -> {
151                 %1 : .<NewTest, NewTest$B> = invoke %0 @"NewTest::b().<NewTest, NewTest$B>";
152                 %2 : .<.<NewTest, NewTest$B>, NewTest$B$C> = new %1 @"func<.<.<NewTest, NewTest$B>, NewTest$B$C>, .<NewTest, NewTest$B>>";
153                 %3 : Var<.<.<NewTest, NewTest$B>, NewTest$B$C>> = var %2 @"c";
154                 return;
155             };
156             """)
157     void test7() {
158         B.C c = b().new C();
159     }
160 
161     static class AG<T> {
162         AG(List<T> l) {}
163     }
164 
165     @CodeReflection
166     @IR("""
167             func @"test8" (%0 : NewTest, %1 : java.util.List<java.lang.String>)void -> {
168                 %2 : Var<java.util.List<java.lang.String>> = var %1 @"l";
169                 %3 : java.util.List<java.lang.String> = var.load %2;
170                 %4 : NewTest$AG<java.lang.String> = new %3 @"func<NewTest$AG, java.util.List>";
171                 %5 : Var<NewTest$AG<java.lang.String>> = var %4 @"a";
172                 return;
173             };
174             """)
175     void test8(List<String> l) {
176         AG<String> a = new AG<>(l);
177     }
178 
179     class BG<T> {
180         BG(List<T> l) {}
181 
182         class CG<U> {
183             CG(List<U> l) {}
184         }
185     }
186 
187     @CodeReflection
188     @IR("""
189             func @"test9" (%0 : NewTest, %1 : java.util.List<java.lang.String>, %2 : java.util.List<java.lang.Number>)void -> {
190                 %3 : Var<java.util.List<java.lang.String>> = var %1 @"l1";
191                 %4 : Var<java.util.List<java.lang.Number>> = var %2 @"l2";
192                 %5 : java.util.List<java.lang.String> = var.load %3;
193                 %6 : .<NewTest, NewTest$BG<java.lang.String>> = new %0 %5 @"func<.<NewTest, NewTest$BG>, NewTest, java.util.List>";
194                 %7 : java.util.List<java.lang.Number> = var.load %4;
195                 %8 : .<.<NewTest, NewTest$BG<java.lang.String>>, NewTest$BG$CG<java.lang.Number>> = new %6 %7 @"func<.<.<NewTest, NewTest$BG>, NewTest$BG$CG>, .<NewTest, NewTest$BG<java.lang.String>>, java.util.List>";
196                 %9 : Var<.<.<NewTest, NewTest$BG<java.lang.String>>, NewTest$BG$CG<java.lang.Number>>> = var %8 @"numberCG";
197                 return;
198             };
199             """)
200     void test9(List<String> l1, List<Number> l2) {
201         BG<String>.CG<Number> numberCG = new BG<String>(l1).new CG<Number>(l2);
202     }
203 
204 
205     @CodeReflection
206     @IR("""
207             func @"test10" (%0 : NewTest)void -> {
208                 %1 : int = constant @"10";
209                 %2 : int[] = new %1 @"func<int[], int>";
210                 %3 : Var<int[]> = var %2 @"i";
211                 return;
212             };
213             """)
214     void test10() {
215         int[] i = new int[10];
216     }
217 
218     @CodeReflection
219     @IR("""
220             func @"test11" (%0 : NewTest, %1 : int)void -> {
221                 %2 : Var<int> = var %1 @"i";
222                 %3 : int = var.load %2;
223                 %4 : int = var.load %2;
224                 %5 : int = constant @"1";
225                 %6 : int = add %4 %5;
226                 %7 : int = var.load %2;
227                 %8 : int = constant @"2";
228                 %9 : int = add %7 %8;
229                 %10 : java.lang.String[][][] = new %3 %6 %9 @"func<java.lang.String[][][], int, int, int>";
230                 %11 : Var<java.lang.String[][][]> = var %10 @"s";
231                 return;
232             };
233             """)
234     void test11(int i) {
235         String[][][] s = new String[i][i + 1][i + 2];
236     }
237 }