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 /*
 25  * @test
 26  * @summary Smoke test for code reflection with constant values.
 27  * @build ConstantsTest
 28  * @build CodeReflectionTester
 29  * @run main CodeReflectionTester ConstantsTest
 30  */
 31 
 32 import java.lang.runtime.CodeReflection;
 33 import java.util.function.Function;
 34 
 35 public class ConstantsTest {
 36     @CodeReflection
 37     @IR("""
 38             func @"test1" (%0 : ConstantsTest)void -> {
 39                 %1 : java.lang.String = constant @"";
 40                 %2 : Var<java.lang.String> = var %1 @"s";
 41                 return;
 42             };
 43             """)
 44     void test1() {
 45         String s = "";
 46     }
 47 
 48     @CodeReflection
 49     @IR("""
 50             func @"test2" (%0 : ConstantsTest)void -> {
 51                 %1 : java.lang.String = constant @"Hello World";
 52                 %2 : Var<java.lang.String> = var %1 @"s";
 53                 return;
 54             };
 55             """)
 56     void test2() {
 57         String s = "Hello World";
 58     }
 59 
 60     @CodeReflection
 61     @IR("""
 62             func @"test3" (%0 : ConstantsTest)void -> {
 63                 %1 : java.lang.String = constant @null;
 64                 %2 : Var<java.lang.String> = var %1 @"s";
 65                 return;
 66             };
 67             """)
 68     void test3() {
 69         String s = null;
 70     }
 71 
 72     @IR("""
 73             func @"test4" (%0 : ConstantsTest)void -> {
 74                 %1 : java.lang.Class = constant @"java.util.function.Function";
 75                 %2 : Var<java.lang.Class<+<java.lang.Object>>> = var %1 @"s";
 76                 return;
 77             };
 78             """)
 79     @CodeReflection
 80     void test4() {
 81         Class<?> s = Function.class;
 82     }
 83 
 84     @IR("""
 85             func @"test5" (%0 : ConstantsTest)void -> {
 86                 %1 : int = constant @"42";
 87                 %2 : byte = conv %1;
 88                 %3 : Var<byte> = var %2 @"v";
 89                 %4 : int = constant @"-42";
 90                 %5 : byte = conv %4;
 91                 var.store %3 %5;
 92                 return;
 93             };
 94             """)
 95     @CodeReflection
 96     void test5() {
 97         byte v = 42;
 98         v = -42;
 99     }
100 
101     @IR("""
102             func @"test6" (%0 : ConstantsTest)void -> {
103                 %1 : int = constant @"42";
104                 %2 : short = conv %1;
105                 %3 : Var<short> = var %2 @"v";
106                 %4 : int = constant @"-42";
107                 %5 : short = conv %4;
108                 var.store %3 %5;
109                 return;
110             };
111             """)
112     @CodeReflection
113     void test6() {
114         short v = 42;
115         v = -42;
116     }
117 
118     @IR("""
119             func @"test7" (%0 : ConstantsTest)void -> {
120                 %1 : int = constant @"42";
121                 %2 : Var<int> = var %1 @"v";
122                 %3 : int = constant @"-42";
123                 var.store %2 %3;
124                 return;
125             };
126             """)
127     @CodeReflection
128     void test7() {
129         int v = 42;
130         v = -42;
131     }
132 
133     @IR("""
134             func @"test8" (%0 : ConstantsTest)void -> {
135                 %1 : long = constant @"42";
136                 %2 : Var<long> = var %1 @"v";
137                 %3 : long = constant @"-42";
138                 var.store %2 %3;
139                 return;
140             };
141             """)
142     @CodeReflection
143     void test8() {
144         long v = 42L;
145         v = -42L;
146     }
147 
148     @IR("""
149             func @"test9" (%0 : ConstantsTest)void -> {
150                 %1 : float = constant @"42.0";
151                 %2 : Var<float> = var %1 @"v";
152                 %3 : float = constant @"42.0";
153                 %4 : float = neg %3;
154                 var.store %2 %4;
155                 return;
156             };
157             """)
158     @CodeReflection
159     void test9() {
160         float v = 42.0f;
161         v = -42.0f;
162     }
163 
164     @IR("""
165             func @"test10" (%0 : ConstantsTest)void -> {
166                 %1 : double = constant @"42.0";
167                 %2 : Var<double> = var %1 @"v";
168                 %3 : double = constant @"42.0";
169                 %4 : double = neg %3;
170                 var.store %2 %4;
171                 return;
172             };
173             """)
174     @CodeReflection
175     void test10() {
176         double v = 42.0;
177         v = -42.0;
178     }
179 
180     @IR("""
181             func @"test11" (%0 : ConstantsTest)void -> {
182                 %1 : char = constant @"a";
183                 %2 : Var<char> = var %1 @"v";
184                 return;
185             };
186             """)
187     @CodeReflection
188     void test11() {
189         char v = 'a';
190     }
191 
192     @IR("""
193             func @"test12" (%0 : ConstantsTest)void -> {
194                 %1 : boolean = constant @"true";
195                 %2 : Var<boolean> = var %1 @"b";
196                 %3 : boolean = constant @"false";
197                 var.store %2 %3;
198                 return;
199             };
200             """)
201     @CodeReflection
202     void test12() {
203         boolean b = true;
204         b = false;
205     }
206 
207     @IR("""
208             func @"test13" (%0 : ConstantsTest)void -> {
209                 %1 : java.lang.Class = constant @"float";
210                 %2 : Var<java.lang.Class<+<java.lang.Object>>> = var %1 @"s";
211                 return;
212             };
213             """)
214     @CodeReflection
215     void test13() {
216         Class<?> s = float.class;
217     }
218 
219     @IR("""
220             func @"test14" (%0 : ConstantsTest)void -> {
221                 %1 : java.lang.Class = constant @"java.lang.String[]";
222                 %2 : Var<java.lang.Class<+<java.lang.Object>>> = var %1 @"s";
223                 return;
224             };
225             """)
226     @CodeReflection
227     void test14() {
228         Class<?> s = String[].class;
229     }
230 
231     @IR("""
232             func @"test15" (%0 : ConstantsTest)void -> {
233                 %1 : java.lang.Class = constant @"java.lang.String[][]";
234                 %2 : Var<java.lang.Class<+<java.lang.Object>>> = var %1 @"s";
235                 return;
236             };
237             """)
238     @CodeReflection
239     void test15() {
240         Class<?> s = String[][].class;
241     }
242 
243     @IR("""
244             func @"test16" (%0 : ConstantsTest)void -> {
245                 %1 : java.lang.Class = constant @"java.lang.String[][][]";
246                 %2 : Var<java.lang.Class<+<java.lang.Object>>> = var %1 @"s";
247                 return;
248             };
249             """)
250     @CodeReflection
251     void test16() {
252         Class<?> s = String[][][].class;
253     }
254 
255     @IR("""
256             func @"test17" (%0 : ConstantsTest)void -> {
257                 %1 : java.lang.Class = constant @"boolean[]";
258                 %2 : Var<java.lang.Class<+<java.lang.Object>>> = var %1 @"s";
259                 return;
260             };
261             """)
262     @CodeReflection
263     void test17() {
264         Class<?> s = boolean[].class;
265     }
266 
267     @IR("""
268             func @"test18" (%0 : ConstantsTest)void -> {
269                 %1 : java.lang.Class = constant @"boolean[][][]";
270                 %2 : Var<java.lang.Class<+<java.lang.Object>>> = var %1 @"s";
271                 return;
272             };
273             """)
274     @CodeReflection
275     void test18() {
276         Class<?> s = boolean[][][].class;
277     }
278 }