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.function.BiConsumer; 26 import java.util.function.Consumer; 27 import java.util.function.Function; 28 import java.util.function.IntFunction; 29 import java.util.function.Supplier; 30 31 /* 32 * @test 33 * @summary Smoke test for code reflection with method reference expressions. 34 * @modules jdk.incubator.code 35 * @build MethodReferenceTest 36 * @build CodeReflectionTester 37 * @run main CodeReflectionTester MethodReferenceTest 38 */ 39 40 public class MethodReferenceTest { 41 42 static void m_s(String s) {} 43 44 void m(String s) {} 45 46 @CodeReflection 47 @IR(""" 48 func @"test1" (%0 : java.type:"MethodReferenceTest")java.type:"void" -> { 49 %1 : java.type:"java.util.function.Consumer<java.lang.String>" = lambda (%2 : java.type:"java.lang.String")java.type:"void" -> { 50 %3 : Var<java.type:"java.lang.String"> = var %2 @"x$0"; 51 %4 : java.type:"java.lang.String" = var.load %3; 52 invoke %4 @java.ref:"MethodReferenceTest::m_s(java.lang.String):void"; 53 return; 54 }; 55 %5 : Var<java.type:"java.util.function.Consumer<java.lang.String>"> = var %1 @"c"; 56 return; 57 }; 58 """) 59 void test1() { 60 Consumer<String> c = MethodReferenceTest::m_s; 61 } 62 63 @CodeReflection 64 @IR(""" 65 func @"test2" (%0 : java.type:"MethodReferenceTest")java.type:"void" -> { 66 %1 : java.type:"java.util.function.BiConsumer<MethodReferenceTest, java.lang.String>" = lambda (%2 : java.type:"MethodReferenceTest", %3 : java.type:"java.lang.String")java.type:"void" -> { 67 %4 : Var<java.type:"MethodReferenceTest"> = var %2 @"rec$"; 68 %5 : Var<java.type:"java.lang.String"> = var %3 @"x$0"; 69 %6 : java.type:"MethodReferenceTest" = var.load %4; 70 %7 : java.type:"java.lang.String" = var.load %5; 71 invoke %6 %7 @java.ref:"MethodReferenceTest::m(java.lang.String):void"; 72 return; 73 }; 74 %8 : Var<java.type:"java.util.function.BiConsumer<MethodReferenceTest, java.lang.String>"> = var %1 @"bc"; 75 return; 76 }; 77 """) 78 void test2() { 79 BiConsumer<MethodReferenceTest, String> bc = MethodReferenceTest::m; 80 } 81 82 @CodeReflection 83 @IR(""" 84 func @"test3" (%0 : java.type:"MethodReferenceTest")java.type:"void" -> { 85 %1 : java.type:"java.util.function.Consumer<java.lang.String>" = lambda (%2 : java.type:"java.lang.String")java.type:"void" -> { 86 %3 : Var<java.type:"java.lang.String"> = var %2 @"x$0"; 87 %4 : java.type:"java.lang.String" = var.load %3; 88 invoke %0 %4 @java.ref:"MethodReferenceTest::m(java.lang.String):void"; 89 return; 90 }; 91 %5 : Var<java.type:"java.util.function.Consumer<java.lang.String>"> = var %1 @"c"; 92 return; 93 }; 94 """) 95 void test3() { 96 Consumer<String> c = this::m; 97 } 98 99 class A<T> { 100 T m(T t) { return t; } 101 } 102 103 <T> A<T> a(T t) { return null; } 104 105 @CodeReflection 106 @IR(""" 107 func @"test4" (%0 : java.type:"MethodReferenceTest")java.type:"void" -> { 108 %1 : java.type:"java.lang.String" = constant @"s"; 109 %2 : java.type:"MethodReferenceTest::A<java.lang.String>" = invoke %0 %1 @java.ref:"MethodReferenceTest::a(java.lang.Object):MethodReferenceTest::A"; 110 %3 : Var<java.type:"MethodReferenceTest::A<java.lang.String>"> = var %2 @"rec$"; 111 %4 : java.type:"java.util.function.Function<java.lang.String, java.lang.String>" = lambda (%5 : java.type:"java.lang.String")java.type:"java.lang.String" -> { 112 %6 : Var<java.type:"java.lang.String"> = var %5 @"x$0"; 113 %7 : java.type:"MethodReferenceTest::A<java.lang.String>" = var.load %3; 114 %8 : java.type:"java.lang.String" = var.load %6; 115 %9 : java.type:"java.lang.String" = invoke %7 %8 @java.ref:"MethodReferenceTest::A::m(java.lang.Object):java.lang.Object"; 116 return %9; 117 }; 118 %10 : Var<java.type:"java.util.function.Function<java.lang.String, java.lang.String>"> = var %4 @"f"; 119 return; 120 }; 121 """) 122 void test4() { 123 Function<String, String> f = a("s")::m; 124 } 125 126 @CodeReflection 127 @IR(""" 128 func @"test5" (%0 : java.type:"MethodReferenceTest")java.type:"void" -> { 129 %1 : java.type:"java.io.PrintStream" = field.load @java.ref:"java.lang.System::out:java.io.PrintStream"; 130 %2 : Var<java.type:"java.io.PrintStream"> = var %1 @"rec$"; 131 %3 : java.type:"java.util.function.Consumer<java.lang.String>" = lambda (%4 : java.type:"java.lang.String")java.type:"void" -> { 132 %5 : Var<java.type:"java.lang.String"> = var %4 @"x$0"; 133 %6 : java.type:"java.io.PrintStream" = var.load %2; 134 %7 : java.type:"java.lang.String" = var.load %5; 135 invoke %6 %7 @java.ref:"java.io.PrintStream::println(java.lang.String):void"; 136 return; 137 }; 138 %8 : Var<java.type:"java.util.function.Consumer<java.lang.String>"> = var %3 @"c3"; 139 return; 140 }; 141 """) 142 void test5() { 143 Consumer<String> c3 = System.out::println; 144 } 145 146 static class X { 147 X(int i) {} 148 } 149 150 @CodeReflection 151 @IR(""" 152 func @"test6" (%0 : java.type:"MethodReferenceTest")java.type:"void" -> { 153 %1 : java.type:"java.util.function.Function<java.lang.Integer, MethodReferenceTest$X>" = lambda (%2 : java.type:"java.lang.Integer")java.type:"MethodReferenceTest$X" -> { 154 %3 : Var<java.type:"java.lang.Integer"> = var %2 @"x$0"; 155 %4 : java.type:"java.lang.Integer" = var.load %3; 156 %5 : java.type:"int" = invoke %4 @java.ref:"java.lang.Integer::intValue():int"; 157 %6 : java.type:"MethodReferenceTest$X" = new %5 @java.ref:"MethodReferenceTest$X::(int)"; 158 return %6; 159 }; 160 %7 : Var<java.type:"java.util.function.Function<java.lang.Integer, MethodReferenceTest$X>"> = var %1 @"xNew"; 161 return; 162 }; 163 """) 164 void test6() { 165 Function<Integer, X> xNew = X::new; 166 } 167 168 @CodeReflection 169 @IR(""" 170 func @"test7" (%0 : java.type:"MethodReferenceTest")java.type:"void" -> { 171 %1 : java.type:"java.util.function.Supplier<MethodReferenceTest::A<java.lang.String>>" = lambda ()java.type:"MethodReferenceTest::A<java.lang.String>" -> { 172 %2 : java.type:"MethodReferenceTest::A<java.lang.String>" = new %0 @java.ref:"MethodReferenceTest::A::(MethodReferenceTest)"; 173 return %2; 174 }; 175 %3 : Var<java.type:"java.util.function.Supplier<MethodReferenceTest::A<java.lang.String>>"> = var %1 @"aNew"; 176 return; 177 }; 178 """) 179 void test7() { 180 Supplier<A<String>> aNew = A::new; 181 } 182 183 @CodeReflection 184 @IR(""" 185 func @"test8" (%0 : java.type:"MethodReferenceTest")java.type:"void" -> { 186 %1 : java.type:"java.util.function.IntFunction<MethodReferenceTest::A<java.lang.String>[]>" = lambda (%2 : java.type:"int")java.type:"MethodReferenceTest::A<java.lang.String>[]" -> { 187 %3 : Var<java.type:"int"> = var %2 @"x$0"; 188 %4 : java.type:"int" = var.load %3; 189 %5 : java.type:"MethodReferenceTest::A[]" = new %4 @java.ref:"MethodReferenceTest::A[]::(int)"; 190 return %5; 191 }; 192 %6 : Var<java.type:"java.util.function.IntFunction<MethodReferenceTest::A<java.lang.String>[]>"> = var %1 @"aNewArray"; 193 return; 194 }; 195 """) 196 void test8() { 197 IntFunction<A<String>[]> aNewArray = A[]::new; 198 } 199 200 }