|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.dyn.MethodHandles.Lookup
public static final class MethodHandles.Lookup
Disabled: no SafeJ information.
A factory object for creating method handles, when the creation
requires access checking. Method handles do not perform
access checks when they are called; this is a major difference
from reflective Method
, which performs access checking
against every caller, on every call. Method handle access
restrictions are enforced when a method handle is created.
The caller class against which those restrictions are enforced
is known as the "lookup class". MethodHandles.Lookup
embodies an
authenticated lookup class, and can be used to create any number
of access-checked method handles, all checked against a single
lookup class.
A class which needs to create method handles will call
MethodHandles.lookup()
to create a factory for itself.
It may then use this factory to create method handles on
all of its methods, including private ones.
It may also delegate the lookup (e.g., to a metaobject protocol)
by passing the Lookup
object to other code.
If this other code creates method handles, they will be access
checked against the original lookup class, and not with any higher
privileges.
Note that access checks only apply to named and reflected methods.
Other method handle creation methods, such as MethodHandles.convertArguments(java.dyn.MethodHandle, java.dyn.MethodType)
,
do not require any access checks, and can be done independently
of any lookup class.
A note about error conditions: A lookup can fail, because
the containing class is not accessible to the lookup class, or
because the desired class member is missing, or because the
desired class member is not accessible to the lookup class.
It can also fail if a security manager is installed and refuses
access. In any of these cases, an exception will be
thrown from the attempted lookup.
In general, the conditions under which a method handle may be
created for a method M
are exactly as restrictive as the conditions
under which the lookup class could have compiled a call to M
.
At least some of these error conditions are likely to be
represented by checked exceptions in the final version of this API.
Method Summary | |
---|---|
MethodHandle |
bind(Object receiver,
String name,
MethodType type)
Produce an early-bound method handle for a non-static method. |
MethodHandle |
findSpecial(Class<?> defc,
String name,
MethodType type,
Class<?> specialCaller)
Produce an early-bound method handle for a virtual method, as if called from an invokespecial
instruction from caller . |
MethodHandle |
findStatic(Class<?> defc,
String name,
MethodType type)
Produce a method handle for a static method. |
MethodHandle |
findVirtual(Class<?> defc,
String name,
MethodType type)
Produce a method handle for a virtual method. |
MethodHandles.Lookup |
in(Class<?> newLookupClass)
Create a lookup on the specified class. |
Class<?> |
lookupClass()
Which class is performing the lookup? It is this class against which checks are performed for visibility and access permissions. |
String |
toString()
Returns a string representation of the object. |
MethodHandle |
unreflect(Method m)
PROVISIONAL API, WORK IN PROGRESS: Make a direct method handle to m, if the lookup class has permission. |
MethodHandle |
unreflectConstructor(Constructor ctor)
PROVISIONAL API, WORK IN PROGRESS: Produce a method handle for a reflected constructor. |
MethodHandle |
unreflectGetter(Field f)
PROVISIONAL API, WORK IN PROGRESS: Produce a method handle giving read access to a reflected field. |
MethodHandle |
unreflectSetter(Field f)
PROVISIONAL API, WORK IN PROGRESS: Produce a method handle giving write access to a reflected field. |
MethodHandle |
unreflectSpecial(Method m,
Class<?> specialCaller)
PROVISIONAL API, WORK IN PROGRESS: Produce a method handle for a reflected method. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Method Detail |
---|
public Class<?> lookupClass()
This value is null if and only if this lookup was produced
by MethodHandles.publicLookup()
.
public MethodHandles.Lookup in(Class<?> newLookupClass)
public String toString()
Object
toString
method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString
method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@
', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
toString
in class Object
public MethodHandle findStatic(Class<?> defc, String name, MethodType type) throws NoAccessException
defc
- the class from which the method is accessedname
- the name of the methodtype
- the type of the method
SecurityException
- TBD
NoAccessException
- if the method does not exist or access checking failspublic MethodHandle findVirtual(Class<?> defc, String name, MethodType type) throws NoAccessException
defc
) prepended.
The method and all its argument types must be accessible to the lookup class.
(BUG NOTE: The type Object
may be prepended instead
of the receiver type, if the receiver type is not on the boot class path.
This is due to a temporary JVM limitation, in which MethodHandle
claims to be unable to access such classes. To work around this
bug, use convertArguments
to normalize the type of the leading
argument to a type on the boot class path, such as 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.)
defc
- the class or interface from which the method is accessedname
- the name of the methodtype
- the type of the method, with the receiver argument omitted
SecurityException
- TBD
NoAccessException
- if the method does not exist or access checking failspublic MethodHandle findSpecial(Class<?> defc, String name, MethodType type, Class<?> specialCaller) throws NoAccessException
invokespecial
instruction from caller
.
The type of the method handle will be that of the method,
with a suitably restricted receiver type (such as caller
) prepended.
The method and all its argument types must be accessible
to the caller.
When called, the handle will treat the first argument as a receiver,
but will not dispatch on the receiver's type.
(This direct invocation action is identical with that performed by an
invokespecial
instruction.)
If the explicitly specified caller class is not identical with the lookup class, a security check TBD is performed.
defc
- the class or interface from which the method is accessedname
- the name of the method, or "type
- the type of the method, with the receiver argument omittedspecialCaller
- the proposed calling class to perform the invokespecial
SecurityException
- TBD
NoAccessException
- if the method does not exist or access checking failspublic MethodHandle bind(Object receiver, String name, MethodType type) throws NoAccessException
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 class.
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.
This is equivalent to the following expression:
where MethodHandles.insertArguments(java.dyn.MethodHandle, int, java.lang.Object...)
(findVirtual(java.lang.Class, java.lang.String, java.dyn.MethodType)
(defc, name, type), receiver)
defc
is either receiver.getClass()
or a super
type of that class, in which the requested method is accessible
to the lookup class.
receiver
- the object from which the method is accessedname
- the name of the methodtype
- the type of the method, with the receiver argument omitted
SecurityException
- TBD
NoAccessException
- if the method does not exist or access checking failspublic MethodHandle unreflect(Method m) throws NoAccessException
accessible
flag is not set,
access checking is performed immediately on behalf of the lookup class.
If m is not public, do not share the resulting handle with untrusted parties.
m
- the reflected method
NoAccessException
- if access checking failspublic MethodHandle unreflectSpecial(Method m, Class<?> specialCaller) throws NoAccessException
invokespecial
instruction.
The type of the method handle will be that of the method,
with the receiver type prepended.
If the method's accessible
flag is not set,
access checking is performed immediately on behalf of the lookup class,
as if invokespecial
instruction were being linked.
m
- the reflected method
NoAccessException
- if access checking failspublic MethodHandle unreflectConstructor(Constructor ctor) throws NoAccessException
newInstance
operation,
creating a new instance of the constructor's class on the
arguments passed to the method handle.
If the constructor's accessible
flag is not set,
access checking is performed immediately on behalf of the lookup class.
ctor
- the reflected constructor
NoAccessException
- if access checking failspublic MethodHandle unreflectGetter(Field f) throws NoAccessException
accessible
flag is not set,
access checking is performed immediately on behalf of the lookup class.
f
- the reflected field
NoAccessException
- if access checking failspublic MethodHandle unreflectSetter(Field f) throws NoAccessException
accessible
flag is not set,
access checking is performed immediately on behalf of the lookup class.
f
- the reflected field
NoAccessException
- if access checking fails
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |