MethodHandleInfo (Java SE 11 & JDK 11 )
-
-
Field Detail
-
REF_getField
static final int REF_getField
A direct method handle reference kind, as defined in the table above.
- See Also:
- Constant Field Values
-
REF_getStatic
static final int REF_getStatic
A direct method handle reference kind, as defined in the table above.
- See Also:
- Constant Field Values
-
REF_putField
static final int REF_putField
A direct method handle reference kind, as defined in the table above.
- See Also:
- Constant Field Values
-
REF_putStatic
static final int REF_putStatic
A direct method handle reference kind, as defined in the table above.
- See Also:
- Constant Field Values
-
REF_invokeVirtual
static final int REF_invokeVirtual
A direct method handle reference kind, as defined in the table above.
- See Also:
- Constant Field Values
-
REF_invokeStatic
static final int REF_invokeStatic
A direct method handle reference kind, as defined in the table above.
- See Also:
- Constant Field Values
-
REF_invokeSpecial
static final int REF_invokeSpecial
A direct method handle reference kind, as defined in the table above.
- See Also:
- Constant Field Values
-
REF_newInvokeSpecial
static final int REF_newInvokeSpecial
A direct method handle reference kind, as defined in the table above.
- See Also:
- Constant Field Values
-
REF_invokeInterface
static final int REF_invokeInterface
A direct method handle reference kind, as defined in the table above.
- See Also:
- Constant Field Values
-
-
Method Detail
-
getReferenceKind
int getReferenceKind()
Returns the reference kind of the cracked method handle, which in turn determines whether the method handle's underlying member was a constructor, method, or field. See the table above for definitions.
- Returns:
- the integer code for the kind of reference used to access the underlying member
-
getDeclaringClass
Class<?> getDeclaringClass()
Returns the class in which the cracked method handle's underlying member was defined.
- Returns:
- the declaring class of the underlying member
-
getName
String getName()
Returns the name of the cracked method handle's underlying member. This is
"<init>"if the underlying member was a constructor, else it is a simple method name or field name.- Returns:
- the simple name of the underlying member
-
getMethodType
MethodType getMethodType()
Returns the nominal type of the cracked symbolic reference, expressed as a method type. If the reference is to a constructor, the return type will be
void. If it is to a non-static method, the method type will not mention thethisparameter. If it is to a field and the requested access is to read the field, the method type will have no parameters and return the field type. If it is to a field and the requested access is to write the field, the method type will have one parameter of the field type and returnvoid.Note that original direct method handle may include a leading
thisparameter, or (in the case of a constructor) will replace thevoidreturn type with the constructed class. The nominal type does not include anythisparameter, and (in the case of a constructor) will returnvoid.- Returns:
- the type of the underlying member, expressed as a method type
-
reflectAs
<T extends Member> T reflectAs​(Class<T> expected, MethodHandles.Lookup lookup)
Reflects the underlying member as a method, constructor, or field object. If the underlying member is public, it is reflected as if by
getMethod,getConstructor, orgetField. Otherwise, it is reflected as if bygetDeclaredMethod,getDeclaredConstructor, orgetDeclaredField. The underlying member must be accessible to the given lookup object.- Type Parameters:
T- the desired type of the result, eitherMemberor a subtype- Parameters:
expected- a class object representing the desired result typeTlookup- the lookup object that created this MethodHandleInfo, or one with equivalent access privileges- Returns:
- a reference to the method, constructor, or field object
- Throws:
ClassCastException- if the member is not of the expected typeNullPointerException- if either argument isnullIllegalArgumentException- if the underlying member is not accessible to the given lookup object
-
getModifiers
int getModifiers()
Returns the access modifiers of the underlying member.
- Returns:
- the Java language modifiers for underlying member, or -1 if the member cannot be accessed
- See Also:
Modifier,reflectAs(java.lang.Class<T>, java.lang.invoke.MethodHandles.Lookup)
-
isVarArgs
default boolean isVarArgs()
Determines if the underlying member was a variable arity method or constructor. Such members are represented by method handles that are varargs collectors.
- Implementation Requirements:
- This produces a result equivalent to:
getReferenceKind() >= REF_invokeVirtual && Modifier.isTransient(getModifiers()) - Returns:
trueif and only if the underlying member was declared with variable arity.
-
referenceKindToString
static String referenceKindToString​(int referenceKind)
Returns the descriptive name of the given reference kind, as defined in the table above. The conventional prefix "REF_" is omitted.
- Parameters:
referenceKind- an integer code for a kind of reference used to access a class member- Returns:
- a mixed-case string such as
"getField" - Throws:
IllegalArgumentException- if the argument is not a valid reference kind number
-
toString
static String toString​(int kind, Class<?> defc, String name, MethodType type)
Returns a string representation for a
MethodHandleInfo, given the four parts of its symbolic reference. This is defined to be of the form"RK C.N:MT", whereRKis the reference kind string forkind,Cis the name ofdefcNis thename, andMTis thetype. These four values may be obtained from the reference kind, declaring class, member name, and method type of aMethodHandleInfoobject.- Implementation Requirements:
- This produces a result equivalent to:
String.format("%s %s.%s:%s", referenceKindToString(kind), defc.getName(), name, type) - Parameters:
kind- the reference kind part of the symbolic referencedefc- the declaring class part of the symbolic referencename- the member name part of the symbolic referencetype- the method type part of the symbolic reference- Returns:
- a string of the form
"RK C.N:MT" - Throws:
IllegalArgumentException- if the first argument is not a valid reference kind numberNullPointerException- if any reference argument isnull
-
-