Top Description Fields Constructors Methods
java.lang.invoke

public final Class LambdaMetafactory

extends Object
Class Inheritance
Imports
java.io.Serializable, java.util.Arrays, .Objects, java.lang.reflect.Array

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:

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.Lookup, String, MethodType, MethodType, MethodHandle, MethodType)) using an optimized protocol, and an alternate version altMetafactory(MethodHandles.Lookup, String, MethodType, Object...)). 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:

Assume the linkage arguments are as follows:

Then the following linkage invariants must hold:

Further, 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:

adaptable types
QSLink-time checksInvocation-time checks
PrimitivePrimitive Q can be converted to S via a primitive widening conversion None
PrimitiveReference S is a supertype of the Wrapper(Q) Cast from Wrapper(Q) to S
ReferencePrimitive 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
ReferenceReference 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.

Since
1.8

Field Summary

Modifier and TypeField 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 implementation

public static final int
FLAG_MARKERS

Flag for altMetafactory indicating the lambda object implements other interfaces besides Serializable

public static final int
FLAG_SERIALIZABLE

Flag for altMetafactory indicating the lambda object must be serializable

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static CallSite

Returns:

a CallSite whose target can be used to perform capture, generating instances of the interface named by factoryType
altMetafactory
(MethodHandles.Lookup
Represents a lookup context with the accessibility privileges of the caller. Specifically, the lookup context must have full privilege access. When used with invokedynamic, this is stacked automatically by the VM.
caller
,
String
The name of the method to implement. When used with invokedynamic, this is provided by the NameAndType of the InvokeDynamic structure and is stacked automatically by the VM.
interfaceMethodName
,
MethodType
The expected signature of the 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.
factoryType
,
Object...
An array of Object containing the required arguments interfaceMethodType, implementation, dynamicMethodType, flags, and any optional arguments, as described above
args
)

Facilitates the creation of simple "function objects" that implement one or more interfaces by delegation to a provided MethodHandle, after appropriate type adaptation and partial evaluation of arguments.

private static <T> T
extractArg(Object[] args, int index, Class<T> type)

private static <T> T[]
extractArgs(Object[] args, int index, Class<T> type, int count)

public static CallSite

Returns:

a CallSite whose target can be used to perform capture, generating instances of the interface named by factoryType
metafactory
(MethodHandles.Lookup
Represents a lookup context with the accessibility privileges of the caller. Specifically, the lookup context must have full privilege access. When used with invokedynamic, this is stacked automatically by the VM.
caller
,
String
The name of the method to implement. When used with invokedynamic, this is provided by the NameAndType of the InvokeDynamic structure and is stacked automatically by the VM.
interfaceMethodName
,
MethodType
The expected signature of the 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.
factoryType
,
MethodType
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 interfaceMethodType.
dynamicMethodType
)

Facilitates the creation of simple "function objects" that implement one or more interfaces by delegation to a provided MethodHandle, after appropriate type adaptation and partial evaluation of arguments.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EMPTY_CLASS_ARRAYback to summary
private static final Class<?>[] EMPTY_CLASS_ARRAY
EMPTY_MT_ARRAYback to summary
private static final MethodType[] EMPTY_MT_ARRAY
FLAG_BRIDGESback to summary
public static final int FLAG_BRIDGES

Flag for alternate metafactories indicating the lambda object requires additional methods that invoke the implementation

FLAG_MARKERSback to summary
public static final int FLAG_MARKERS

Flag for altMetafactory indicating the lambda object implements other interfaces besides Serializable

FLAG_SERIALIZABLEback to summary
public static final int FLAG_SERIALIZABLE

Flag for altMetafactory indicating the lambda object must be serializable

Constructor Detail

LambdaMetafactoryback to summary
private LambdaMetafactory()

Method Detail

altMetafactoryback to summary
public static CallSite altMetafactory(MethodHandles.Lookup caller, String interfaceMethodName, MethodType factoryType, Object... args) throws LambdaConversionException

Facilitates the creation of simple "function objects" that implement one or more interfaces by delegation to a provided MethodHandle, after appropriate type adaptation and partial evaluation of arguments. Typically used as a bootstrap method for invokedynamic call sites, to support the lambda expression and method reference expression features of the Java Programming Language.

This is the general, more flexible metafactory; a streamlined version is provided by metafactory(java.lang.invoke.MethodHandles.Lookup, String, MethodType, MethodType, MethodHandle, MethodType). A general description of the behavior of this method is provided above.

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 invokedynamic invocation, and an Object[] parameter that contains additional parameters. The declared argument list for this method is:

CallSite altMetafactory(MethodHandles.Lookup caller,
                         String interfaceMethodName,
                         MethodType factoryType,
                         Object... args)

but it behaves as if the argument list is as follows:

CallSite altMetafactory(MethodHandles.Lookup caller,
                         String interfaceMethodName,
                         MethodType factoryType,
                         MethodType interfaceMethodType,
                         MethodHandle implementation,
                         MethodType dynamicMethodType,
                         int flags,
                         int altInterfaceCount,        // IF flags has MARKERS set
                         Class... altInterfaces,       // IF flags has MARKERS set
                         int altMethodCount,           // IF flags has BRIDGES set
                         MethodType... altMethods      // IF flags has BRIDGES set
                         )

