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 : MethodReferenceTest)void -> { 49 %1 : java.util.function.Consumer<java.lang.String> = lambda (%2 : java.lang.String)void -> { 50 %3 : Var<java.lang.String> = var %2 @"x$0"; 51 %4 : java.lang.String = var.load %3; 52 invoke %4 @"MethodReferenceTest::m_s(java.lang.String)void"; 53 return; 54 }; 55 %5 : Var<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 : MethodReferenceTest)void -> { 66 %1 : java.util.function.BiConsumer<MethodReferenceTest, java.lang.String> = lambda (%2 : MethodReferenceTest, %3 : java.lang.String)void -> { 67 %4 : Var<MethodReferenceTest> = var %2 @"rec$"; 68 %5 : Var<java.lang.String> = var %3 @"x$0"; 69 %6 : MethodReferenceTest = var.load %4; 70 %7 : java.lang.String = var.load %5; 71 invoke %6 %7 @"MethodReferenceTest::m(java.lang.String)void"; 72 return; 73 }; 74 %8 : Var<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 : MethodReferenceTest)void -> { 85 %1 : java.util.function.Consumer<java.lang.String> = lambda (%2 : java.lang.String)void -> { 86 %3 : Var<java.lang.String> = var %2 @"x$0"; 87 %4 : java.lang.String = var.load %3; 88 invoke %0 %4 @"MethodReferenceTest::m(java.lang.String)void"; 89 return; 90 }; 91 %5 : Var<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 : MethodReferenceTest)void -> { 108 %1 : java.lang.String = constant @"s"; 109 %2 : .<MethodReferenceTest, MethodReferenceTest$A<java.lang.String>> = invoke %0 %1 @"MethodReferenceTest::a(java.lang.Object).<MethodReferenceTest, MethodReferenceTest$A>"; 110 %3 : Var<.<MethodReferenceTest, MethodReferenceTest$A<java.lang.String>>> = var %2 @"rec$"; 111 %4 : java.util.function.Function<java.lang.String, java.lang.String> = lambda (%5 : java.lang.String)java.lang.String -> { 112 %6 : Var<java.lang.String> = var %5 @"x$0"; 113 %7 : .<MethodReferenceTest, MethodReferenceTest$A<java.lang.String>> = var.load %3; 114 %8 : java.lang.String = var.load %6; 115 %9 : java.lang.String = invoke %7 %8 @".<MethodReferenceTest, MethodReferenceTest$A>::m(java.lang.Object)java.lang.Object"; 116 return %9; 117 }; 118 %10 : Var<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 : MethodReferenceTest)void -> { 129 %1 : java.io.PrintStream = field.load @"java.lang.System::out()java.io.PrintStream"; 130 %2 : Var<java.io.PrintStream> = var %1 @"rec$"; 131 %3 : java.util.function.Consumer<java.lang.String> = lambda (%4 : java.lang.String)void -> { 132 %5 : Var<java.lang.String> = var %4 @"x$0"; 133 %6 : java.io.PrintStream = var.load %2; 134 %7 : java.lang.String = var.load %5; 135 invoke %6 %7 @"java.io.PrintStream::println(java.lang.String)void"; 136 return; 137 }; 138 %8 : Var<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 : MethodReferenceTest)void -> { 153 %1 : java.util.function.Function<java.lang.Integer, MethodReferenceTest$X> = lambda (%2 : java.lang.Integer)MethodReferenceTest$X -> { 154 %3 : Var<java.lang.Integer> = var %2 @"x$0"; 155 %4 : java.lang.Integer = var.load %3; 156 %5 : int = invoke %4 @"java.lang.Integer::intValue()int"; 157 %6 : MethodReferenceTest$X = new %5 @"func<MethodReferenceTest$X, int>"; 158 return %6; 159 }; 160 %7 : Var<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 : MethodReferenceTest)void -> { 171 %1 : java.util.function.Supplier<.<MethodReferenceTest, MethodReferenceTest$A<java.lang.String>>> = lambda ().<MethodReferenceTest, MethodReferenceTest$A<java.lang.String>> -> { 172 %2 : .<MethodReferenceTest, MethodReferenceTest$A<java.lang.String>> = new %0 @"func<.<MethodReferenceTest, MethodReferenceTest$A>, MethodReferenceTest>"; 173 return %2; 174 }; 175 %3 : Var<java.util.function.Supplier<.<MethodReferenceTest, 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 : MethodReferenceTest)void -> { 186 %1 : java.util.function.IntFunction<.<MethodReferenceTest, MethodReferenceTest$A<java.lang.String>>[]> = lambda (%2 : int).<MethodReferenceTest, MethodReferenceTest$A<java.lang.String>>[] -> { 187 %3 : Var<int> = var %2 @"x$0"; 188 %4 : int = var.load %3; 189 %5 : .<MethodReferenceTest, MethodReferenceTest$A>[] = new %4 @"func<.<MethodReferenceTest, MethodReferenceTest$A>[], int>"; 190 return %5; 191 }; 192 %6 : Var<java.util.function.IntFunction<.<MethodReferenceTest, 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 }