Interface MethodHandleInfo


public interfaceMethodHandleInfo
A symbolic reference obtained by cracking a direct method handleinto its constituent symbolic parts.To crack a direct method handle, callLookup.revealDirect.

Direct Method Handles

Adirect method handle represents a method, constructor, or field withoutany intervening argument bindings or other transformations.The method, constructor, or field referred to by a direct method handle is calleditsunderlying member.Direct method handles may be obtained in any of these ways:

Restrictions on Cracking

Given a suitableLookup object, it is possible to crack any direct method handleto recover a symbolic reference for the underlying method, constructor, or field.Cracking must be done via aLookup object equivalent to that which createdthe target method handle, or which has enough access permissions to recreatean equivalent method handle.

If the underlying method iscaller sensitive,the direct method handle will have been "bound" to a particular caller class, thelookup classof the lookup object used to create it.Cracking this method handle with a different lookup class will faileven if the underlying method is public (likeClass.forName).

The requirement of lookup object matching provides a "fast fail" behaviorfor programs which may otherwise trust erroneous revelation of a methodhandle with symbolic information (or caller binding) from an unexpected scope.UseMethodHandles.reflectAs(Class, MethodHandle) to override this limitation.

Reference kinds

TheLookup Factory Methodscorrespond to all major use cases for methods, constructors, and fields.These use cases may be distinguished using small integers as follows:
reference kinds
reference kinddescriptive namescopememberbehavior
1REF_getFieldclassFT f;(T) this.f;
2REF_getStaticclass orinterfacestatic
FT f;
(T) C.f;
3REF_putFieldclassFT f;this.f = x;
4REF_putStaticclassstatic
FT f;
C.f = arg;
5REF_invokeVirtualclassT m(A*);(T) this.m(arg*);
6REF_invokeStaticclass orinterfacestatic
T m(A*);
(T) C.m(arg*);
7REF_invokeSpecialclass orinterfaceT m(A*);(T) super.m(arg*);
8REF_newInvokeSpecialclassC(A*);new C(arg*);
9REF_invokeInterfaceinterfaceT m(A*);(T) this.m(arg*);
Since:
1.8
  • Field Details

  • Method Details

    • getReferenceKind

      int getReferenceKind()
      Returns the reference kind of the cracked method handle, which in turndetermines whether the method handle's underlying member was a constructor, method, or field.See thetable 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 bevoid.If it is to a non-static method, the method type will not mention thethis parameter.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 leadingthis parameter,or (in the case of a constructor) will replace thevoid return typewith the constructed class.The nominal type does not include anythis parameter,and (in the case of a constructor) will returnvoid.

      Returns:
      the type of the underlying member, expressed as a method type
    • reflectAs

      <T extendsMember> 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 bygetMethod,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, eitherMember or a subtype
      Parameters:
      expected - a class object representing the desired result typeT
      lookup - 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 type
      NullPointerException - if either argument isnull
      IllegalArgumentException - 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:
    • 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:
      true if 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 thetable 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 validreference kind number
    • toString

      static String toString(int kind,Class<?> defc,String name,MethodType type)
      Returns a string representation for aMethodHandleInfo,given the four parts of its symbolic reference.This is defined to be of the form"RK C.N:MT", whereRK is thereference kind string forkind,C is thename ofdefcN is thename, andMT is thetype.These four values may be obtained from thereference kind,declaring class,member name,andmethod typeof aMethodHandleInfo object.
      Implementation Requirements:
      This produces a result equivalent to:
          String.format("%s %s.%s:%s", referenceKindToString(kind), defc.getName(), name, type)
      Parameters:
      kind - thereference kind part of the symbolic reference
      defc - thedeclaring class part of the symbolic reference
      name - themember name part of the symbolic reference
      type - themethod 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 validreference kind number
      NullPointerException - if any reference argument isnull