Arguments that appear in the argument list for metafactory(MethodHandles.Lookup, String, MethodType, MethodType, MethodHandle, MethodType) have the same specification as in that method. The additional arguments are interpreted as follows:

  • flags indicates additional options; this is a bitwise OR of desired flags. Defined flags are FLAG_BRIDGES, FLAG_MARKERS, and FLAG_SERIALIZABLE.
  • altInterfaceCount is the number of additional interfaces the function object should implement, and is present if and only if the FLAG_MARKERS flag is set.
  • altInterfaces is a variable-length list of additional interfaces to implement, whose length equals altInterfaceCount, and is present if and only if the FLAG_MARKERS flag is set.
  • altMethodCount is the number of additional method signatures the function object should implement, and is present if and only if the FLAG_BRIDGES flag is set.
  • altMethods is a variable-length list of additional methods signatures to implement, whose length equals altMethodCount, and is present if and only if the FLAG_BRIDGES flag is set.

Each class named by altInterfaces is subject to the same restrictions as Rd, the return type of factoryType, as described above. Each MethodType named by altMethods is subject to the same restrictions as interfaceMethodType, as described above.

When FLAG_SERIALIZABLE is set in flags, the function objects will implement Serializable, and will have a writeReplace method that returns an appropriate SerializedLambda. The caller class must have an appropriate $deserializeLambda$ method, as described in SerializedLambda.

When the target of the CallSite returned from this method is invoked, the resulting function objects are instances of a class with the following properties:

  • The class implements the interface named by the return type of factoryType and any interfaces named by altInterfaces
  • The class declares methods with the name given by interfaceMethodName, and the signature given by interfaceMethodType and additional signatures given by altMethods
  • The class may override methods from Object, and may implement methods related to serialization.
Parameters
caller:MethodHandles.Lookup

Represents a lookup context with the accessibility privileges of the caller. Specifically, the lookup context must have full privilege access. When used with invokedynamic, this is stacked automatically by the VM.

interfaceMethodName:String

The name of the method to implement. When used with invokedynamic, this is provided by the NameAndType of the InvokeDynamic structure and is stacked automatically by the VM.

factoryType:MethodType

The expected signature of the 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.

args:Object[]

An array of Object containing the required arguments interfaceMethodType, implementation, dynamicMethodType, flags, and any optional arguments, as described above

Returns:CallSite

a CallSite whose target can be used to perform capture, generating instances of the interface named by factoryType

Exceptions
LambdaConversionException:
If caller does not have full privilege access, or if interfaceMethodName is not a valid JVM method name, or if the return type of factoryType is not an interface, or if any of altInterfaces is not an interface, or if implementation is not a direct method handle referencing a method or constructor, or if the linkage invariants are violated, as defined above.
NullPointerException:
If any argument, or any component of args, is null.
IllegalArgumentException:
If the number or types of the components of args do not follow the above rules, or if altInterfaceCount or altMethodCount are negative integers.
SecurityException:
If a security manager is present, and it refuses access from caller to the package of implementation.
extractArgback to summary
private static <T> T extractArg(Object[] args, int index, Class<T> type)
extractArgsback to summary
private static <T> T[] extractArgs(Object[] args, int index, Class<T> type, int count)
metafactoryback to summary
public static CallSite metafactory(MethodHandles.Lookup caller, String interfaceMethodName, MethodType factoryType, MethodType interfaceMethodType, MethodHandle implementation, MethodType dynamicMethodType) throws LambdaConversionException

Facilitates the creation of simple "function objects" that implement one or more interfaces by delegation to a provided MethodHandle, after appropriate type adaptation and partial evaluation of arguments. Typically used as a bootstrap method for invokedynamic call sites, to support the lambda expression and method reference expression features of the Java Programming Language.

This is the standard, streamlined metafactory; additional flexibility is provided by altMetafactory(MethodHandles.Lookup, String, MethodType, Object...). A general description of the behavior of this method is provided above.

When the target of the CallSite returned from this method is invoked, the resulting function objects are instances of a class which implements the interface named by the return type of factoryType, declares a method with the name given by interfaceMethodName and the signature given by interfaceMethodType. It may also override additional methods from Object.

Parameters
caller:MethodHandles.Lookup

Represents a lookup context with the accessibility privileges of the caller. Specifically, the lookup context must have full privilege access. When used with invokedynamic, this is stacked automatically by the VM.

interfaceMethodName:String

The name of the method to implement. When used with invokedynamic, this is provided by the NameAndType of the InvokeDynamic structure and is stacked automatically by the VM.

factoryType:MethodType

The expected signature of the 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.

interfaceMethodType:MethodType

Signature and return type of method to be implemented by the function object.

implementation: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.

dynamicMethodType:MethodType

The signature and return type that should be enforced dynamically at invocation time. In simple use cases this is the same as interfaceMethodType.

Returns:CallSite

a CallSite whose target can be used to perform capture, generating instances of the interface named by factoryType

Exceptions
LambdaConversionException:
If caller does not have full privilege access, or if interfaceMethodName is not a valid JVM method name, or if the return type of factoryType is not an interface, or if implementation is not a direct method handle referencing a method or constructor, or if the linkage invariants are violated, as defined above.
NullPointerException:
If any argument is null.
SecurityException:
If a security manager is present, and it refuses access from caller to the package of implementation.