Spec-Zone .ru
спецификации, руководства, описания, API
|
|
Java™ Platform Standard Ed. 7 DRAFT ea-b118 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.dyn.MethodHandles.Lookup
public static final class MethodHandles.Lookup extends Object
A lookup object is a factory for creating method handles,
when the creation requires access checking.
Method handles do not perform
access checks when they are called, but rather when they are created.
(This is a major difference
from reflective Method
, which performs access checking
against every caller, on every call.)
Therefore, method handle access
restrictions must be enforced when a method handle is created.
The caller class against which those restrictions are enforced
is known as the lookup class.
A lookup object 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.
Access checks only apply to named and reflected methods.
Other method handle creation methods, such as
MethodHandles.convertArguments
,
do not require any access checks, and can be done independently
of any lookup class.
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
.
This rule is applied even if the Java compiler might have created
an wrapper method to access a private method of another class
in the same top-level declaration.
For example, a lookup object created for a nested class C.D
can access private members within other related classes such as
C
, C.D.E
, or C.B
.
Modifier and Type | Method and Description |
---|---|
MethodHandle |
bind(Object receiver,
String name,
MethodType type)
Produce an early-bound method handle for a non-static method. |
MethodHandle |
findConstructor(Class<?> refc,
MethodType type)
Produce 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)
PROVISIONAL API, WORK IN PROGRESS: Produce a method handle giving read access to a non-static field. |
MethodHandle |
findSetter(Class<?> refc,
String name,
Class<?> type)
PROVISIONAL API, WORK IN PROGRESS: Produce a method handle giving write access to a non-static field. |
MethodHandle |
findSpecial(Class<?> refc,
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<?> refc,
String name,
MethodType type)
Produce a method handle for a static method. |
MethodHandle |
findStaticGetter(Class<?> refc,
String name,
Class<?> type)
PROVISIONAL API, WORK IN PROGRESS: Produce a method handle giving read access to a static field. |
MethodHandle |
findStaticSetter(Class<?> refc,
String name,
Class<?> type)
PROVISIONAL API, WORK IN PROGRESS: Produce a method handle giving write access to a static field. |
MethodHandle |
findVirtual(Class<?> refc,
String name,
MethodType type)
Produce a method handle for a virtual method. |
MethodHandles.Lookup |
in(Class<?> requestedLookupClass)
Create a lookup on the specified new lookup class. |
Class<?> |
lookupClass()
Which class is performing the lookup? It is this class against which checks are performed for visibility and access permissions. |
int |
lookupModes()
Which types of members can this lookup object produce? The result is a bit-mask of the Modifier bits
PUBLIC (0x01),
PROTECTED (0x02),
PRIVATE (0x04),
and STATIC (0x08). |
String |
toString()
Display the name of the class. |
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 c)
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()
The class implies a maximum level of access permission,
but the permissions may be additionally limited by the bitmask
lookupModes()
, which controls whether non-public members
can be accessed.
public int lookupModes()
Modifier
bits
PUBLIC (0x01),
PROTECTED (0x02),
PRIVATE (0x04),
and STATIC (0x08).
The modifier bit STATIC
stands in for the package protection mode,
which does not have an explicit modifier bit.
public MethodHandles.Lookup in(Class<?> requestedLookupClass)
lookupClass
.
However, the resulting Lookup
object is guaranteed
to have no more access capabilities than the original.
In particular:
public String toString()
toString
in class Object
public MethodHandle findStatic(Class<?> refc, String name, MethodType type) throws NoAccessException
findVirtual(java.lang.Class<?>, java.lang.String, java.dyn.MethodType)
or findSpecial(java.lang.Class<?>, java.lang.String, java.dyn.MethodType, java.lang.Class<?>)
.)
The method and all its argument types must be accessible to the lookup class.
If the method's class has not yet been initialized, that is done
immediately, before the method handle is returned.
refc
- the class from which the method is accessedname
- the name of the methodtype
- the type of the methodSecurityException
- TBDNoAccessException
- if the method does not exist or access checking failspublic MethodHandle findVirtual(Class<?> refc, String name, MethodType type) throws NoAccessException
refc
) 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.)
refc
- the class or interface from which the method is accessedname
- the name of the methodtype
- the type of the method, with the receiver argument omittedSecurityException
- TBDNoAccessException
- if the method does not exist or access checking failspublic MethodHandle findConstructor(Class<?> refc, MethodType type) throws NoAccessException
Note: The requested type must have a return type of void
.
This is consistent with the JVM's treatment of constructor signatures.
refc
- the class or interface from which the method is accessedtype
- the type of the method, with the receiver argument omitted, and a void return typeSecurityException
- TBDNoAccessException
- if the method does not exist or access checking failspublic MethodHandle findSpecial(Class<?> refc, 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.
refc
- the class or interface from which the method is accessedname
- the name of the method (which must not be "<init>")type
- the type of the method, with the receiver argument omittedspecialCaller
- the proposed calling class to perform the invokespecial
SecurityException
- TBDNoAccessException
- if the method does not exist or access checking failspublic MethodHandle findGetter(Class<?> refc, String name, Class<?> type) throws NoAccessException
name
- the field's nametype
- the field's typeNoAccessException
- if access checking failspublic MethodHandle findSetter(Class<?> refc, String name, Class<?> type) throws NoAccessException
name
- the field's nametype
- the field's typeNoAccessException
- if access checking failspublic MethodHandle findStaticGetter(Class<?> refc, String name, Class<?> type) throws NoAccessException
name
- the field's nametype
- the field's typeNoAccessException
- if access checking failspublic MethodHandle findStaticSetter(Class<?> refc, String name, Class<?> type) throws NoAccessException
name
- the field's nametype
- the field's typeNoAccessException
- if 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 insertArguments
(findVirtual
(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 omittedSecurityException
- TBDNoAccessException
- 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 methodNoAccessException
- if access checking failspublic MethodHandle unreflectSpecial(Method m, Class<?> specialCaller) throws NoAccessException
invokespecial
instruction from within the specialCaller
.
The type of the method handle will be that of the method,
with the special caller type prepended (and not the receiver of the method).
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 methodspecialCaller
- the class nominally calling the methodNoAccessException
- if access checking failspublic MethodHandle unreflectConstructor(Constructor c) 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.
c
- the reflected constructorNoAccessException
- 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 fieldNoAccessException
- 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 fieldNoAccessException
- if access checking fails
|
Java™ Platform Standard Ed. 7 DRAFT ea-b118 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright © 1993, 2010, Oracle Corporation. All rights reserved.