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:
Linkage occurs when the methods in this class are invoked.
They take as arguments an interface to be implemented (typically a
functional interface, one with a single abstract method), a
name and signature of a method from that interface to be implemented, a
direct method handle describing the desired
implementation behavior for that method, and possibly other additional
metadata, and produce a CallSite
whose target can be used to
create suitable function objects.
Linkage may involve dynamically loading a new class that implements the target interface, or re-using a suitable existing class.
The CallSite
can be considered a "factory" for function
objects and so these linkage methods are referred to as
"metafactories".
Capture occurs when the CallSite
's target is
invoked, typically through an invokedynamic
call site,
producing a function object. This may occur many times for
a single factory CallSite
.
If the behavior MethodHandle
has additional parameters beyond
those of the specified interface method, these are referred to as
captured parameters, which must be provided as arguments to the
CallSite
target. The expected number and types of captured
parameters are determined during linkage.
Capture may involve allocation of a new function object, or may return
a suitable existing function object. The identity of a function object
produced by capture is unpredictable, and therefore identity-sensitive
operations (such as reference equality, object locking, and System.identityHashCode()
) may produce different results in different
implementations, or even upon different invocations in the same
implementation.
Invocation occurs when an implemented interface method is
invoked on a function object. This may occur many times for a single
function object. The method referenced by the implementation
MethodHandle
is invoked, passing to it the captured arguments and
the invocation arguments. The result of the method is returned.
It is sometimes useful to restrict the set of inputs or results permitted
at invocation. For example, when the generic interface Predicate<T>
is parameterized as Predicate<String>
, the input must be a
String
, even though the method to implement allows any Object
.
At linkage time, an additional MethodType
parameter describes the
"dynamic" method type; on invocation, the arguments and eventual result
are checked against this MethodType
.
This class provides two forms of linkage methods: a standard version
(metafactory(MethodHandles.
)
using an optimized protocol, and an alternate version
altMetafactory(MethodHandles.
).
The alternate version is a generalization of the standard version, providing
additional control over the behavior of the generated function objects via
flags and additional arguments. The alternate version adds the ability to
manage the following attributes of function objects:
FLAG_BRIDGES
indicates that a list of additional
MethodType
s will be provided, each of which will be implemented
by the resulting function object. These methods will share the same
name and instantiated type.FLAG_MARKERS
indicates that a list of additional interfaces will be provided, each of
which should be implemented by the resulting function object.FLAG_SERIALIZABLE
can be used to indicate that the function objects should be serializable.
Serializable function objects will use, as their serialized form,
instances of the class SerializedLambda
, which requires additional
assistance from the capturing class (the class described by the
MethodHandles.Lookup
parameter caller
); see
SerializedLambda
for details.Assume the linkage arguments are as follows:
factoryType
(describing the CallSite
signature) has
K parameters of types (D1..Dk) and return type Rd;interfaceMethodType
(describing the implemented method type) has N
parameters, of types (U1..Un) and return type Ru;implementation
(the MethodHandle
providing the
implementation) has M parameters, of types (A1..Am) and return type Ra
(if the method describes an instance method, the method type of this
method handle already includes an extra first argument corresponding to
the receiver);dynamicMethodType
(allowing restrictions on invocation)
has N parameters, of types (T1..Tn) and return type Rt.Then the following linkage invariants must hold:
interfaceMethodType
and dynamicMethodType
have the same
arity N, and for i=1..N, Ti and Ui are the same type, or Ti and Ui are
both reference types and Ti is a subtype of UiFurther, at capture time, if implementation
corresponds to an instance
method, and there are any capture arguments (K > 0
), then the first
capture argument (corresponding to the receiver) must be non-null.
A type Q is considered adaptable to S as follows:
Q | S | Link-time checks | Invocation-time checks |
---|---|---|---|
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 S
for 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 Q
for 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.
The implementation method is described with a direct method handle referencing a method or constructor. In theory, any method handle could be used, but this is not compatible with some implementation techniques and would complicate the work implementations must do.
Modifier and Type | Field and Description |
---|---|
private static final Class | |
private static final MethodType[] | |
public static final int | FLAG_BRIDGES
Flag for alternate metafactories indicating the lambda object requires
additional methods that invoke the |
public static final int | FLAG_MARKERS
Flag for |
public static final int | FLAG_SERIALIZABLE
Flag for |
Access | Constructor and Description |
---|---|
private |
Modifier and Type | Method and Description |
---|---|
public static CallSite | Returns: a CallSite whose target can be used to perform capture, generating instances of the interface named byfactoryType Represents a lookup context with the accessibility
privileges of the caller. Specifically, the lookup context
must have full privilege access.
When used with caller,invokedynamic , this is stacked
automatically by the VM.The name of the method to implement. When used with
interfaceMethodName, MethodType invokedynamic , this is provided by the
NameAndType of the InvokeDynamic
structure and is stacked automatically by the VM.The expected signature of the factoryType, Object... CallSite . The
parameter types represent the types of capture variables;
the return type is the interface to implement. When
used with invokedynamic , this is provided by
the NameAndType of the InvokeDynamic
structure and is stacked automatically by the VM.An array of args)Object containing the required
arguments interfaceMethodType , implementation ,
dynamicMethodType , flags , and any
optional arguments, as described aboveFacilitates the creation of simple "function objects" that implement one
or more interfaces by delegation to a provided |
private static <T> T | |
private static <T> T[] | |
public static CallSite | Returns: a CallSite whose target can be used to perform capture, generating instances of the interface named byfactoryType Represents a lookup context with the accessibility
privileges of the caller. Specifically, the lookup context
must have full privilege access.
When used with caller,invokedynamic , this is stacked
automatically by the VM.The name of the method to implement. When used with
interfaceMethodName, MethodType invokedynamic , this is provided by the
NameAndType of the InvokeDynamic
structure and is stacked automatically by the VM.The expected signature of the factoryType, MethodType CallSite . The
parameter types represent the types of capture variables;
the return type is the interface to implement. When
used with invokedynamic , this is provided by
the NameAndType of the InvokeDynamic
structure and is stacked automatically by the VM.Signature and return type of method to be
implemented by the function object. interfaceMethodType, MethodHandle A direct method handle describing the implementation
method which should be called (with suitable adaptation
of argument types and return types, and with captured
arguments prepended to the invocation arguments) at
invocation time. implementation, MethodType The signature and return type that should
be enforced dynamically at invocation time.
In simple use cases this is the same as
dynamicMethodType)interfaceMethodType .Facilitates the creation of simple "function objects" that implement one
or more interfaces by delegation to a provided |
EMPTY_CLASS_ARRAY | back to summary |
---|---|
private static final Class<?>[] EMPTY_CLASS_ARRAY |
EMPTY_MT_ARRAY | back to summary |
---|---|
private static final MethodType[] EMPTY_MT_ARRAY |
FLAG_BRIDGES | back to summary |
---|---|
public static final int FLAG_BRIDGES Flag for alternate metafactories indicating the lambda object requires
additional methods that invoke the |
FLAG_MARKERS | back to summary |
---|---|
public static final int FLAG_MARKERS Flag for |
FLAG_SERIALIZABLE | back to summary |
---|---|
public static final int FLAG_SERIALIZABLE Flag for |
LambdaMetafactory | back to summary |
---|---|
private LambdaMetafactory() |
altMetafactory | back to summary |
---|---|
public static CallSite altMetafactory(MethodHandles. Facilitates the creation of simple "function objects" that implement one
or more interfaces by delegation to a provided This is the general, more flexible metafactory; a streamlined version
is provided by The argument list for this method includes three fixed parameters,
corresponding to the parameters automatically stacked by the VM for the
bootstrap method in an
but it behaves as if the argument list is as follows:
Arguments that appear in the argument list for
Each class named by When FLAG_SERIALIZABLE is set in When the target of the
|
extractArg | back to summary |
---|---|
private static <T> T extractArg(Object[] args, int index, Class<T> type) |
extractArgs | back to summary |
---|---|
private static <T> T[] extractArgs(Object[] args, int index, Class<T> type, int count) |
metafactory | back to summary |
---|---|
public static CallSite metafactory(MethodHandles. Facilitates the creation of simple "function objects" that implement one
or more interfaces by delegation to a provided This is the standard, streamlined metafactory; additional flexibility
is provided by When the target of the
|