- java.lang.Object
-
- java.lang.invoke.MethodHandles.Lookup
-
- Enclosing class:
- MethodHandles
public static final class MethodHandles.Lookup extends Object
A factory for creating MethodHandles that require access-checking on creation.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 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.
-
-
Field Summary
Fields Modifier and Type Field Description static int
MODULE
Bit flag 0x10 representing module access.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.static int
UNCONDITIONAL
Bit flag 0x20, which contributes to a lookup, and indicates assumed readability.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Class<?>
accessClass(Class<?> targetClass)
Tells whether the target class is accessible to the lookup class.MethodHandle
bind(Object receiver, String methodName, MethodType type)
Return an early-bound method handle to a non-static method.Class<?>
defineClass(byte[] classBytes)
Return a class object with the same class loader, the same package and the same protection domain as the lookup's lookup class.MethodHandles.Lookup
dropLookupMode(int dropMode)
Return a MethodHandles.Lookup object without the requested lookup mode.Class<?>
findClass(String targetName)
Search the target class by class name via the lookup context.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.VarHandle
findStaticVarHandle(Class<?> definingClass, String name, Class<?> type)
Factory method for obtaining a VarHandle referencing a static field.VarHandle
findVarHandle(Class<?> definingClass, String name, Class<?> type)
Factory method for obtaining a VarHandle referencing a non-static field.MethodHandle
findVirtual(Class<?> clazz, String methodName, MethodType type)
Return a MethodHandle to a virtual method.boolean
hasPrivateAccess()
Return true if the lookup class has private accessMethodHandles.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.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 classspecialToken
.VarHandle
unreflectVarHandle(Field field)
Create aVarHandle
that operates on the field represented by thereflect field
.
-
-
-
Field Detail
-
PUBLIC
public static final int PUBLIC
Bit flag 0x1 representing public access. SeelookupModes()
.- See Also:
- Constant Field Values
-
PRIVATE
public static final int PRIVATE
Bit flag 0x2 representing private access. SeelookupModes()
.- See Also:
- Constant Field Values
-
PROTECTED
public static final int PROTECTED
Bit flag 0x4 representing protected access. SeelookupModes()
.- See Also:
- Constant Field Values
-
PACKAGE
public static final int PACKAGE
Bit flag 0x8 representing package access. SeelookupModes()
.- See Also:
- Constant Field Values
-
MODULE
public static final int MODULE
Bit flag 0x10 representing module access. SeelookupModes()
.- See Also:
- Constant Field Values
-
UNCONDITIONAL
public static final int UNCONDITIONAL
Bit flag 0x20, which contributes to a lookup, and indicates assumed readability. This bit, with the Public bit, indicates the lookup can access all public members where the public type is exported unconditionally.- See Also:
- Constant Field Values
-
-
Method Detail
-
lookupModes
public int lookupModes()
A query to determine the lookup capabilities held by this instance.- Returns:
- the lookup mode bit mask for this Lookup object
-
bind
public MethodHandle bind(Object receiver, String methodName, MethodType type) throws IllegalAccessException, NoSuchMethodException
Return an early-bound method handle to a non-static method. The receiver must have a Class in its hierarchy that provides the virtual method named methodName.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.
- Parameters:
receiver
- - The Object to insert as a receiver. Must implement the methodNamemethodName
- - The name of the methodtype
- - The MethodType describing the method- Returns:
- a MethodHandle to the required method.
- Throws:
IllegalAccessException
- if access is deniedNoSuchMethodException
- if the method doesn't exist
-
findSpecial
public MethodHandle findSpecial(Class<?> clazz, String methodName, MethodType type, Class<?> specialToken) throws IllegalAccessException, NoSuchMethodException, SecurityException, NullPointerException
Return a MethodHandle bound to a specific-implementation of a virtual method, as if created by an invokespecial bytecode using the class specialToken. The method and all Classes in its MethodType must be accessible to the caller.The receiver class will be added to the MethodType of the handle, but dispatch will not occur based on the receiver.
- Parameters:
clazz
- - the class or interface from which defines the methodmethodName
- - the method nametype
- - the MethodType of the methodspecialToken
- - the calling class for the invokespecial- Returns:
- a MethodHandle
- Throws:
IllegalAccessException
- - 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 method
-
findStatic
public MethodHandle findStatic(Class<?> clazz, String methodName, MethodType type) throws IllegalAccessException, NoSuchMethodException
Return a MethodHandle to a static method. The MethodHandle will have the same type as the method. The method and all classes in its type must be accessible to the caller.- Parameters:
clazz
- - the class defining the methodmethodName
- - the name of the methodtype
- - the MethodType of the method- Returns:
- A MethodHandle able to invoke the requested static method
- Throws:
IllegalAccessException
- - 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 method
-
findVirtual
public MethodHandle findVirtual(Class<?> clazz, String methodName, MethodType type) throws IllegalAccessException, NoSuchMethodException
Return a MethodHandle to a virtual method. The method will be looked up in the first argument (aka receiver) prior to dispatch. The type of the MethodHandle will be that of the method with the receiver type prepended.- Parameters:
clazz
- - the class defining the methodmethodName
- - the name of the methodtype
- - the type of the method- Returns:
- a MethodHandle that will do virtual dispatch on the first argument
- Throws:
IllegalAccessException
- - if method is static or access is refused or clazz's package is not exported to the current moduleIllegalAccessError
- - if the type's packages are not exported to the current moduleNullPointerException
- - 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 method
-
findGetter
public MethodHandle findGetter(Class<?> clazz, String fieldName, Class<?> fieldType) throws IllegalAccessException, NoSuchFieldException, SecurityException, NullPointerException
Return a MethodHandle that provides read access to a field. The MethodHandle will have a MethodType taking a single argument with typeclazz
and returning something of typefieldType
.- Parameters:
clazz
- - the class defining the fieldfieldName
- - the name of the fieldfieldType
- - the type of the field- Returns:
- a MethodHandle able to return the value of the field
- Throws:
IllegalAccessException
- 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 null
-
findStaticGetter
public MethodHandle findStaticGetter(Class<?> clazz, String fieldName, Class<?> fieldType) throws IllegalAccessException, NoSuchFieldException, SecurityException, NullPointerException
Return a MethodHandle that provides read access to a field. The MethodHandle will have a MethodType taking no arguments and returning something of typefieldType
.- Parameters:
clazz
- - the class defining the fieldfieldName
- - the name of the fieldfieldType
- - the type of the field- Returns:
- a MethodHandle able to return the value of the field
- Throws:
IllegalAccessException
- 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 null
-
findSetter
public MethodHandle findSetter(Class<?> clazz, String fieldName, Class<?> fieldType) throws IllegalAccessException, NoSuchFieldException, SecurityException, NullPointerException
Return a MethodHandle that provides write access to a field. The MethodHandle will have a MethodType taking a two arguments, the first with typeclazz
and the second with typefieldType
, and returning void.- Parameters:
clazz
- - the class defining the fieldfieldName
- - the name of the fieldfieldType
- - the type of the field- Returns:
- a MethodHandle able to set the value of the field
- Throws:
IllegalAccessException
- 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 null
-
findStaticSetter
public MethodHandle findStaticSetter(Class<?> clazz, String fieldName, Class<?> fieldType) throws IllegalAccessException, NoSuchFieldException, SecurityException, NullPointerException
Return a MethodHandle that provides write access to a field. The MethodHandle will have a MethodType taking one argument of typefieldType
and returning void.- Parameters:
clazz
- - the class defining the fieldfieldName
- - the name of the fieldfieldType
- - the type of the field- Returns:
- a MethodHandle able to set the value of the field
- Throws:
IllegalAccessException
- 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 null
-
in
public MethodHandles.Lookup in(Class<?> lookupClass)
Create a lookup on the request class. The resulting lookup will have no more access privileges than the original.- Parameters:
lookupClass
- - the class to create the lookup on- Returns:
- a new MethodHandles.Lookup object
-
lookupClass
public Class<?> lookupClass()
The class being used for visibility checks and access permissions.- Returns:
- The class used in by this Lookup object for access checking
-
unreflect
public MethodHandle unreflect(Method method) throws IllegalAccessException
Make a MethodHandle to the Reflect method. If the method is non-static, the receiver argument is treated as the initial argument in the MethodType.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.- Parameters:
method
- - the reflect method- Returns:
- A MethodHandle able to invoke the reflect method
- Throws:
IllegalAccessException
- - if access is denied
-
unreflectConstructor
public MethodHandle unreflectConstructor(Constructor<?> method) throws IllegalAccessException
Return a MethodHandle for the reflect constructor. The MethodType has a return type of the declared class, and the arguments of the constructor. The MethodHandle creates a new object as through by newInstance.If the
accessible
flag is not set, then access checking is performed using the lookup class.- Parameters:
method
- - the Reflect constructor- Returns:
- a Methodhandle that creates new instances using the requested constructor
- Throws:
IllegalAccessException
- - if access is denied
-
findConstructor
public MethodHandle findConstructor(Class<?> declaringClass, MethodType type) throws IllegalAccessException, NoSuchMethodException
Return a MethodHandle that will create an object of the required class and initialize it using the constructor method with signature type. The MethodHandle will have a MethodType with the same parameters as the constructor method, but will have a return type of the declaringClass instead of void.- Parameters:
declaringClass
- - Class that declares the constructortype
- - the MethodType of the constructor. Return type must be void.- Returns:
- a MethodHandle able to construct and initialize the required class
- Throws:
IllegalAccessException
- if access is deniedNoSuchMethodException
- if the method doesn't existSecurityException
- if the SecurityManager prevents accessNullPointerException
- if any of the arguments are null
-
unreflectSpecial
public MethodHandle unreflectSpecial(Method method, Class<?> specialToken) throws IllegalAccessException
Return a MethodHandle for the Reflect method, that will directly call the requested method as through from the classspecialToken
. The MethodType of the method handle will be that of the method with the receiver argument prepended.- Parameters:
method
- - a Reflect methodspecialToken
- - the class the call is from- Returns:
- a MethodHandle that directly dispatches the requested method
- Throws:
IllegalAccessException
- - if access is denied
-
unreflectGetter
public MethodHandle unreflectGetter(Field field) throws IllegalAccessException
Create a MethodHandle that returns the value of the Reflect field. There are two cases:- a static field - which has the MethodType with only a return type of the field
- an instance field - which has the MethodType with a return type of the field and a single argument of the object that contains the field
If the
accessible
flag is not set, then access checking is performed using the lookup class.- Parameters:
field
- - a Reflect field- Returns:
- a MethodHandle able to return the field value
- Throws:
IllegalAccessException
- - if access is denied
-
unreflectSetter
public MethodHandle unreflectSetter(Field field) throws IllegalAccessException
Create a MethodHandle that sets the value of the Reflect field. All MethodHandles created here have a return type of void. For the arguments, there are two cases:- a static field - which takes a single argument the same as the field
- an instance field - which takes two arguments, the object that contains the field, and the type of the field
If the
accessible
flag is not set, then access checking is performed using the lookup class.- Parameters:
field
- - a Reflect field- Returns:
- a MethodHandle able to set the field value
- Throws:
IllegalAccessException
- - if access is denied
-
revealDirect
public MethodHandleInfo revealDirect(MethodHandle target) throws IllegalArgumentException, NullPointerException, SecurityException
Cracks a MethodHandle, which allows access to its symbolic parts. The MethodHandle must have been created by this Lookup object or one that is able to recreate the MethodHandle. If the Lookup object is not able to recreate the MethodHandle, the cracking may fail.- Parameters:
target
- The MethodHandle to be cracked- Returns:
- a MethodHandleInfo which provides access to the target's symbolic parts
- Throws:
IllegalArgumentException
- if the target is not a direct handle, or if the access check failsNullPointerException
- if the target is nullSecurityException
- if a SecurityManager denies access
-
findVarHandle
public VarHandle findVarHandle(Class<?> definingClass, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException
Factory method for obtaining a VarHandle referencing a non-static field.- Parameters:
definingClass
- The class that contains the fieldname
- The name of the fieldtype
- The exact type of the field- Returns:
- A VarHandle that can access the field
- Throws:
NoSuchFieldException
- If the field doesn't existIllegalAccessException
- If the field is not accessible or doesn't exist
-
findStaticVarHandle
public VarHandle findStaticVarHandle(Class<?> definingClass, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException
Factory method for obtaining a VarHandle referencing a static field.- Parameters:
definingClass
- The class that contains the fieldname
- The name of the fieldtype
- The type of the field- Returns:
- A VarHandle that can access the field
- Throws:
NoSuchFieldException
- If the field doesn't existIllegalAccessException
- If the field is not accessible or doesn't exist
-
unreflectVarHandle
public VarHandle unreflectVarHandle(Field field) throws IllegalAccessException
Create aVarHandle
that operates on the field represented by thereflect field
.Access checking is performed regardless of whether the reflect field's
accessible
flag is set.- Parameters:
field
- - a reflect field- Returns:
- a VarHandle able to operate on the field
- Throws:
IllegalAccessException
- - if access is denied
-
findClass
public Class<?> findClass(String targetName) throws ClassNotFoundException, IllegalAccessException
Search the target class by class name via the lookup context. The target class is not statically initialized.The lookup context depends upon the lookup class, its classloader and its access modes. This method tells whether the target class is accessible to this lookup class after successfully loading the target class.
- Parameters:
targetName
- - the name of the target class- Returns:
- the target class
- Throws:
ClassNotFoundException
- if the lookup class's classloader fails to load the target class.IllegalAccessException
- if access is deniedSecurityException
- if the SecurityManager prevents accessLinkageError
- if it fails in linking
-
accessClass
public Class<?> accessClass(Class<?> targetClass) throws IllegalAccessException
Tells whether the target class is accessible to the lookup class. The target class is not statically initialized.The lookup context depends upon the lookup class and its access modes.
- Parameters:
targetClass
- - the target class to be checked- Returns:
- the target class after check
- Throws:
IllegalAccessException
- if access is deniedSecurityException
- if the SecurityManager prevents access
-
defineClass
public Class<?> defineClass(byte[] classBytes) throws NullPointerException, IllegalAccessException, IllegalArgumentException, SecurityException, LinkageError
Return a class object with the same class loader, the same package and the same protection domain as the lookup's lookup class.- Parameters:
classBytes
- the requested class bytes from a valid class file- Returns:
- a class object for the requested class bytes
- Throws:
NullPointerException
- if the requested class bytes is nullIllegalArgumentException
- if the requested class belongs to another package different from the lookup classIllegalAccessException
- if the access mode of the lookup doesn't include PACKAGESecurityException
- if the SecurityManager prevents accessLinkageError
- if the requested class is malformed, or is already defined, or fails in bytecode verification or linking
-
dropLookupMode
public MethodHandles.Lookup dropLookupMode(int dropMode)
Return a MethodHandles.Lookup object without the requested lookup mode. Note: the requested mode must exists in the access mode; otherwise, do nothing.- Parameters:
dropMode
- the mode to be dropped which must exists in the access mode- Returns:
- a MethodHandles.Lookup object without the requested lookup mode
- Throws:
IllegalArgumentException
- - if the requested lookup mode is not one of the existing access modes
-
hasPrivateAccess
public boolean hasPrivateAccess()
Return true if the lookup class has private access- Returns:
- a boolean type indicating whether the lookup class has private access
-
-