Top Description Inners Fields Constructors Methods
java.lang.invoke

public Class MethodHandles

extends Object
Class Inheritance
Imports
jdk.internal.access.SharedSecrets, jdk.internal.misc.Unsafe, .VM, jdk.internal.org.objectweb.asm.ClassReader, .Opcodes, .Type, jdk.internal.reflect.CallerSensitive, .CallerSensitiveAdapter, .Reflection, jdk.internal.util.ClassFileDumper, jdk.internal.vm.annotation.ForceInline, sun.invoke.util.ValueConversions, .VerifyAccess, .Wrapper, sun.reflect.misc.ReflectUtil, sun.security.util.SecurityConstants, java.lang.constant.ConstantDescs, java.lang.invoke.LambdaForm.BasicType, java.lang.reflect.Constructor, .Field, .Member, .Method, .Modifier, java.nio.ByteOrder, java.security.ProtectionDomain, java.util.ArrayList, .Arrays, .BitSet, .Comparator, .Iterator, .List, .Objects, .Set, java.util.concurrent.ConcurrentHashMap, java.util.stream.Stream

This class consists exclusively of static methods that operate on or return method handles. They fall into several categories: A lookup, combinator, or factory method will fail and throw an IllegalArgumentException if the created method handle's type would have too many parameters.
Author
John Rose, JSR 292 EG
Since
1.7

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
MethodHandles.Lookup

A lookup object is a factory for creating method handles, when the creation requires access checking.

Field Summary

Modifier and TypeField and Description
private static final MethodHandle[]
pack-priv static final MemberName.Factory
private static final MethodHandle[]

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static MethodHandle
argumentsWithCombiner(boolean filter, MethodHandle target, int position, MethodHandle combiner, int... argPositions)

private static Class<?>
argumentsWithCombinerChecks(int position, boolean filter, MethodType targetType, MethodType combinerType, int... argPos)

public static MethodHandle

Returns:

a method handle which can create arrays of the given type
arrayConstructor
(Class<?>
an array type
arrayClass
)

Produces a method handle constructing arrays of a desired type, as if by the anewarray bytecode.

public static MethodHandle

Returns:

a method handle which can load values from the given array type
arrayElementGetter
(Class<?>
an array type
arrayClass
)

Produces a method handle giving read access to elements of an array, as if by the aaload bytecode.

public static MethodHandle

Returns:

a method handle which can store values into the array type
arrayElementSetter
(Class<?>
the class of an array
arrayClass
)

Produces a method handle giving write access to elements of an array, as if by the astore bytecode.

public static VarHandle

Returns:

a VarHandle giving access to elements of an array
arrayElementVarHandle
(Class<?>
the class of an array, of type T[]
arrayClass
)

Produces a VarHandle giving access to elements of an array of type arrayClass.

public static MethodHandle

Returns:

a method handle which can retrieve the length of an array of the given array type
arrayLength
(Class<?>
an array type
arrayClass
)

Produces a method handle returning the length of an array, as if by the arraylength bytecode.

pack-priv static MethodHandle
private static List<Class<?>>
public static VarHandle

Returns:

a VarHandle giving access to elements of a byte[] array viewed as if elements corresponding to the components type of the view array class
byteArrayViewVarHandle
(Class<?>
the view array class, with a component type of type T
viewArrayClass
,
ByteOrder
the endianness of the view array elements, as stored in the underlying byte array
byteOrder
)

Produces a VarHandle giving access to elements of a byte[] array viewed as if it were a different primitive array type, such as int[] or long[].

public static VarHandle

Returns:

a VarHandle giving access to elements of a ByteBuffer viewed as if elements corresponding to the components type of the view array class
byteBufferViewVarHandle
(Class<?>
the view array class, with a component type of type T
viewArrayClass
,
ByteOrder
the endianness of the view array elements, as stored in the underlying ByteBuffer (Note this overrides the endianness of a ByteBuffer)
byteOrder
)

Produces a VarHandle giving access to elements of a ByteBuffer viewed as if it were an array of elements of a different primitive component type to that of byte, such as int[] or long[].

public static MethodHandle

Returns:

