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 quotable lambdas. 27 * @modules jdk.incubator.code 28 * @build QuotableSubtypeTest 29 * @build CodeReflectionTester 30 * @run main CodeReflectionTester QuotableSubtypeTest 31 */ 32 33 import jdk.incubator.code.Quotable; 34 import jdk.incubator.code.CodeReflection; 35 import java.util.function.IntBinaryOperator; 36 import java.util.function.IntFunction; 37 import java.util.function.IntSupplier; 38 import java.util.function.IntUnaryOperator; 39 40 public class QuotableSubtypeTest { 41 42 interface QuotableRunnable extends Runnable, Quotable { } 43 44 @IR(""" 45 func @"f" ()java.type:"void" -> { 46 %0 : java.type:"QuotableSubtypeTest$QuotableRunnable" = lambda ()java.type:"void" -> { 47 return; 48 }; 49 return; 50 }; 51 """) 52 static final QuotableRunnable QUOTED_NO_PARAM_VOID = () -> { }; 53 54 interface QuotableIntSupplier extends IntSupplier, Quotable { } 55 56 @IR(""" 57 func @"f" ()java.type:"void" -> { 58 %0 : java.type:"QuotableSubtypeTest$QuotableIntSupplier" = lambda ()java.type:"int" -> { 59 %1 : java.type:"int" = constant @1; 60 return %1; 61 }; 62 return; 63 }; 64 """) 65 static final QuotableIntSupplier QUOTED_NO_PARAM_CONST = () -> 1; 66 67 interface QuotableIntUnaryOperator extends IntUnaryOperator, Quotable { } 68 69 @IR(""" 70 func @"f" ()java.type:"void" -> { 71 %0 : java.type:"QuotableSubtypeTest$QuotableIntUnaryOperator" = lambda (%1 : java.type:"int")java.type:"int" -> { 72 %2 : Var<java.type:"int"> = var %1 @"x"; 73 %3 : java.type:"int" = var.load %2; 74 return %3; 75 }; 76 return; 77 }; 78 """) 79 static final QuotableIntUnaryOperator QUOTED_ID = x -> x; 80 81 interface QuotableIntBinaryOperator extends IntBinaryOperator, Quotable { } 82 83 @IR(""" 84 func @"f" ()java.type:"void" -> { 85 %0 : java.type:"QuotableSubtypeTest$QuotableIntBinaryOperator" = lambda (%1 : java.type:"int", %2 : java.type:"int")java.type:"int" -> { 86 %3 : Var<java.type:"int"> = var %1 @"x"; 87 %4 : Var<java.type:"int"> = var %2 @"y"; 88 %5 : java.type:"int" = var.load %3; 89 %6 : java.type:"int" = var.load %4; 90 %7 : java.type:"int" = add %5 %6; 91 return %7; 92 }; 93 return; 94 }; 95 """) 96 static final QuotableIntBinaryOperator QUOTED_PLUS = (x, y) -> x + y; 97 @IR(""" 98 func @"f" ()java.type:"void" -> { 99 %0 : java.type:"QuotableSubtypeTest$QuotableRunnable" = lambda ()java.type:"void" -> { 100 %1 : java.type:"java.lang.AssertionError" = new @java.ref:"java.lang.AssertionError::()"; 101 throw %1; 102 }; 103 return; 104 }; 105 """) 106 static final QuotableRunnable QUOTED_THROW_NO_PARAM = () -> { throw new AssertionError(); }; 107 108 @IR(""" 109 func @"f" (%0 : Var<java.type:"int">)java.type:"void" -> { 110 %1 : java.type:"QuotableSubtypeTest$QuotableIntUnaryOperator" = lambda (%2 : java.type:"int")java.type:"int" -> { 111 %3 : Var<java.type:"int"> = var %2 @"y"; 112 %4 : java.type:"int" = var.load %0; 113 %5 : java.type:"int" = var.load %3; 114 %6 : java.type:"int" = add %4 %5; 115 return %6; 116 }; 117 return; 118 }; 119 """) 120 static final QuotableIntUnaryOperator QUOTED_CAPTURE_PARAM = new Object() { 121 QuotableIntUnaryOperator captureContext(int x) { 122 return y -> x + y; 123 } 124 }.captureContext(42); 125 126 static class Context { 127 int x, y; 128 129 QuotableIntUnaryOperator capture() { 130 return z -> x + y + z; 131 } 132 } 133 134 @IR(""" 135 func @"f" (%0 : java.type:"QuotableSubtypeTest$Context")java.type:"void" -> { 136 %1 : java.type:"QuotableSubtypeTest$QuotableIntUnaryOperator" = lambda (%2 : java.type:"int")java.type:"int" -> { 137 %3 : Var<java.type:"int"> = var %2 @"z"; 138 %4 : java.type:"int" = field.load %0 @java.ref:"QuotableSubtypeTest$Context::x:int"; 139 %5 : java.type:"int" = field.load %0 @java.ref:"QuotableSubtypeTest$Context::y:int"; 140 %6 : java.type:"int" = add %4 %5; 141 %7 : java.type:"int" = var.load %3; 142 %8 : java.type:"int" = add %6 %7; 143 return %8; 144 }; 145 return; 146 }; 147 """) 148 static final QuotableIntUnaryOperator QUOTED_CAPTURE_FIELD = new Context().capture(); 149 150 @CodeReflection 151 @IR(""" 152 func @"captureParam" (%0 : java.type:"int")java.type:"void" -> { 153 %1 : Var<java.type:"int"> = var %0 @"x"; 154 %2 : java.type:"QuotableSubtypeTest$QuotableIntUnaryOperator" = lambda (%3 : java.type:"int")java.type:"int" -> { 155 %4 : Var<java.type:"int"> = var %3 @"y"; 156 %5 : java.type:"int" = var.load %1; 157 %6 : java.type:"int" = var.load %4; 158 %7 : java.type:"int" = add %5 %6; 159 return %7; 160 }; 161 %8 : Var<java.type:"QuotableSubtypeTest$QuotableIntUnaryOperator"> = var %2 @"op"; 162 return; 163 }; 164 """) 165 static void captureParam(int x) { 166 QuotableIntUnaryOperator op = y -> x + y; 167 } 168 169 int x, y; 170 171 @CodeReflection 172 @IR(""" 173 func @"captureField" (%0 : java.type:"QuotableSubtypeTest")java.type:"void" -> { 174 %1 : java.type:"QuotableSubtypeTest$QuotableIntUnaryOperator" = lambda (%2 : java.type:"int")java.type:"int" -> { 175 %3 : Var<java.type:"int"> = var %2 @"z"; 176 %4 : java.type:"int" = field.load %0 @java.ref:"QuotableSubtypeTest::x:int"; 177 %5 : java.type:"int" = field.load %0 @java.ref:"QuotableSubtypeTest::y:int"; 178 %6 : java.type:"int" = add %4 %5; 179 %7 : java.type:"int" = var.load %3; 180 %8 : java.type:"int" = add %6 %7; 181 return %8; 182 }; 183 %9 : Var<java.type:"QuotableSubtypeTest$QuotableIntUnaryOperator"> = var %1 @"op"; 184 return; 185 }; 186 """) 187 void captureField() { 188 QuotableIntUnaryOperator op = z -> x + y + z; 189 } 190 191 static void m() { } 192 193 @IR(""" 194 func @"f" ()java.type:"void" -> { 195 %0 : java.type:"QuotableSubtypeTest$QuotableRunnable" = lambda ()java.type:"void" -> { 196 invoke @java.ref:"QuotableSubtypeTest::m():void"; 197 return; 198 }; 199 return; 200 }; 201 """) 202 static final QuotableRunnable QUOTED_NO_PARAM_VOID_REF = QuotableSubtypeTest::m; 203 204 static int g(int i) { return i; } 205 206 @IR(""" 207 func @"f" ()java.type:"void" -> { 208 %0 : java.type:"QuotableSubtypeTest$QuotableIntUnaryOperator" = lambda (%1 : java.type:"int")java.type:"int" -> { 209 %2 : Var<java.type:"int"> = var %1 @"x$0"; 210 %3 : java.type:"int" = var.load %2; 211 %4 : java.type:"int" = invoke %3 @java.ref:"QuotableSubtypeTest::g(int):int"; 212 return %4; 213 }; 214 return; 215 }; 216 """) 217 static final QuotableIntUnaryOperator QUOTED_INT_PARAM_INT_RET_REF = QuotableSubtypeTest::g; 218 219 interface QuotableIntFunction<A> extends Quotable, IntFunction<A> { } 220 221 @IR(""" 222 func @"f" ()java.type:"void" -> { 223 %0 : java.type:"QuotableSubtypeTest$QuotableIntFunction<int[]>" = lambda (%1 : java.type:"int")java.type:"int[]" -> { 224 %2 : Var<java.type:"int"> = var %1 @"x$0"; 225 %3 : java.type:"int" = var.load %2; 226 %4 : java.type:"int[]" = new %3 @java.ref:"int[]::(int)"; 227 return %4; 228 }; 229 return; 230 }; 231 """) 232 static final QuotableIntFunction<int[]> QUOTED_INT_PARAM_ARR_RET_REF = int[]::new; 233 234 static class ContextRef { 235 int g(int i) { return i; } 236 237 QuotableIntUnaryOperator capture() { 238 return this::g; 239 } 240 } 241 242 @IR(""" 243 func @"f" (%0 : java.type:"QuotableSubtypeTest$ContextRef")java.type:"void" -> { 244 %1 : java.type:"QuotableSubtypeTest$QuotableIntUnaryOperator" = lambda (%2 : java.type:"int")java.type:"int" -> { 245 %3 : Var<java.type:"int"> = var %2 @"x$0"; 246 %4 : java.type:"int" = var.load %3; 247 %5 : java.type:"int" = invoke %0 %4 @java.ref:"QuotableSubtypeTest$ContextRef::g(int):int"; 248 return %5; 249 }; 250 return; 251 }; 252 """) 253 static final QuotableIntUnaryOperator QUOTED_CAPTURE_THIS_REF = new ContextRef().capture(); 254 }