1 /* 2 * Copyright (c) 2012, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.lang.invoke; 27 28 import java.io.Serializable; 29 import java.util.Arrays; 30 import java.lang.reflect.Array; 31 import java.util.Objects; 32 33 /** 34 * <p>Methods to facilitate the creation of simple "function objects" that 35 * implement one or more interfaces by delegation to a provided {@link MethodHandle}, 36 * possibly after type adaptation and partial evaluation of arguments. These 37 * methods are typically used as <em>bootstrap methods</em> for {@code invokedynamic} 38 * call sites, to support the <em>lambda expression</em> and <em>method 39 * reference expression</em> features of the Java Programming Language. 40 * 41 * <p>Indirect access to the behavior specified by the provided {@code MethodHandle} 42 * proceeds in order through three phases: 43 * <ul> 44 * <li><p><em>Linkage</em> occurs when the methods in this class are invoked. 45 * They take as arguments an interface to be implemented (typically a 46 * <em>functional interface</em>, one with a single abstract method), a 47 * name and signature of a method from that interface to be implemented, a 48 * {@linkplain MethodHandleInfo direct method handle} describing the desired 49 * implementation behavior for that method, and possibly other additional 50 * metadata, and produce a {@link CallSite} whose target can be used to 51 * create suitable function objects. 52 * 53 * <p>Linkage may involve dynamically loading a new class that implements 54 * the target interface, or re-using a suitable existing class. 55 * 56 * <p>The {@code CallSite} can be considered a "factory" for function 57 * objects and so these linkage methods are referred to as 58 * "metafactories".</li> 59 * 60 * <li><p><em>Capture</em> occurs when the {@code CallSite}'s target is 61 * invoked, typically through an {@code invokedynamic} call site, 62 * producing a function object. This may occur many times for 63 * a single factory {@code CallSite}. 64 * 65 * <p>If the behavior {@code MethodHandle} has additional parameters beyond 66 * those of the specified interface method, these are referred to as 67 * <em>captured parameters</em>, which must be provided as arguments to the 68 * {@code CallSite} target. The expected number and types of captured 69 * parameters are determined during linkage. 70 * 71 * <p>Capture may involve allocation of a new function object, or may return 72 * a suitable existing function object. The identity of a function object 73 * produced by capture is unpredictable, and therefore identity-sensitive 74 * operations (such as reference equality, object locking, and {@code 75 * System.identityHashCode()}) may produce different results in different 76 * implementations, or even upon different invocations in the same 77 * implementation.</li> 78 * 79 * <li><p><em>Invocation</em> occurs when an implemented interface method is 80 * invoked on a function object. This may occur many times for a single 81 * function object. The method referenced by the implementation 82 * {@code MethodHandle} is invoked, passing to it the captured arguments and 83 * the invocation arguments. The result of the method is returned. 84 * </li> 85 * </ul> 86 * 87 * <p>It is sometimes useful to restrict the set of inputs or results permitted 88 * at invocation. For example, when the generic interface {@code Predicate<T>} 89 * is parameterized as {@code Predicate<String>}, the input must be a 90 * {@code String}, even though the method to implement allows any {@code Object}. 91 * At linkage time, an additional {@link MethodType} parameter describes the 92 * "dynamic" method type; on invocation, the arguments and eventual result 93 * are checked against this {@code MethodType}. 94 * 95 * <p>This class provides two forms of linkage methods: a standard version 96 * ({@link #metafactory(MethodHandles.Lookup, String, MethodType, MethodType, MethodHandle, MethodType)}) 97 * using an optimized protocol, and an alternate version 98 * {@link #altMetafactory(MethodHandles.Lookup, String, MethodType, Object...)}). 99 * The alternate version is a generalization of the standard version, providing 100 * additional control over the behavior of the generated function objects via 101 * flags and additional arguments. The alternate version adds the ability to 102 * manage the following attributes of function objects: 103 * 104 * <ul> 105 * <li><em>Multiple methods.</em> It is sometimes useful to implement multiple 106 * variations of the method signature, involving argument or return type 107 * adaptation. This occurs when multiple distinct VM signatures for a method 108 * are logically considered to be the same method by the language. The 109 * flag {@code FLAG_BRIDGES} indicates that a list of additional 110 * {@code MethodType}s will be provided, each of which will be implemented 111 * by the resulting function object. These methods will share the same 112 * name and instantiated type.</li> 113 * 114 * <li><em>Multiple interfaces.</em> If needed, more than one interface 115 * can be implemented by the function object. (These additional interfaces 116 * are typically marker interfaces with no methods.) The flag {@code FLAG_MARKERS} 117 * indicates that a list of additional interfaces will be provided, each of 118 * which should be implemented by the resulting function object.</li> 119 * 120 * <li><em>Serializability.</em> The generated function objects do not 121 * generally support serialization. If desired, {@code FLAG_SERIALIZABLE} 122 * can be used to indicate that the function objects should be serializable. 123 * Serializable function objects will use, as their serialized form, 124 * instances of the class {@code SerializedLambda}, which requires additional 125 * assistance from the capturing class (the class described by the 126 * {@link MethodHandles.Lookup} parameter {@code caller}); see 127 * {@link SerializedLambda} for details.</li> 128 * </ul> 129 * 130 * <p>Assume the linkage arguments are as follows: 131 * <ul> 132 * <li>{@code factoryType} (describing the {@code CallSite} signature) has 133 * K parameters of types (D1..Dk) and return type Rd;</li> 134 * <li>{@code interfaceMethodType} (describing the implemented method type) has N 135 * parameters, of types (U1..Un) and return type Ru;</li> 136 * <li>{@code implementation} (the {@code MethodHandle} providing the 137 * implementation) has M parameters, of types (A1..Am) and return type Ra 138 * (if the method describes an instance method, the method type of this 139 * method handle already includes an extra first argument corresponding to 140 * the receiver);</li> 141 * <li>{@code dynamicMethodType} (allowing restrictions on invocation) 142 * has N parameters, of types (T1..Tn) and return type Rt.</li> 143 * </ul> 144 * 145 * <p>Then the following linkage invariants must hold: 146 * <ul> 147 * <li>{@code interfaceMethodType} and {@code dynamicMethodType} have the same 148 * arity N, and for i=1..N, Ti and Ui are the same type, or Ti and Ui are 149 * both reference types and Ti is a subtype of Ui</li> 150 * <li>Either Rt and Ru are the same type, or both are reference types and 151 * Rt is a subtype of Ru</li> 152 * <li>K + N = M</li> 153 * <li>For i=1..K, Di = Ai</li> 154 * <li>For i=1..N, Ti is adaptable to Aj, where j=i+k</li> 155 * <li>The return type Rt is void, or the return type Ra is not void and is 156 * adaptable to Rt</li> 157 * </ul> 158 * 159 * <p>Further, at capture time, if {@code implementation} corresponds to an instance 160 * method, and there are any capture arguments ({@code K > 0}), then the first 161 * capture argument (corresponding to the receiver) must be non-null. 162 * 163 * <p>A type Q is considered adaptable to S as follows: 164 * <table class="striped"> 165 * <caption style="display:none">adaptable types</caption> 166 * <thead> 167 * <tr><th scope="col">Q</th><th scope="col">S</th><th scope="col">Link-time checks</th><th scope="col">Invocation-time checks</th></tr> 168 * </thead> 169 * <tbody> 170 * <tr> 171 * <th scope="row">Primitive</th><th scope="row">Primitive</th> 172 * <td>Q can be converted to S via a primitive widening conversion</td> 173 * <td>None</td> 174 * </tr> 175 * <tr> 176 * <th scope="row">Primitive</th><th scope="row">Reference</th> 177 * <td>S is a supertype of the Wrapper(Q)</td> 178 * <td>Cast from Wrapper(Q) to S</td> 179 * </tr> 180 * <tr> 181 * <th scope="row">Reference</th><th scope="row">Primitive</th> 182 * <td>for parameter types: Q is a primitive wrapper and Primitive(Q) 183 * can be widened to S 184 * <br>for return types: If Q is a primitive wrapper, check that 185 * Primitive(Q) can be widened to S</td> 186 * <td>If Q is not a primitive wrapper, cast Q to the base Wrapper(S); 187 * for example Number for numeric types</td> 188 * </tr> 189 * <tr> 190 * <th scope="row">Reference</th><th scope="row">Reference</th> 191 * <td>for parameter types: S is a supertype of Q 192 * <br>for return types: none</td> 193 * <td>Cast from Q to S</td> 194 * </tr> 195 * </tbody> 196 * </table> 197 * 198 * @apiNote These linkage methods are designed to support the evaluation 199 * of <em>lambda expressions</em> and <em>method references</em> in the Java 200 * Language. For every lambda expressions or method reference in the source code, 201 * there is a target type which is a functional interface. Evaluating a lambda 202 * expression produces an object of its target type. The recommended mechanism 203 * for evaluating lambda expressions is to desugar the lambda body to a method, 204 * invoke an invokedynamic call site whose static argument list describes the 205 * sole method of the functional interface and the desugared implementation 206 * method, and returns an object (the lambda object) that implements the target 207 * type. (For method references, the implementation method is simply the 208 * referenced method; no desugaring is needed.) 209 * 210 * <p>The argument list of the implementation method and the argument list of 211 * the interface method(s) may differ in several ways. The implementation 212 * methods may have additional arguments to accommodate arguments captured by 213 * the lambda expression; there may also be differences resulting from permitted 214 * adaptations of arguments, such as casting, boxing, unboxing, and primitive 215 * widening. (Varargs adaptations are not handled by the metafactories; these are 216 * expected to be handled by the caller.) 217 * 218 * <p>Invokedynamic call sites have two argument lists: a static argument list 219 * and a dynamic argument list. The static argument list is stored in the 220 * constant pool; the dynamic argument is pushed on the operand stack at capture 221 * time. The bootstrap method has access to the entire static argument list 222 * (which in this case, includes information describing the implementation method, 223 * the target interface, and the target interface method(s)), as well as a 224 * method signature describing the number and static types (but not the values) 225 * of the dynamic arguments and the static return type of the invokedynamic site. 226 * 227 * <p>The implementation method is described with a direct method handle 228 * referencing a method or constructor. In theory, any method handle could be 229 * used, but this is not compatible with some implementation techniques and 230 * would complicate the work implementations must do. 231 * 232 * <p>Uses besides evaluation of lambda expressions and method references are 233 * unintended. These linkage methods may change their unspecified behaviors at 234 * any time to better suit the Java language features they were designed to 235 * support, and such changes may impact unintended uses. Unintended uses of 236 * these linkage methods may lead to resource leaks, or other unspecified 237 * negative effects. 238 * 239 * @implNote In the reference implementation, the classes implementing the created 240 * function objects are strongly reachable from the defining class loader of the 241 * caller, like classes and interfaces in Java source code. This technique 242 * reduces heap memory use, but as a consequence, the implementation classes can 243 * be unloaded only if the caller class can be unloaded. In particular, if the 244 * caller is a {@linkplain MethodHandles.Lookup.ClassOption#STRONG weak hidden 245 * class}, the implementation class, a strong hidden class, may not be unloaded 246 * even if the caller may be unloaded. 247 * 248 * @since 1.8 249 */ 250 public final class LambdaMetafactory { 251 252 private LambdaMetafactory() {} 253 254 /** Flag for {@link #altMetafactory} indicating the lambda object 255 * must be serializable */ 256 public static final int FLAG_SERIALIZABLE = 1 << 0; 257 258 /** 259 * Flag for {@link #altMetafactory} indicating the lambda object implements 260 * other interfaces besides {@code Serializable} 261 */ 262 public static final int FLAG_MARKERS = 1 << 1; 263 264 /** 265 * Flag for alternate metafactories indicating the lambda object requires 266 * additional methods that invoke the {@code implementation} 267 */ 268 public static final int FLAG_BRIDGES = 1 << 2; 269 270 /** Flag for {@link #altMetafactory} indicating the lambda object 271 * must be a {@code Quotable} object, inspectable using code reflection. */ 272 public static final int FLAG_QUOTABLE = 1 << 3; 273 274 private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0]; 275 private static final MethodType[] EMPTY_MT_ARRAY = new MethodType[0]; 276 277 // LambdaMetafactory bootstrap methods are startup sensitive, and may be 278 // special cased in java.lang.invoke.BootstrapMethodInvoker to ensure 279 // methods are invoked with exact type information to avoid generating 280 // code for runtime checks. Take care any changes or additions here are 281 // reflected there as appropriate. 282 283 /** 284 * Facilitates the creation of simple "function objects" that implement one 285 * or more interfaces by delegation to a provided {@link MethodHandle}, 286 * after appropriate type adaptation and partial evaluation of arguments. 287 * Typically used as a <em>bootstrap method</em> for {@code invokedynamic} 288 * call sites, to support the <em>lambda expression</em> and <em>method 289 * reference expression</em> features of the Java Programming Language. 290 * 291 * <p>This is the standard, streamlined metafactory; additional flexibility 292 * is provided by {@link #altMetafactory(MethodHandles.Lookup, String, MethodType, Object...)}. 293 * A general description of the behavior of this method is provided 294 * {@link LambdaMetafactory above}. 295 * 296 * <p>When the target of the {@code CallSite} returned from this method is 297 * invoked, the resulting function objects are instances of a class which 298 * implements the interface named by the return type of {@code factoryType}, 299 * declares a method with the name given by {@code interfaceMethodName} and the 300 * signature given by {@code interfaceMethodType}. It may also override additional 301 * methods from {@code Object}. 302 * 303 * @param caller Represents a lookup context with the accessibility 304 * privileges of the caller. Specifically, the lookup context 305 * must have {@linkplain MethodHandles.Lookup#hasFullPrivilegeAccess() 306 * full privilege access}. 307 * When used with {@code invokedynamic}, this is stacked 308 * automatically by the VM. 309 * @param interfaceMethodName The name of the method to implement. When used with 310 * {@code invokedynamic}, this is provided by the 311 * {@code NameAndType} of the {@code InvokeDynamic} 312 * structure and is stacked automatically by the VM. 313 * @param factoryType The expected signature of the {@code CallSite}. The 314 * parameter types represent the types of capture variables; 315 * the return type is the interface to implement. When 316 * used with {@code invokedynamic}, this is provided by 317 * the {@code NameAndType} of the {@code InvokeDynamic} 318 * structure and is stacked automatically by the VM. 319 * @param interfaceMethodType Signature and return type of method to be 320 * implemented by the function object. 321 * @param implementation A direct method handle describing the implementation 322 * method which should be called (with suitable adaptation 323 * of argument types and return types, and with captured 324 * arguments prepended to the invocation arguments) at 325 * invocation time. 326 * @param dynamicMethodType The signature and return type that should 327 * be enforced dynamically at invocation time. 328 * In simple use cases this is the same as 329 * {@code interfaceMethodType}. 330 * @return a CallSite whose target can be used to perform capture, generating 331 * instances of the interface named by {@code factoryType} 332 * @throws LambdaConversionException If {@code caller} does not have full privilege 333 * access, or if {@code interfaceMethodName} is not a valid JVM 334 * method name, or if the return type of {@code factoryType} is not 335 * an interface, or if {@code implementation} is not a direct method 336 * handle referencing a method or constructor, or if the linkage 337 * invariants are violated, as defined {@link LambdaMetafactory above}. 338 * @throws NullPointerException If any argument is {@code null}. 339 */ 340 public static CallSite metafactory(MethodHandles.Lookup caller, 341 String interfaceMethodName, 342 MethodType factoryType, 343 MethodType interfaceMethodType, 344 MethodHandle implementation, 345 MethodType dynamicMethodType) 346 throws LambdaConversionException { 347 AbstractValidatingLambdaMetafactory mf; 348 mf = new InnerClassLambdaMetafactory(Objects.requireNonNull(caller), 349 Objects.requireNonNull(factoryType), 350 Objects.requireNonNull(interfaceMethodName), 351 Objects.requireNonNull(interfaceMethodType), 352 Objects.requireNonNull(implementation), 353 Objects.requireNonNull(dynamicMethodType), 354 false, 355 EMPTY_CLASS_ARRAY, 356 EMPTY_MT_ARRAY, 357 null); 358 mf.validateMetafactoryArgs(); 359 return mf.buildCallSite(); 360 } 361 362 /** 363 * Facilitates the creation of simple "function objects" that implement one 364 * or more interfaces by delegation to a provided {@link MethodHandle}, 365 * after appropriate type adaptation and partial evaluation of arguments. 366 * Typically used as a <em>bootstrap method</em> for {@code invokedynamic} 367 * call sites, to support the <em>lambda expression</em> and <em>method 368 * reference expression</em> features of the Java Programming Language. 369 * 370 * <p>This is the general, more flexible metafactory; a streamlined version 371 * is provided by {@link #metafactory(java.lang.invoke.MethodHandles.Lookup, 372 * String, MethodType, MethodType, MethodHandle, MethodType)}. 373 * A general description of the behavior of this method is provided 374 * {@link LambdaMetafactory above}. 375 * 376 * <p>The argument list for this method includes three fixed parameters, 377 * corresponding to the parameters automatically stacked by the VM for the 378 * bootstrap method in an {@code invokedynamic} invocation, and an {@code Object[]} 379 * parameter that contains additional parameters. The declared argument 380 * list for this method is: 381 * 382 * <pre>{@code 383 * CallSite altMetafactory(MethodHandles.Lookup caller, 384 * String interfaceMethodName, 385 * MethodType factoryType, 386 * Object... args) 387 * }</pre> 388 * 389 * <p>but it behaves as if the argument list is as follows: 390 * 391 * <pre>{@code 392 * CallSite altMetafactory(MethodHandles.Lookup caller, 393 * String interfaceMethodName, 394 * MethodType factoryType, 395 * MethodType interfaceMethodType, 396 * MethodHandle implementation, 397 * MethodType dynamicMethodType, 398 * int flags, 399 * int altInterfaceCount, // IF flags has MARKERS set 400 * Class... altInterfaces, // IF flags has MARKERS set 401 * int altMethodCount, // IF flags has BRIDGES set 402 * MethodType... altMethods // IF flags has BRIDGES set 403 * MethodHandle quotableField // IF flags has QUOTABLE set 404 * ) 405 * }</pre> 406 * 407 * <p>Arguments that appear in the argument list for 408 * {@link #metafactory(MethodHandles.Lookup, String, MethodType, MethodType, MethodHandle, MethodType)} 409 * have the same specification as in that method. The additional arguments 410 * are interpreted as follows: 411 * <ul> 412 * <li>{@code flags} indicates additional options; this is a bitwise 413 * OR of desired flags. Defined flags are {@link #FLAG_BRIDGES}, 414 * {@link #FLAG_MARKERS}, and {@link #FLAG_SERIALIZABLE}.</li> 415 * <li>{@code altInterfaceCount} is the number of additional interfaces 416 * the function object should implement, and is present if and only if the 417 * {@code FLAG_MARKERS} flag is set.</li> 418 * <li>{@code altInterfaces} is a variable-length list of additional 419 * interfaces to implement, whose length equals {@code altInterfaceCount}, 420 * and is present if and only if the {@code FLAG_MARKERS} flag is set.</li> 421 * <li>{@code altMethodCount} is the number of additional method signatures 422 * the function object should implement, and is present if and only if 423 * the {@code FLAG_BRIDGES} flag is set.</li> 424 * <li>{@code altMethods} is a variable-length list of additional 425 * methods signatures to implement, whose length equals {@code altMethodCount}, 426 * and is present if and only if the {@code FLAG_BRIDGES} flag is set.</li> 427 * <li>{@code quotableField} is a 428 * {@linkplain MethodHandles.Lookup#findGetter(Class, String, Class) getter} method handle 429 * that is used to retrieve the string representation of the quotable lambda's associated 430 * intermediate representation.</li> 431 * </ul> 432 * 433 * <p>Each class named by {@code altInterfaces} is subject to the same 434 * restrictions as {@code Rd}, the return type of {@code factoryType}, 435 * as described {@link LambdaMetafactory above}. Each {@code MethodType} 436 * named by {@code altMethods} is subject to the same restrictions as 437 * {@code interfaceMethodType}, as described {@link LambdaMetafactory above}. 438 * 439 * <p>When FLAG_SERIALIZABLE is set in {@code flags}, the function objects 440 * will implement {@code Serializable}, and will have a {@code writeReplace} 441 * method that returns an appropriate {@link SerializedLambda}. The 442 * {@code caller} class must have an appropriate {@code $deserializeLambda$} 443 * method, as described in {@link SerializedLambda}. 444 * 445 * <p>When FLAG_QUOTABLE is set in {@code flags}, the function objects 446 * will implement {@code Quotable}. 447 * 448 * <p>When the target of the {@code CallSite} returned from this method is 449 * invoked, the resulting function objects are instances of a class with 450 * the following properties: 451 * <ul> 452 * <li>The class implements the interface named by the return type 453 * of {@code factoryType} and any interfaces named by {@code altInterfaces}</li> 454 * <li>The class declares methods with the name given by {@code interfaceMethodName}, 455 * and the signature given by {@code interfaceMethodType} and additional signatures 456 * given by {@code altMethods}</li> 457 * <li>The class may override methods from {@code Object}, and may 458 * implement methods related to serialization.</li> 459 * </ul> 460 * 461 * @param caller Represents a lookup context with the accessibility 462 * privileges of the caller. Specifically, the lookup context 463 * must have {@linkplain MethodHandles.Lookup#hasFullPrivilegeAccess() 464 * full privilege access}. 465 * When used with {@code invokedynamic}, this is stacked 466 * automatically by the VM. 467 * @param interfaceMethodName The name of the method to implement. When used with 468 * {@code invokedynamic}, this is provided by the 469 * {@code NameAndType} of the {@code InvokeDynamic} 470 * structure and is stacked automatically by the VM. 471 * @param factoryType The expected signature of the {@code CallSite}. The 472 * parameter types represent the types of capture variables; 473 * the return type is the interface to implement. When 474 * used with {@code invokedynamic}, this is provided by 475 * the {@code NameAndType} of the {@code InvokeDynamic} 476 * structure and is stacked automatically by the VM. 477 * @param args An array of {@code Object} containing the required 478 * arguments {@code interfaceMethodType}, {@code implementation}, 479 * {@code dynamicMethodType}, {@code flags}, and any 480 * optional arguments, as described above 481 * @return a CallSite whose target can be used to perform capture, generating 482 * instances of the interface named by {@code factoryType} 483 * @throws LambdaConversionException If {@code caller} does not have full privilege 484 * access, or if {@code interfaceMethodName} is not a valid JVM 485 * method name, or if the return type of {@code factoryType} is not 486 * an interface, or if any of {@code altInterfaces} is not an 487 * interface, or if {@code implementation} is not a direct method 488 * handle referencing a method or constructor, or if the linkage 489 * invariants are violated, as defined {@link LambdaMetafactory above}. 490 * @throws NullPointerException If any argument, or any component of {@code args}, 491 * is {@code null}. 492 * @throws IllegalArgumentException If the number or types of the components 493 * of {@code args} do not follow the above rules, or if 494 * {@code altInterfaceCount} or {@code altMethodCount} are negative 495 * integers. 496 */ 497 public static CallSite altMetafactory(MethodHandles.Lookup caller, 498 String interfaceMethodName, 499 MethodType factoryType, 500 Object... args) 501 throws LambdaConversionException { 502 Objects.requireNonNull(caller); 503 Objects.requireNonNull(interfaceMethodName); 504 Objects.requireNonNull(factoryType); 505 Objects.requireNonNull(args); 506 int argIndex = 0; 507 MethodType interfaceMethodType = extractArg(args, argIndex++, MethodType.class); 508 MethodHandle implementation = extractArg(args, argIndex++, MethodHandle.class); 509 MethodType dynamicMethodType = extractArg(args, argIndex++, MethodType.class); 510 int flags = extractArg(args, argIndex++, Integer.class); 511 Class<?>[] altInterfaces = EMPTY_CLASS_ARRAY; 512 MethodType[] altMethods = EMPTY_MT_ARRAY; 513 // Getter that returns the op of a Quotable instance 514 MethodHandle quotableOpGetter = null; 515 if ((flags & FLAG_MARKERS) != 0) { 516 int altInterfaceCount = extractArg(args, argIndex++, Integer.class); 517 if (altInterfaceCount < 0) { 518 throw new IllegalArgumentException("negative argument count"); 519 } 520 if (altInterfaceCount > 0) { 521 altInterfaces = extractArgs(args, argIndex, Class.class, altInterfaceCount); 522 argIndex += altInterfaceCount; 523 } 524 } 525 if ((flags & FLAG_BRIDGES) != 0) { 526 int altMethodCount = extractArg(args, argIndex++, Integer.class); 527 if (altMethodCount < 0) { 528 throw new IllegalArgumentException("negative argument count"); 529 } 530 if (altMethodCount > 0) { 531 altMethods = extractArgs(args, argIndex, MethodType.class, altMethodCount); 532 argIndex += altMethodCount; 533 } 534 } 535 if ((flags & FLAG_QUOTABLE) != 0) { 536 quotableOpGetter = extractArg(args, argIndex++, MethodHandle.class); 537 altInterfaces = Arrays.copyOf(altInterfaces, altInterfaces.length + 1); 538 altInterfaces[altInterfaces.length-1] = InnerClassLambdaMetafactory.CodeReflectionSupport.QUOTABLE_CLASS; 539 } 540 if (argIndex < args.length) { 541 throw new IllegalArgumentException("too many arguments"); 542 } 543 544 boolean isSerializable = ((flags & FLAG_SERIALIZABLE) != 0); 545 if (isSerializable) { 546 boolean foundSerializableSupertype = Serializable.class.isAssignableFrom(factoryType.returnType()); 547 for (Class<?> c : altInterfaces) 548 foundSerializableSupertype |= Serializable.class.isAssignableFrom(c); 549 if (!foundSerializableSupertype) { 550 altInterfaces = Arrays.copyOf(altInterfaces, altInterfaces.length + 1); 551 altInterfaces[altInterfaces.length-1] = Serializable.class; 552 } 553 } 554 555 AbstractValidatingLambdaMetafactory mf 556 = new InnerClassLambdaMetafactory(caller, 557 factoryType, 558 interfaceMethodName, 559 interfaceMethodType, 560 implementation, 561 dynamicMethodType, 562 isSerializable, 563 altInterfaces, 564 altMethods, 565 quotableOpGetter); 566 mf.validateMetafactoryArgs(); 567 return mf.buildCallSite(); 568 } 569 570 private static <T> T extractArg(Object[] args, int index, Class<T> type) { 571 if (index >= args.length) { 572 throw new IllegalArgumentException("missing argument"); 573 } 574 Object result = Objects.requireNonNull(args[index]); 575 if (!type.isInstance(result)) { 576 throw new IllegalArgumentException("argument has wrong type"); 577 } 578 return type.cast(result); 579 } 580 581 private static <T> T[] extractArgs(Object[] args, int index, Class<T> type, int count) { 582 @SuppressWarnings("unchecked") 583 T[] result = (T[]) Array.newInstance(type, count); 584 for (int i = 0; i < count; i++) { 585 result[i] = extractArg(args, index + i, type); 586 } 587 return result; 588 } 589 590 }