method handle which incorporates the specified try/catch logic
catchException
(MethodHandle
method handle to call
target
,
Class<? extends Throwable>
the type of exception which the handler will catch
exType
,
MethodHandle
method handle to call if a matching exception is thrown
handler
)

Makes a method handle which adapts a target method handle, by running it inside an exception handler.

public static <
the type to cast the class data object to
T
>
T

Returns:

the value of the class data if present in the lookup class; otherwise null
classData
(MethodHandles.Lookup
the lookup context describing the class performing the operation (normally stacked by the JVM)
caller
,
String name, Class<T>
the type of the class data
type
)

Returns the class data associated with the lookup class of the given caller lookup object, or null.

pack-priv static Object
public static <
the type to cast the result object to
T
>
T

Returns:

the element at the given index in the class data if the class data is present; otherwise null
classDataAt
(MethodHandles.Lookup
the lookup context describing the class performing the operation (normally stacked by the JVM)
caller
,
String name, Class<T>
the type of the element at the given index in the class data
type
,
int
index of the element in the class data
index
)

Returns the element at the specified index in the class data, if the class data associated with the lookup class of the given caller lookup object is a List.

public static MethodHandle

Returns:

method handle which incorporates the specified argument subsequence filtering logic
collectArguments
(MethodHandle
the method handle to invoke after filtering the subsequence of arguments
target
,
int
the position of the first adapter argument to pass to the filter, and/or the target argument which receives the result of the filter
pos
,
MethodHandle
method handle to call on the subsequence of arguments
filter
)

Adapts a target method handle by pre-processing a sub-sequence of its arguments with a filter (another method handle).

private static MethodType
public static VarHandle

Returns:

an adapter var handle which filters the incoming coordinate values, before calling the target var handle
collectCoordinates
(VarHandle
the var handle to invoke after the coordinates have been filtered
target
,
int
the position in the coordinate list of the target var handle where the filter is to be inserted
pos
,
MethodHandle
the filter method handle
filter
)

Adapts a target var handle by pre-processing a sub-sequence of its coordinate values with a filter (a method handle).

pack-priv static MethodHandle

Returns:

the adapter method handle
collectReturnValue
(MethodHandle
the target method handle
target
,
MethodHandle
the filter method handle
filter
)

Filter the return value of a target method handle with a filter function.

public static MethodHandle

Returns:

a method handle of the given return type and no arguments, which always returns the given value
constant
(Class<?>
the return type of the desired method handle
type
,
Object
the value to return
value
)

Produces a method handle of the requested return type which returns the given constant value every time it is invoked.

public static MethodHandle

Returns:

a method handle representing the loop.
countedLoop
(MethodHandle
a non-null handle to return the number of iterations this loop should run. The handle's result type must be int. See above for other constraints.
iterations
,
MethodHandle
optional initializer, providing the initial value of the loop variable. May be null, implying a default initial value. See above for other constraints.
init
,
MethodHandle
body of the loop, which may not be null. It controls the loop parameters and result type in the standard case (see above for details). It must accept its own return type (if non-void) plus an int parameter (for the counter), and may accept any number of additional types. See above for other constraints.
body
)

Constructs a loop that runs a given number of iterations.

public static MethodHandle

Returns:

a method handle representing the loop.
countedLoop
(MethodHandle
a non-null handle to return the start value of the loop counter, which must be int. See above for other constraints.
start
,
MethodHandle
a non-null handle to return the end value of the loop counter (the loop will run to end-1). The result type must be int. See above for other constraints.
end
,
MethodHandle
optional initializer, providing the initial value of the loop variable. May be null, implying a default initial value. See above for other constraints.
init
,
MethodHandle
body of the loop, which may not be null. It controls the loop parameters and result type in the standard case (see above for details). It must accept its own return type (if non-void) plus an int parameter (for the counter), and may accept any number of additional types. See above for other constraints.
body
)

Constructs a loop that counts over a range of numbers.

private static void
public static MethodHandle

Returns:

a method handle implementing the while loop as described by the arguments.
doWhileLoop
(MethodHandle
optional initializer, providing the initial value of the loop variable. May be null, implying a default initial value. See above for other constraints.
init
,
MethodHandle
body of the loop, which may not be null. It controls the loop parameters and result type. See above for other constraints.
body
,
MethodHandle
condition for the loop, which may not be null. Its result type must be boolean. See above for other constraints.
pred
)

Constructs a do-while loop from an initializer, a body, and a predicate.

private static int
dropArgumentChecks(MethodType oldType, int pos, Class<?>[] valueTypes)

public static MethodHandle

Returns:

a method handle which drops arguments of the given types, before calling the original method handle
dropArguments
(MethodHandle
the method handle to invoke after the arguments are dropped
target
,
int
position of first argument to drop (zero for the leftmost)
pos
,
List<Class<?>>
the type(s) of the argument(s) to drop
valueTypes
)

Produces a method handle which will discard some dummy arguments before calling some other specified target method handle.

public static MethodHandle

Returns:

a method handle which drops arguments of the given types, before calling the original method handle
dropArguments
(MethodHandle
the method handle to invoke after the arguments are dropped
target
,
int
position of first argument to drop (zero for the leftmost)
pos
,
Class<?>...
the type(s) of the argument(s) to drop
valueTypes
)

Produces a method handle which will discard some dummy arguments before calling some other specified target method handle.

pack-priv static MethodHandle
dropArguments(MethodHandle target, int pos, Class<?> valueType1)

pack-priv static MethodHandle
dropArguments(MethodHandle target, int pos, Class<?> valueType1, Class<?> valueType2)

private static MethodHandle
dropArgumentsToMatch(MethodHandle target, int skip, Class<?>[] newTypes, int pos, boolean nullOnFailure)

public static MethodHandle

Returns:

a possibly adapted method handle
dropArgumentsToMatch
(MethodHandle
the method handle to adapt
target
,
int
number of targets parameters to disregard (they will be unchanged)
skip
,
List<Class<?>>
the list of types to match target's parameter type list to
newTypes
,
int
place in newTypes where the non-skipped target parameters must occur
pos
)

Adapts a target method handle to match the given parameter type list.

pack-priv static MethodHandle
dropArgumentsTrusted(MethodHandle target, int pos, Class<?>[] valueTypes)

public static VarHandle

Returns:

an adapter var handle which drops some dummy coordinates, before calling the target var handle
dropCoordinates
(VarHandle
the var handle to invoke after the dummy coordinates are dropped
target
,
int
position of the first coordinate to drop (zero for the leftmost)
pos
,
Class<?>...
the type(s) of the coordinate(s) to drop
valueTypes
)

Returns a var handle which will discard some dummy coordinates before delegating to the target var handle.

public static MethodHandle

Returns:

a possibly adapted method handle
dropReturn
(MethodHandle
the method handle to adapt
target
)

Drop the return value of the target handle (if any).

public static MethodHandle

Returns:

a constant method handle of the given type, which returns a default value of the given return type
empty
(MethodType
the type of the desired method handle
type
)

Produces a method handle of the requested type which ignores any arguments, does nothing, and returns a suitable default depending on the return type.

public static MethodHandle

Returns:

a method handle suitable for invoking any method handle of the given type
exactInvoker
(MethodType
the desired target type
type
)

Produces a special invoker method handle which can be used to invoke any method handle of the given type, as if by invokeExact.

public static MethodHandle

Returns:

a method handle which delegates to the target after performing any necessary argument conversions, and arranges for any necessary return value conversions
explicitCastArguments
(MethodHandle
the method handle to invoke after arguments are retyped
target
,
MethodType
the expected type of the new method handle
newType
)

Produces a method handle which adapts the type of the given method handle to a new type by pairwise argument and return type conversion.

private static void
private static List<MethodHandle>
fillParameterTypes(List<MethodHandle> hs, final List<Class<?>> targetParams)

pack-priv static MethodHandle
filterArgument(MethodHandle target, int pos, MethodHandle filter)

private static void
public static MethodHandle

Returns:

method handle which incorporates the specified argument filtering logic
filterArguments
(MethodHandle
the method handle to invoke after arguments are filtered
target
,
int
the position of the first argument to filter
pos
,
MethodHandle...
method handles to call initially on filtered arguments
filters
)

Adapts a target method handle by pre-processing one or more of its arguments, each with its own unary filter function, and then calling the target with each pre-processed argument replaced by the result of its corresponding filter function.

