public static final class MethodHandles.Lookup extends Object
Unlike Reflection, MethodHandle only requires access-checking when the MethodHandle is created, not when it is invoked.
This class provides the lookup authentication necessary when creating MethodHandles. Any number of MethodHandles can be lookup using this token, and the token can be shared to provide others with the "owner's" authentication level.
Sharing MethodHandles.Lookup
objects should be done with care, as they may allow access to private
methods.
When using the lookup factory methods (find and unreflect methods), creating the MethodHandle may fail because the method's arity is too high.
Modifier and Type | Field and Description |
---|---|
static int |
PACKAGE
Bit flag 0x8 representing package access.
|
static int |
PRIVATE
Bit flag 0x2 representing private access.
|
static int |
PROTECTED
Bit flag 0x4 representing protected access.
|
static int |
PUBLIC
Bit flag 0x1 representing public access.
|
Modifier and Type | Method and Description |
---|---|
MethodHandle |
bind(Object receiver,
String methodName,
MethodType type)
Return an early-bound method handle to a non-static method.
|
MethodHandle |
findConstructor(Class<?> declaringClass,
MethodType type)
Return a MethodHandle that will create an object of the required class and initialize it using
the constructor method with signature type.
|
MethodHandle |
findGetter(Class<?> clazz,
String fieldName,
Class<?> fieldType)
Return a MethodHandle that provides read access to a field.
|
MethodHandle |
findSetter(Class<?> clazz,
String fieldName,
Class<?> fieldType)
Return a MethodHandle that provides write access to a field.
|
MethodHandle |
findSpecial(Class<?> clazz,
String methodName,
MethodType type,
Class<?> specialToken)
Return a MethodHandle bound to a specific-implementation of a virtual method, as if created by an invokespecial bytecode
using the class specialToken.
|
MethodHandle |
findStatic(Class<?> clazz,
String methodName,
MethodType type)
Return a MethodHandle to a static method.
|
MethodHandle |
findStaticGetter(Class<?> clazz,
String fieldName,
Class<?> fieldType)
Return a MethodHandle that provides read access to a field.
|
MethodHandle |
findStaticSetter(Class<?> clazz,
String fieldName,
Class<?> fieldType)
Return a MethodHandle that provides write access to a field.
|
MethodHandle |
findVirtual(Class<?> clazz,
String methodName,
MethodType type)
Return a MethodHandle to a virtual method.
|
MethodHandles.Lookup |
in(Class<?> lookupClass)
Create a lookup on the request class.
|
Class<?> |
lookupClass()
The class being used for visibility checks and access permissions.
|
int |
lookupModes()
A query to determine the lookup capabilities held by this instance.
|
MethodHandleInfo |
revealDirect(MethodHandle target)
Cracks a MethodHandle, which allows access to its symbolic parts.
|
String |
toString()
Answers a string containing a concise, human-readable
description of the receiver.
|
MethodHandle |
unreflect(Method method)
Make a MethodHandle to the Reflect method.
|
MethodHandle |
unreflectConstructor(Constructor<?> method)
Return a MethodHandle for the reflect constructor.
|
MethodHandle |
unreflectGetter(Field field)
Create a MethodHandle that returns the value of the Reflect field.
|
MethodHandle |
unreflectSetter(Field field)
Create a MethodHandle that sets the value of the Reflect field.
|
MethodHandle |
unreflectSpecial(Method method,
Class<?> specialToken)
Return a MethodHandle for the Reflect method, that will directly call the requested method
as through from the class
specialToken . |
public static final int PUBLIC
lookupModes()
.public static final int PRIVATE
lookupModes()
.public static final int PROTECTED
lookupModes()
.public static final int PACKAGE
lookupModes()
.public int lookupModes()
public MethodHandle bind(Object receiver, String methodName, MethodType type) throws IllegalAccessException, NoSuchMethodException
The final MethodType of the MethodHandle will be identical to that of the method. The receiver will be inserted prior to the call and therefore does not need to be included in the MethodType.
receiver
- - The Object to insert as a receiver. Must implement the methodNamemethodName
- - The name of the methodtype
- - The MethodType describing the methodIllegalAccessException
- if access is deniedNoSuchMethodException
- if the method doesn't existpublic MethodHandle findSpecial(Class<?> clazz, String methodName, MethodType type, Class<?> specialToken) throws IllegalAccessException, NoSuchMethodException, SecurityException, NullPointerException
The receiver class will be added to the MethodType of the handle, but dispatch will not occur based on the receiver.
clazz
- - the class or interface from which defines the methodmethodName
- - the method nametype
- - the MethodType of the methodspecialToken
- - the calling class for the invokespecialIllegalAccessException
- - if the method is static or access checking failsNullPointerException
- - if clazz, methodName, type or specialToken is nullNoSuchMethodException
- - if clazz has no instance method named methodName with signature matching typeSecurityException
- - if any installed SecurityManager denies access to the methodpublic MethodHandle findStatic(Class<?> clazz, String methodName, MethodType type) throws IllegalAccessException, NoSuchMethodException
clazz
- - the class defining the methodmethodName
- - the name of the methodtype
- - the MethodType of the methodIllegalAccessException
- - if the method is not static or access checking failsNullPointerException
- - if clazz, methodName or type is nullNoSuchMethodException
- - if clazz has no static method named methodName with signature matching typeSecurityException
- - if any installed SecurityManager denies access to the methodpublic MethodHandle findVirtual(Class<?> clazz, String methodName, MethodType type) throws IllegalAccessException, NoSuchMethodException
clazz
- - the class defining the methodmethodName
- - the name of the methodtype
- - the type of the methodIllegalAccessException
- - if method is static or access is refusedNullPointerException
- - if clazz, methodName or type is nullNoSuchMethodException
- - if clazz has no virtual method named methodName with signature matching typeSecurityException
- - if any installed SecurityManager denies access to the methodpublic MethodHandle findGetter(Class<?> clazz, String fieldName, Class<?> fieldType) throws IllegalAccessException, NoSuchFieldException, SecurityException, NullPointerException
clazz
and returning something of
type fieldType
.clazz
- - the class defining the fieldfieldName
- - the name of the fieldfieldType
- - the type of the fieldIllegalAccessException
- if access is denied or the field is staticNoSuchFieldException
- if no field is found with given name and type in clazzSecurityException
- if the SecurityManager prevents accessNullPointerException
- if any of the arguments are nullpublic MethodHandle findStaticGetter(Class<?> clazz, String fieldName, Class<?> fieldType) throws IllegalAccessException, NoSuchFieldException, SecurityException, NullPointerException
fieldType
.clazz
- - the class defining the fieldfieldName
- - the name of the fieldfieldType
- - the type of the fieldIllegalAccessException
- if access is denied or the field is not staticNoSuchFieldException
- if no field is found with given name and type in clazzSecurityException
- if the SecurityManager prevents accessNullPointerException
- if any of the arguments are nullpublic MethodHandle findSetter(Class<?> clazz, String fieldName, Class<?> fieldType) throws IllegalAccessException, NoSuchFieldException, SecurityException, NullPointerException
clazz
and the second with
type fieldType
, and returning void.clazz
- - the class defining the fieldfieldName
- - the name of the fieldfieldType
- - the type of the fieldIllegalAccessException
- if access is deniedNoSuchFieldException
- if no field is found with given name and type in clazzSecurityException
- if the SecurityManager prevents accessNullPointerException
- if any of the arguments are nullpublic MethodHandle findStaticSetter(Class<?> clazz, String fieldName, Class<?> fieldType) throws IllegalAccessException, NoSuchFieldException, SecurityException, NullPointerException
fieldType
and returning void.clazz
- - the class defining the fieldfieldName
- - the name of the fieldfieldType
- - the type of the fieldIllegalAccessException
- if access is deniedNoSuchFieldException
- if no field is found with given name and type in clazzSecurityException
- if the SecurityManager prevents accessNullPointerException
- if any of the arguments are nullpublic MethodHandles.Lookup in(Class<?> lookupClass)
lookupClass
- - the class to create the lookup onpublic Class<?> lookupClass()
public MethodHandle unreflect(Method method) throws IllegalAccessException
If m is a virtual method, normal virtual dispatch is used on each invocation.
If the accessible
flag is not set on the Reflect method, then access checking
is performed using the lookup class.
method
- - the reflect methodIllegalAccessException
- - if access is deniedpublic MethodHandle unreflectConstructor(Constructor<?> method) throws IllegalAccessException
If the accessible
flag is not set, then access checking
is performed using the lookup class.
method
- - the Reflect constructorIllegalAccessException
- - if access is deniedpublic MethodHandle findConstructor(Class<?> declaringClass, MethodType type) throws IllegalAccessException, NoSuchMethodException
declaringClass
- - Class that declares the constructortype
- - the MethodType of the constructor. Return type must be void.IllegalAccessException
- if access is deniedNoSuchMethodException
- if the method doesn't existSecurityException
- if the SecurityManager prevents accessNullPointerException
- if any of the arguments are nullpublic MethodHandle unreflectSpecial(Method method, Class<?> specialToken) throws IllegalAccessException
specialToken
. The MethodType of the method handle
will be that of the method with the receiver argument prepended.method
- - a Reflect methodspecialToken
- - the class the call is fromIllegalAccessException
- - if access is deniedpublic MethodHandle unreflectGetter(Field field) throws IllegalAccessException
If the accessible
flag is not set, then access checking
is performed using the lookup class.
field
- - a Reflect fieldIllegalAccessException
- - if access is deniedpublic MethodHandle unreflectSetter(Field field) throws IllegalAccessException
If the accessible
flag is not set, then access checking
is performed using the lookup class.
field
- - a Reflect fieldIllegalAccessException
- - if access is deniedpublic MethodHandleInfo revealDirect(MethodHandle target) throws IllegalArgumentException, NullPointerException, SecurityException
target
- The MethodHandle to be crackedIllegalArgumentException
- if the target is not a direct handle, or if the access check failsNullPointerException
- if the target is nullSecurityException
- if a SecurityManager denies access Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.