GeezerVison is showing docs.oracle.com — MethodHandles.Lookup (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
compact1, compact2, compact3
java.lang.invoke
Class MethodHandles.Lookup
-
-
Field Summary
Fields
| Modifier and Type |
Field |
Description |
static int |
PACKAGE |
A single-bit mask representing package access (default access),
which may contribute to the result of lookupModes.
|
static int |
PRIVATE |
A single-bit mask representing private access,
which may contribute to the result of lookupModes.
|
static int |
PROTECTED |
A single-bit mask representing protected access,
which may contribute to the result of lookupModes.
|
static int |
PUBLIC |
A single-bit mask representing public access,
which may contribute to the result of lookupModes.
|
-
Method Summary
All Methods Instance Methods Concrete Methods
| Modifier and Type |
Method |
Description |
MethodHandle |
bind(Object receiver,
String name,
MethodType type) |
Produces an early-bound method handle for a non-static method.
|
MethodHandle |
findConstructor(Class<?> refc,
MethodType type) |
Produces a method handle which creates an object and initializes it, using
the constructor of the specified type.
|
MethodHandle |
findGetter(Class<?> refc,
String name,
Class<?> type) |
Produces a method handle giving read access to a non-static field.
|
MethodHandle |
findSetter(Class<?> refc,
String name,
Class<?> type) |
Produces a method handle giving write access to a non-static field.
|
MethodHandle |
findSpecial(Class<?> refc,
String name,
MethodType type,
Class<?> specialCaller) |
Produces an early-bound method handle for a virtual method.
|
MethodHandle |
findStatic(Class<?> refc,
String name,
MethodType type) |
Produces a method handle for a static method.
|
MethodHandle |
findStaticGetter(Class<?> refc,
String name,
Class<?> type) |
Produces a method handle giving read access to a static field.
|
MethodHandle |
findStaticSetter(Class<?> refc,
String name,
Class<?> type) |
Produces a method handle giving write access to a static field.
|
MethodHandle |
findVirtual(Class<?> refc,
String name,
MethodType type) |
Produces a method handle for a virtual method.
|
MethodHandles.Lookup |
in(Class<?> requestedLookupClass) |
Creates a lookup on the specified new lookup class.
|
Class<?> |
lookupClass() |
Tells which class is performing the lookup.
|
int |
lookupModes() |
Tells which access-protection classes of members this lookup object can produce.
|
MethodHandleInfo |
revealDirect(MethodHandle target) |
|
String |
toString() |
Displays the name of the class from which lookups are to be made.
|
MethodHandle |
unreflect(Method m) |
|
MethodHandle |
unreflectConstructor(Constructor<?> c) |
Produces a method handle for a reflected constructor.
|
MethodHandle |
unreflectGetter(Field f) |
Produces a method handle giving read access to a reflected field.
|
MethodHandle |
unreflectSetter(Field f) |
Produces a method handle giving write access to a reflected field.
|
MethodHandle |
unreflectSpecial(Method m,
Class<?> specialCaller) |
Produces a method handle for a reflected method.
|
-
-
Method Detail
-
lookupModes
public int lookupModes()
Tells which access-protection classes of members this lookup object can produce.
The result is a bit-mask of the bits
PUBLIC (0x01),
PRIVATE (0x02),
PROTECTED (0x04),
and PACKAGE (0x08).
A freshly-created lookup object
on the caller's class
has all possible bits set, since the caller class can access all its own members.
A lookup object on a new lookup class
created from a previous lookup object
may have some mode bits set to zero.
The purpose of this is to restrict access via the new lookup object,
so that it can access only names which can be reached by the original
lookup object, and also by the new lookup class.
- Returns:
- the lookup modes, which limit the kinds of access performed by this lookup object
-
toString
public String toString()
Displays the name of the class from which lookups are to be made.
(The name is the one reported by Class.getName.)
If there are restrictions on the access permitted to this lookup,
this is indicated by adding a suffix to the class name, consisting
of a slash and a keyword. The keyword represents the strongest
allowed access, and is chosen as follows:
- If no access is allowed, the suffix is "/noaccess".
- If only public access is allowed, the suffix is "/public".
- If only public and package access are allowed, the suffix is "/package".
- If only public, package, and private access are allowed, the suffix is "/private".
If none of the above cases apply, it is the case that full
access (public, package, private, and protected) is allowed.
In this case, no suffix is added.
This is true only of an object obtained originally from
MethodHandles.lookup.
Objects created by Lookup.in
always have restricted access, and will display a suffix.
(It may seem strange that protected access should be
stronger than private access. Viewed independently from
package access, protected access is the first to be lost,
because it requires a direct subclass relationship between
caller and callee.)
- Overrides:
toString in class Object
- Returns:
- a string representation of the object.
- See Also:
in(java.lang.Class<?>)
-
findStatic
public MethodHandle findStatic(Class<?> refc,
String name,
MethodType type)
throws NoSuchMethodException,
IllegalAccessException
Produces a method handle for a static method.
The type of the method handle will be that of the method.
(Since static methods do not take receivers, there is no
additional receiver argument inserted into the method handle type,
as there would be with findVirtual or findSpecial.)
The method and all its argument types must be accessible to the lookup object.
The returned method handle will have
variable arity if and only if
the method's variable arity modifier bit (0x0080) is set.
If the returned method handle is invoked, the method's class will
be initialized, if it has not already been initialized.
Example:
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.*;
...
MethodHandle MH_asList = publicLookup().findStatic(Arrays.class,
"asList", methodType(List.class, Object[].class));
assertEquals("[x, y]", MH_asList.invoke("x", "y").toString());
- Parameters:
refc - the class from which the method is accessed
name - the name of the method
type - the type of the method
- Returns:
- the desired method handle
- Throws:
NoSuchMethodException - if the method does not exist
IllegalAccessException - if access checking fails,
or if the method is not static,
or if the method's variable arity modifier bit
is set and asVarargsCollector fails
SecurityException - if a security manager is present and it
refuses access
NullPointerException - if any argument is null
-
findVirtual
public MethodHandle findVirtual(Class<?> refc,
String name,
MethodType type)
throws NoSuchMethodException,
IllegalAccessException
Produces a method handle for a virtual method.
The type of the method handle will be that of the method,
with the receiver type (usually refc) prepended.
The method and all its argument types must be accessible to the lookup object.
When called, the handle will treat the first argument as a receiver
and dispatch on the receiver's type to determine which method
implementation to enter.
(The dispatching action is identical with that performed by an
invokevirtual or invokeinterface instruction.)
The first argument will be of type refc if the lookup
class has full privileges to access the member. Otherwise
the member must be protected and the first argument
will be restricted in type to the lookup class.
The returned method handle will have
variable arity if and only if
the method's variable arity modifier bit (0x0080) is set.
Because of the general equivalence between invokevirtual
instructions and method handles produced by findVirtual,
if the class is MethodHandle and the name string is
invokeExact or invoke, the resulting
method handle is equivalent to one produced by
MethodHandles.exactInvoker or
MethodHandles.invoker
with the same type argument.
Example:
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.*;
...
MethodHandle MH_concat = publicLookup().findVirtual(String.class,
"concat", methodType(String.class, String.class));
MethodHandle MH_hashCode = publicLookup().findVirtual(Object.class,
"hashCode", methodType(int.class));
MethodHandle MH_hashCode_String = publicLookup().findVirtual(String.class,
"hashCode", methodType(int.class));
assertEquals("xy", (String) MH_concat.invokeExact("x", "y"));
assertEquals("xy".hashCode(), (int) MH_hashCode.invokeExact((Object)"xy"));
assertEquals("xy".hashCode(), (int) MH_hashCode_String.invokeExact("xy"));
// interface method:
MethodHandle MH_subSequence = publicLookup().findVirtual(CharSequence.class,
"subSequence", methodType(CharSequence.class, int.class, int.class));
assertEquals("def", MH_subSequence.invoke("abcdefghi", 3, 6).toString());
// constructor "internal method" must be accessed differently:
MethodType MT_newString = methodType(void.class); //()V for new String()
try { assertEquals("impossible", lookup()
.findVirtual(String.class, "<init>", MT_newString));
} catch (NoSuchMethodException ex) { } // OK
MethodHandle MH_newString = publicLookup()
.findConstructor(String.class, MT_newString);
assertEquals("", (String) MH_newString.invokeExact());
- Parameters:
refc - the class or interface from which the method is accessed
name - the name of the method
type - the type of the method, with the receiver argument omitted
- Returns:
- the desired method handle
- Throws:
NoSuchMethodException - if the method does not exist
IllegalAccessException - if access checking fails,
or if the method is static
or if the method's variable arity modifier bit
is set and asVarargsCollector fails
SecurityException - if a security manager is present and it
refuses access
NullPointerException - if any argument is null
-
findSpecial
public MethodHandle findSpecial(Class<?> refc,
String name,
MethodType type,
Class<?> specialCaller)
throws NoSuchMethodException,
IllegalAccessException
Produces an early-bound method handle for a virtual method.
It will bypass checks for overriding methods on the receiver,
as if called from an invokespecial
instruction from within the explicitly specified specialCaller.
The type of the method handle will be that of the method,
with a suitably restricted receiver type prepended.
(The receiver type will be specialCaller or a subtype.)
The method and all its argument types must be accessible
to the lookup object.
Before method resolution,
if the explicitly specified caller class is not identical with the
lookup class, or if this lookup object does not have
private access
privileges, the access fails.
The returned method handle will have
variable arity if and only if
the method's variable arity modifier bit (0x0080) is set.
(Note: JVM internal methods named "<init>" are not visible to this API,
even though the invokespecial instruction can refer to them
in special circumstances. Use findConstructor
to access instance initialization methods in a safe manner.)
Example:
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.*;
...
static class Listie extends ArrayList {
public String toString() { return "[wee Listie]"; }
static Lookup lookup() { return MethodHandles.lookup(); }
}
...
// no access to constructor via invokeSpecial:
MethodHandle MH_newListie = Listie.lookup()
.findConstructor(Listie.class, methodType(void.class));
Listie l = (Listie) MH_newListie.invokeExact();
try { assertEquals("impossible", Listie.lookup().findSpecial(
Listie.class, "<init>", methodType(void.class), Listie.class));
} catch (NoSuchMethodException ex) { } // OK
// access to super and self methods via invokeSpecial:
MethodHandle MH_super = Listie.lookup().findSpecial(
ArrayList.class, "toString" , methodType(String.class), Listie.class);
MethodHandle MH_this = Listie.lookup().findSpecial(
Listie.class, "toString" , methodType(String.class), Listie.class);
MethodHandle MH_duper = Listie.lookup().findSpecial(
Object.class, "toString" , methodType(String.class), Listie.class);
assertEquals("[]", (String) MH_super.invokeExact(l));
assertEquals(""+l, (String) MH_this.invokeExact(l));
assertEquals("[]", (String) MH_duper.invokeExact(l)); // ArrayList method
try { assertEquals("inaccessible", Listie.lookup().findSpecial(
String.class, "toString", methodType(String.class), Listie.class));
} catch (IllegalAccessException ex) { } // OK
Listie subl = new Listie() { public String toString() { return "[subclass]"; } };
assertEquals(""+l, (String) MH_this.invokeExact(subl)); // Listie method
- Parameters:
refc - the class or interface from which the method is accessed
name - the name of the method (which must not be "<init>")
type - the type of the method, with the receiver argument omitted
specialCaller - the proposed calling class to perform the invokespecial
- Returns:
- the desired method handle
- Throws:
NoSuchMethodException - if the method does not exist
IllegalAccessException - if access checking fails
or if the method's variable arity modifier bit
is set and asVarargsCollector fails
SecurityException - if a security manager is present and it
refuses access
NullPointerException - if any argument is null
-
findGetter
public MethodHandle findGetter(Class<?> refc,
String name,
Class<?> type)
throws NoSuchFieldException,
IllegalAccessException
Produces a method handle giving read access to a non-static field.
The type of the method handle will have a return type of the field's
value type.
The method handle's single argument will be the instance containing
the field.
Access checking is performed immediately on behalf of the lookup class.
- Parameters:
refc - the class or interface from which the method is accessed
name - the field's name
type - the field's type
- Returns:
- a method handle which can load values from the field
- Throws:
NoSuchFieldException - if the field does not exist
IllegalAccessException - if access checking fails, or if the field is static
SecurityException - if a security manager is present and it
refuses access
NullPointerException - if any argument is null
-
findSetter
public MethodHandle findSetter(Class<?> refc,
String name,
Class<?> type)
throws NoSuchFieldException,
IllegalAccessException
Produces a method handle giving write access to a non-static field.
The type of the method handle will have a void return type.
The method handle will take two arguments, the instance containing
the field, and the value to be stored.
The second argument will be of the field's value type.
Access checking is performed immediately on behalf of the lookup class.
- Parameters:
refc - the class or interface from which the method is accessed
name - the field's name
type - the field's type
- Returns:
- a method handle which can store values into the field
- Throws:
NoSuchFieldException - if the field does not exist
IllegalAccessException - if access checking fails, or if the field is static
SecurityException - if a security manager is present and it
refuses access
NullPointerException - if any argument is null
-
findStaticGetter
public MethodHandle findStaticGetter(Class<?> refc,
String name,
Class<?> type)
throws NoSuchFieldException,
IllegalAccessException
Produces a method handle giving read access to a static field.
The type of the method handle will have a return type of the field's
value type.
The method handle will take no arguments.
Access checking is performed immediately on behalf of the lookup class.
If the returned method handle is invoked, the field's class will
be initialized, if it has not already been initialized.
- Parameters:
refc - the class or interface from which the method is accessed
name - the field's name
type - the field's type
- Returns:
- a method handle which can load values from the field
- Throws:
NoSuchFieldException - if the field does not exist
IllegalAccessException - if access checking fails, or if the field is not static
SecurityException - if a security manager is present and it
refuses access
NullPointerException - if any argument is null
-
findStaticSetter
public MethodHandle findStaticSetter(Class<?> refc,
String name,
Class<?> type)
throws NoSuchFieldException,
IllegalAccessException
Produces a method handle giving write access to a static field.
The type of the method handle will have a void return type.
The method handle will take a single
argument, of the field's value type, the value to be stored.
Access checking is performed immediately on behalf of the lookup class.
If the returned method handle is invoked, the field's class will
be initialized, if it has not already been initialized.
- Parameters:
refc - the class or interface from which the method is accessed
name - the field's name
type - the field's type
- Returns:
- a method handle which can store values into the field
- Throws:
NoSuchFieldException - if the field does not exist
IllegalAccessException - if access checking fails, or if the field is not static
SecurityException - if a security manager is present and it
refuses access
NullPointerException - if any argument is null
-
bind
public MethodHandle bind(Object receiver,
String name,
MethodType type)
throws NoSuchMethodException,
IllegalAccessException
Produces an early-bound method handle for a non-static method.
The receiver must have a supertype defc in which a method
of the given name and type is accessible to the lookup class.
The method and all its argument types must be accessible to the lookup object.
The type of the method handle will be that of the method,
without any insertion of an additional receiver parameter.
The given receiver will be bound into the method handle,
so that every call to the method handle will invoke the
requested method on the given receiver.
The returned method handle will have
variable arity if and only if
the method's variable arity modifier bit (0x0080) is set
and the trailing array argument is not the only argument.
(If the trailing array argument is the only argument,
the given receiver value will be bound to it.)
This is equivalent to the following code:
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.*;
...
MethodHandle mh0 = lookup().findVirtual(defc, name, type);
MethodHandle mh1 = mh0.bindTo(receiver);
MethodType mt1 = mh1.type();
if (mh0.isVarargsCollector())
mh1 = mh1.asVarargsCollector(mt1.parameterType(mt1.parameterCount()-1));
return mh1;
where defc is either receiver.getClass() or a super
type of that class, in which the requested method is accessible
to the lookup class.
(Note that bindTo does not preserve variable arity.)
- Parameters:
receiver - the object from which the method is accessed
name - the name of the method
type - the type of the method, with the receiver argument omitted
- Returns:
- the desired method handle
- Throws:
NoSuchMethodException - if the method does not exist
IllegalAccessException - if access checking fails
or if the method's variable arity modifier bit
is set and asVarargsCollector fails
SecurityException - if a security manager is present and it
refuses access
NullPointerException - if any argument is null
- See Also:
MethodHandle.bindTo(java.lang.Object),
findVirtual(java.lang.Class<?>, java.lang.String, java.lang.invoke.MethodType)
Java™ Platform Standard Ed. 8
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, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|