private static void
pack-priv static MethodHandle

Returns:

method handle which incorporates the specified argument folding logic
filterArgumentsWithCombiner
(MethodHandle
the method handle to invoke after arguments are combined
target
,
int
the position at which to start folding and at which to insert the folding result; if this is 0, the effect is the same as for foldArguments(MethodHandle, MethodHandle).
position
,
MethodHandle
method handle to call initially on the incoming arguments
combiner
,
int...
indexes of the target to pick arguments sent to the combiner from
argPositions
)

Adapts a target method handle by pre-processing some of its arguments, then calling the target with the result of the pre-processing replacing the argument at the given position.

public static VarHandle

Returns:

an adapter var handle which accepts new coordinate types, applying the provided transformation to the new coordinate values.
filterCoordinates
(VarHandle
the target var handle
target
,
int
the position of the first coordinate to be transformed
pos
,
MethodHandle...
the unary functions which are used to transform coordinates starting at position pos
filters
)

Adapts a target var handle by pre-processing incoming coordinate values using unary filter functions.

private static MethodHandle
filterRepeatedArgument(MethodHandle adapter, MethodHandle filter, int[] positions)

public static MethodHandle

Returns:

method handle which incorporates the specified return value filtering logic
filterReturnValue
(MethodHandle
the method handle to invoke before filtering the return value
target
,
MethodHandle
method handle to call on the return value
filter
)

Adapts a target method handle by post-processing its return value (if any) with a filter (another method handle).

private static void
public static VarHandle

Returns:

an adapter var handle which accepts a new type, performing the provided boxing/unboxing conversions.
filterValue
(VarHandle
the target var handle
target
,
MethodHandle
a filter to convert some type S into the type of target
filterToTarget
,
MethodHandle
a filter to convert the type of target to some type S
filterFromTarget
)

Adapts a target var handle by pre-processing incoming and outgoing values using a pair of filter functions.

private static int
findFirstDupOrDrop(int[] reorder, int newArity)

Return an indication of any duplicate or omission in reorder.

private static List<MethodHandle>
private static Class<?>
foldArgumentChecks(int foldPos, MethodType targetType, MethodType combinerType)

public static MethodHandle

Returns:

method handle which incorporates the specified argument folding logic
foldArguments
(MethodHandle
the method handle to invoke after arguments are combined
target
,
MethodHandle
method handle to call initially on the incoming arguments
combiner
)

Adapts a target method handle by pre-processing some of its arguments, and then calling the target with the result of the pre-processing, inserted into the original sequence of arguments.

public static MethodHandle

Returns:

method handle which incorporates the specified argument folding logic
foldArguments
(MethodHandle
the method handle to invoke after arguments are combined
target
,
int
the position at which to start folding and at which to insert the folding result; if this is 0, the effect is the same as for foldArguments(MethodHandle, MethodHandle).
pos
,
MethodHandle
method handle to call initially on the incoming arguments
combiner
)

Adapts a target method handle by pre-processing some of its arguments, starting at a given position, and then calling the target with the result of the pre-processing, inserted into the original sequence of arguments just before the folded arguments.

pack-priv static MethodHandle

Returns:

method handle which incorporates the specified argument folding logic
foldArgumentsWithCombiner
(MethodHandle
the method handle to invoke after arguments are combined
target
,
int
the position at which to start folding and at which to insert the folding result; if this is 0, the effect is the same as for foldArguments(MethodHandle, MethodHandle).
position
,
MethodHandle
method handle to call initially on the incoming arguments
combiner
,
int...
indexes of the target to pick arguments sent to the combiner from
argPositions
)

Adapts a target method handle by pre-processing some of its arguments, calling the target with the result of the pre-processing inserted into the original sequence of arguments at the given position.

public static MethodHandle

Returns:

method handle which incorporates the specified if/then/else logic
guardWithTest
(MethodHandle
method handle used for test, must return boolean
test
,
MethodHandle
method handle to call if test passes
target
,
MethodHandle
method handle to call if test fails
fallback
)

Makes a method handle which adapts a target method handle, by guarding it with a test, a boolean-valued method handle.

public static MethodHandle

Returns:

