A symbolic reference obtained by cracking a direct method handle into its consitutent symbolic parts. To crack a direct method handle, call
Lookup.revealDirect
.
Direct Method Handles
A
direct method handle represents a method, constructor, or field without any intervening argument bindings or other transformations. The method, constructor, or field referred to by a direct method handle is called its
underlying member . Direct method handles may be obtained in any of these ways:
Restrictions on Cracking
Given a suitable
Lookup
object, it is possible to crack any direct method handle to recover a symbolic reference for the underlying method, constructor, or field. Cracking must be done via a
Lookup
object equivalent to that which created the target method handle, or which has enough access permissions to recreate an equivalent method handle.
If the underlying method is caller sensitive , the direct method handle will have been "bound" to a particular caller class, the lookup class of the lookup object used to create it. Cracking this method handle with a different lookup class will fail even if the underlying method is public (like Class.forName
).
The requirement of lookup object matching provides a "fast fail" behavior for programs which may otherwise trust erroneous revelation of a method handle with symbolic information (or caller binding) from an unexpected scope. Use MethodHandles.reflectAs(java.lang.Class<T>, java.lang.invoke.MethodHandle)
to override this limitation.
Reference kinds
The
Lookup Factory Methods correspond to all major use cases for methods, constructors, and fields. These use cases may be distinguished using small integers as follows:
reference kinds
reference kind | descriptive name | scope | member | behavior |
1 | REF_getField | class | FT f; | (T) this.f; |
2 | REF_getStatic | class or interface | static
FT f; | (T) C.f; |
3 | REF_putField | class | FT f; | this.f = x; |
4 | REF_putStatic | class | static
FT f; | C.f = arg; |
5 | REF_invokeVirtual | class | T m(A*); | (T) this.m(arg*); |
6 | REF_invokeStatic | class or interface | static
T m(A*); | (T) C.m(arg*); |
7 | REF_invokeSpecial | class or interface | T m(A*); | (T) super.m(arg*); |
8 | REF_newInvokeSpecial | class | C(A*); | new C(arg*); |
9 | REF_invokeInterface | interface | T m(A*); | (T) this.m(arg*); |