LambdaMetafactory (Java SE 9 & JDK 9 ) (original) (raw)

public final class LambdaMetafactory
extends Object
Methods to facilitate the creation of simple "function objects" that implement one or more interfaces by delegation to a provided MethodHandle, possibly after type adaptation and partial evaluation of arguments. These methods are typically used as bootstrap methods for invokedynamic call sites, to support the lambda expression and method reference expression features of the Java Programming Language.
Indirect access to the behavior specified by the provided MethodHandle proceeds in order through three phases:

| --------- | --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| Primitive | Primitive | Q can be converted to S via a primitive widening conversion | None |
| Primitive | Reference | S is a supertype of the Wrapper(Q) | Cast from Wrapper(Q) to S |
| Reference | Primitive | for parameter types: Q is a primitive wrapper and Primitive(Q) can be widened to Sfor return types: If Q is a primitive wrapper, check that Primitive(Q) can be widened to S | If Q is not a primitive wrapper, cast Q to the base Wrapper(S); for example Number for numeric types |
| Reference | Reference | for parameter types: S is a supertype of Qfor return types: none | Cast from Q to S |
API Note:
These linkage methods are designed to support the evaluation of lambda expressions and method references in the Java Language. For every lambda expressions or method reference in the source code, there is a target type which is a functional interface. Evaluating a lambda expression produces an object of its target type. The recommended mechanism for evaluating lambda expressions is to desugar the lambda body to a method, invoke an invokedynamic call site whose static argument list describes the sole method of the functional interface and the desugared implementation method, and returns an object (the lambda object) that implements the target type. (For method references, the implementation method is simply the referenced method; no desugaring is needed.)
The argument list of the implementation method and the argument list of the interface method(s) may differ in several ways. The implementation methods may have additional arguments to accommodate arguments captured by the lambda expression; there may also be differences resulting from permitted adaptations of arguments, such as casting, boxing, unboxing, and primitive widening. (Varargs adaptations are not handled by the metafactories; these are expected to be handled by the caller.)
Invokedynamic call sites have two argument lists: a static argument list and a dynamic argument list. The static argument list is stored in the constant pool; the dynamic argument is pushed on the operand stack at capture time. The bootstrap method has access to the entire static argument list (which in this case, includes information describing the implementation method, the target interface, and the target interface method(s)), as well as a method signature describing the number and static types (but not the values) of the dynamic arguments and the static return type of the invokedynamic site.
Implementation Note:
The implementation method is described with a method handle. In theory, any method handle could be used. Currently supported are direct method handles representing invocation of virtual, interface, constructor and static methods.
Since:
1.8