a unary method handle which accepts and returns the given type
identity
(Class<?>
the type of the sole parameter and return value of the desired method handle
type
)

Produces a method handle which returns its sole argument when invoked.

private static MethodHandle
private static BoundMethodHandle
insertArgumentPrimitive(BoundMethodHandle result, int pos, Class<?> ptype, Object value)

public static MethodHandle

Returns:

a method handle which inserts an additional argument, before calling the original method handle
insertArguments
(MethodHandle
the method handle to invoke after the argument is inserted
target
,
int
where to insert the argument (zero for the first)
pos
,
Object...
the series of arguments to insert
values
)

Provides a target method handle with one or more bound arguments in advance of the method handle's invocation.

private static Class<?>[]
insertArgumentsChecks(MethodHandle target, int insCount, int pos)

public static VarHandle

Returns:

an adapter var handle which inserts additional coordinates, before calling the target var handle
insertCoordinates
(VarHandle
the var handle to invoke after the bound coordinates are inserted
target
,
int
the position of the first coordinate to be inserted
pos
,
Object...
the series of bound coordinates to insert
values
)

Provides a target var handle with one or more bound coordinates in advance of the var handle's invocation.

public static MethodHandle

Returns:

a method handle suitable for invoking any method handle convertible to the given type
invoker
(MethodType
the desired target type
type
)

Produces a special invoker method handle which can be used to invoke any method handle compatible with the given type, as if by invoke.

public static MethodHandle

Returns:

a method handle embodying the iteration loop functionality.
iteratedLoop
(MethodHandle
an optional handle to return the iterator to start the loop. If non-null, the handle must return java.util.Iterator or a subtype. See above for other constraints.
iterator
,
MethodHandle
optional initializer, providing the initial value of the loop variable. May be null, implying a default initial value. See above for other constraints.
init
,
MethodHandle
body of the loop, which may not be null. It controls the loop parameters and result type in the standard case (see above for details). It must accept its own return type (if non-void) plus a T parameter (for the iterated values), and may accept any number of additional types. See above for other constraints.
body
)

Constructs a loop that ranges over the values produced by an Iterator<T>.

private static Class<?>
private static List<Class<?>>
public static MethodHandles.Lookup

Returns:

a lookup object for the caller of this method, with original and full privilege access.
lookup
()

Returns a lookup object with full capabilities to emulate all supported bytecode behaviors of the caller.

private static MethodHandles.Lookup
lookup(Class<?> caller)

This lookup method is the alternate implementation of the lookup method with a leading caller class argument which is non-caller-sensitive.

public static MethodHandle

Returns:

a method handle embodying the looping behavior as defined by the arguments.
loop
(MethodHandle[]...
an array of arrays (4-tuples) of MethodHandles adhering to the rules described above.
clauses
)

Constructs a method handle representing a loop with several loop variables that are updated and checked upon each iteration.

private static void
private static void
private static void
loopChecks1b(List<MethodHandle> init, List<Class<?>> commonSuffix)

private static void
loopChecks1cd(List<MethodHandle> pred, List<MethodHandle> fini, Class<?> loopReturnType)

private static void
loopChecks2(List<MethodHandle> step, List<MethodHandle> pred, List<MethodHandle> fini, List<Class<?>> commonParameterSequence)

private static MethodHandle
makeIdentity(Class<?> ptype)

private static MethodHandle
makeZero(Class<?> rtype)

pack-priv static <T> RuntimeException
misMatchedTypes(String what, T t1, T t2)

pack-priv static boolean
permuteArgumentChecks(int[] reorder, MethodType newType, MethodType oldType)

public static MethodHandle

Returns:

a method handle which delegates to the target after it drops unused arguments and moves and/or duplicates the other arguments
permuteArguments
(MethodHandle
the method handle to invoke after arguments are reordered
target
,
MethodType
the expected type of the new method handle
newType
,
int...
an index array which controls the reordering
reorder
)

Produces a method handle which adapts the calling sequence of the given method handle to a new type, by reordering the arguments.

public static VarHandle

Returns:

an adapter var handle which re-arranges the incoming coordinate values, before calling the target var handle
permuteCoordinates
(VarHandle
the var handle to invoke after the coordinates have been reordered
target
,
List<Class<?>>
the new coordinate types
newCoordinates
,
int...
an index array which controls the reordering
reorder
)

