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.util.Collection;
 26 import java.util.List;
 27 
 28 /*
 29  * @test
 30  * @summary Smoke test for code reflection with cast expressions.
 31  * @modules jdk.incubator.code
 32  * @build CastInstanceOfTest
 33  * @build CodeReflectionTester
 34  * @run main CodeReflectionTester CastInstanceOfTest
 35  */
 36 
 37 public class CastInstanceOfTest {
 38 
 39     @CodeReflection
 40     @IR("""
 41             func @"test1" (%0 : CastInstanceOfTest, %1 : java.lang.Object)void -> {
 42                 %2 : Var<java.lang.Object> = var %1 @"o";
 43                 %3 : java.lang.Object = var.load %2;
 44                 %4 : java.lang.String = cast %3 @"java.lang.String";
 45                 %5 : Var<java.lang.String> = var %4 @"s";
 46                 %6 : java.lang.String = var.load %5;
 47                 %7 : Var<java.lang.String> = var %6 @"ss";
 48                 return;
 49             };
 50             """)
 51     void test1(Object o) {
 52         String s = (String) o;
 53         String ss = (String) s;
 54     }
 55 
 56     @CodeReflection
 57     @IR("""
 58             func @"test2" (%0 : CastInstanceOfTest, %1 : java.lang.Object)void -> {
 59                 %2 : Var<java.lang.Object> = var %1 @"o";
 60                 %3 : java.lang.Object = var.load %2;
 61                 %4 : java.util.List<java.lang.String> = cast %3 @"java.util.List";
 62                 %5 : Var<java.util.List<java.lang.String>> = var %4 @"l";
 63                 %6 : java.util.List<java.lang.String> = var.load %5;
 64                 %7 : Var<java.util.Collection<java.lang.String>> = var %6 @"c1";
 65                 %8 : java.util.List<java.lang.String> = var.load %5;
 66                 %9 : Var<java.util.Collection<java.lang.String>> = var %8 @"c2";
 67                 return;
 68             };
 69             """)
 70     void test2(Object o) {
 71         List<String> l = (List<String>) o;
 72         Collection<String> c1 = (List<String>) l;
 73         Collection<String> c2 = (Collection) l;
 74     }
 75 
 76     @CodeReflection
 77     @IR("""
 78             func @"test3" (%0 : CastInstanceOfTest, %1 : java.util.List<java.lang.String>)void -> {
 79                 %2 : Var<java.util.List<java.lang.String>> = var %1 @"l";
 80                 %3 : java.util.List<java.lang.String> = var.load %2;
 81                 %4 : Var<java.util.List> = var %3 @"raw";
 82                 %5 : java.util.List = var.load %4;
 83                 %6 : Var<java.util.List<java.lang.Number>> = var %5 @"ln";
 84                 return;
 85             };
 86             """)
 87     void test3(List<String> l) {
 88         List raw = l;
 89         List<Number> ln = raw;
 90     }
 91 
 92     @CodeReflection
 93     @IR("""
 94             func @"test4" (%0 : CastInstanceOfTest, %1 : int)void -> {
 95                 %2 : Var<int> = var %1 @"i";
 96                 %3 : int = var.load %2;
 97                 %4 : long = conv %3;
 98                 %5 : Var<long> = var %4 @"l";
 99                 return;
100             };
101             """)
102     void test4(int i) {
103         long l = (int) i;
104     }
105 
106     @CodeReflection
107     @IR("""
108             func @"test5" (%0 : CastInstanceOfTest, %1 : java.lang.Object)void -> {
109                 %2 : Var<java.lang.Object> = var %1 @"o";
110                 %3 : java.lang.Object = var.load %2;
111                 %4 : boolean = instanceof %3 @"java.lang.String";
112                 %5 : Var<boolean> = var %4 @"b";
113                 return;
114             };
115             """)
116     void test5(Object o) {
117         boolean b = o instanceof String;
118     }
119 
120     @CodeReflection
121     @IR("""
122             func @"test6" (%0 : CastInstanceOfTest, %1 : java.util.List<java.lang.Object>)void -> {
123                 %2 : Var<java.util.List<java.lang.Object>> = var %1 @"l";
124                 %3 : java.util.List<java.lang.Object> = var.load %2;
125                 %4 : int = constant @"0";
126                 %5 : java.lang.Object = invoke %3 %4 @"java.util.List::get(int)java.lang.Object";
127                 %6 : boolean = instanceof %5 @"java.lang.String";
128                 %7 : Var<boolean> = var %6 @"b";
129                 return;
130             };
131             """)
132     void test6(List<Object> l) {
133         boolean b = l.get(0) instanceof String;
134     }
135 }