Provides a var handle which adapts the coordinate values of the target var handle, by re-arranging them so that the new coordinates match the provided ones.

public static MethodHandles.Lookup

Returns:

a lookup object for the target class, with private access
privateLookupIn
(Class<?>
the target class
targetClass
,
MethodHandles.Lookup
the caller lookup object
caller
)

Returns a lookup object on a target class to emulate all supported bytecode behaviors, including private access.

public static MethodHandles.Lookup

Returns:

a lookup object which is trusted minimally
publicLookup
()

Returns a lookup object which is trusted minimally.

public static <
the desired type of the result, either Member or a subtype
T extends Member
>
T

Returns:

a reference to the method, constructor, or field object
reflectAs
(Class<T>
a class object representing the desired result type T
expected
,
MethodHandle
a direct method handle to crack into symbolic reference components
target
)

Performs an unchecked "crack" of a direct method handle.

private static synchronized MethodHandle
public static MethodHandle

Returns:

a method handle suitable for invoking any method handle of the given type
spreadInvoker
(MethodType
the desired target type
type
,
int
number of fixed arguments, to be passed unchanged to the target
leadingArgCount
)

Produces a method handle which will invoke any method handle of the given type, with a given number of trailing arguments replaced by a single trailing Object[] array.

pack-priv static MethodHandle
swapArguments(MethodHandle mh, int i, int j)

public static MethodHandle

Returns:

the table switch method handle.
tableSwitch
(MethodHandle
the fallback method handle that is called when the selector is not within the range [0, N).
fallback
,
MethodHandle...
array of target method handles.
targets
)

Creates a table switch method handle, which can be used to switch over a set of target method handles, based on a given target index, called selector.

private static MethodType
tableSwitchChecks(MethodHandle defaultCase, MethodHandle[] caseActions)

public static MethodHandle

Returns:

method handle which can throw the given exceptions
throwException
(Class<?>
the return type of the desired method handle
returnType
,
Class<? extends Throwable>
the parameter type of the desired method handle
exType
)

Produces a method handle which will throw exceptions of the given exType.

public static MethodHandle

Returns:

a method handle embodying the try-finally block composed of the two arguments.
tryFinally
(MethodHandle
the handle whose execution is to be wrapped in a try block.
target
,
MethodHandle
the handle that is invoked in the finally block.
cleanup
)

Makes a method handle that adapts a target method handle by wrapping it in a try-finally block.

private static void
public static MethodHandle

Returns:

a method handle suitable for invoking an access mode method of any VarHandle whose access mode type is of the given type.
varHandleExactInvoker
(VarHandle.AccessMode
the VarHandle access mode
accessMode
,
MethodType
the desired target type
type
)

Produces a special invoker method handle which can be used to invoke a signature-polymorphic access mode method on any VarHandle whose associated access mode type is compatible with the given type.

public static MethodHandle

Returns:

a method handle suitable for invoking an access mode method of any VarHandle whose access mode type is convertible to the given type.
varHandleInvoker
(VarHandle.AccessMode
the VarHandle access mode
accessMode
,
MethodType
the desired target type
type
)

Produces a special invoker method handle which can be used to invoke a signature-polymorphic access mode method on any VarHandle whose associated access mode type is compatible with the given type.

public static MethodHandle

Returns:

a method handle implementing the while loop as described by the arguments.
whileLoop
(MethodHandle
optional initializer, providing the initial value of the loop variable. May be null, implying a default initial value. See above for other constraints.
init
,
MethodHandle
condition for the loop, which may not be null. Its result type must be boolean. See above for other constraints.
pred
,
MethodHandle
body of the loop, which may not be null. It controls the loop parameters and result type. See above for other constraints.
body
)

Constructs a while loop from an initializer, a body, and a predicate.

private static void
public static MethodHandle

Returns:

a constant method handle that takes no arguments and returns the default value of the given type (or void, if the type is void)
zero
(Class<?>
the expected return type of the desired method handle
type
)

Produces a constant method handle of the requested return type which returns the default value for that type every time it is invoked.

private static MethodHandle
zero(Wrapper btw, Class<?> rtype)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait