Class MethodHandleUtils

  • All Implemented Interfaces:
    Utils

    public abstract class MethodHandleUtils
    extends java.lang.Object
    implements Utils
    Utility class for working with MethodHandle.

    This class provides helper methods to simplify the usage of the MethodHandles API, including creating lookups, finding methods, and handling invocation failures.

    Example Usage

    
     // Example 1: Find a virtual method handle
     MethodHandle mh = MethodHandleUtils.findVirtual(String.class, "length", int.class);
     int length = (int) mh.invokeExact("Hello");
     System.out.println(length); // Output: 5
    
     // Example 2: Find a static method handle
     MethodHandle mh = MethodHandleUtils.findStatic(Math.class, "abs", double.class);
     double abs = (double) mh.invokeExact(-42.0);
     System.out.println(abs); // Output: 42.0
    
     // Example 3: Handle invokeExact failure
     try {
         MethodHandle mh = MethodHandleUtils.findVirtual(Object.class, "nonExistentMethod", void.class);
         mh.invokeExact();
     } catch (Throwable e) {
         MethodHandleUtils.handleInvokeExactFailure(e, mh);
     }
     
    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    MethodHandles, MethodHandle
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  MethodHandleUtils.LookupMode
      The allowed MethodHandles.Lookup modes enumeration
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int ALL_MODES
      A single-bit mask representing all accesses (public, private, protected and package) The value, 0x0f, happens to be the same as the value of the modifier bit.
      static int MODULE
      A single-bit mask representing module access, which may contribute to the result of lookupModes.
      static int ORIGINAL
      A single-bit mask representing original access which may contribute to the result of lookupModes.
      static java.lang.invoke.MethodHandles.Lookup PUBLIC_LOOKUP
      The MethodHandles.Lookup for MethodHandles.publicLookup()
      static int UNCONDITIONAL
      A single-bit mask representing unconditional access which may contribute to the result of lookupModes.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      protected static java.lang.invoke.MethodHandle find​(java.lang.Class<?> requestedClass, java.lang.String methodName, java.lang.Class[] parameterTypes, ThrowableBiFunction<java.lang.invoke.MethodHandles.Lookup,​java.lang.invoke.MethodType,​java.lang.invoke.MethodHandle> function)  
      static java.lang.invoke.MethodHandle findStatic​(java.lang.Class<?> requestedClass, java.lang.String methodName, java.lang.Class... parameterTypes)
      The convenient method to find MethodHandles.Lookup.findStatic(Class, String, MethodType)
      static java.lang.invoke.MethodHandle findVirtual​(java.lang.Class<?> requestedClass, java.lang.String methodName, java.lang.Class... parameterTypes)
      The convenient method to find MethodHandles.Lookup.findVirtual(Class, String, MethodType)
      static void handleInvokeExactFailure​(java.lang.Throwable e, java.lang.invoke.MethodHandle methodHandle, java.lang.Object... args)
      handle the failure of MethodHandle.invokeExact(Object...)
      static java.lang.invoke.MethodHandles.Lookup lookup​(java.lang.Class<?> requestedClass)
      Create an instance of MethodHandles.Lookup by the specified lookup class with all accesses (public, private, protected and package)
      static java.lang.invoke.MethodHandles.Lookup lookup​(java.lang.Class<?> requestedClass, MethodHandleUtils.LookupMode... lookupModes)
      Create an instance of MethodHandles.Lookup by the specified lookup class with all access (public, private, protected and package)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • MODULE

        public static final int MODULE
        A single-bit mask representing module access, which may contribute to the result of lookupModes. The value is 0x10, which does not correspond meaningfully to any particular modifier bit. In conjunction with the PUBLIC modifier bit, a Lookup with this lookup mode can access all public types in the module of the lookup class and public types in packages exported by other modules to the module of the class to be looked up.

        If this lookup mode is set, the previous lookup class is always null.

        Since:
        9
        See Also:
        MethodHandles.Lookup#MODULE, Constant Field Values
      • UNCONDITIONAL

        public static final int UNCONDITIONAL
        A single-bit mask representing unconditional access which may contribute to the result of lookupModes. The value is 0x20, which does not correspond meaningfully to any particular modifier bit. A Lookup with this lookup mode assumes readability. This lookup mode can access all public members of public types of all modules when the type is in a package that is exported unconditionally.

        If this lookup mode is set, the previous lookup class is always null.

        Since:
        9
        See Also:
        MethodHandles.publicLookup(), MethodHandles.Lookup#UNCONDITIONAL, Constant Field Values
      • ORIGINAL

        public static final int ORIGINAL
        A single-bit mask representing original access which may contribute to the result of lookupModes. The value is 0x40, which does not correspond meaningfully to any particular modifier bit.

        If this lookup mode is set, the Lookup object must be created by the original lookup class by calling MethodHandles.lookup() method or by a bootstrap method invoked by the VM. The Lookup object with this lookup mode has full privilege access.

        Since:
        16
        See Also:
        MethodHandles.Lookup#ORIGINAL, Constant Field Values
      • ALL_MODES

        public static final int ALL_MODES
        A single-bit mask representing all accesses (public, private, protected and package) The value, 0x0f, happens to be the same as the value of the modifier bit.
        See Also:
        Constant Field Values
      • PUBLIC_LOOKUP

        public static final java.lang.invoke.MethodHandles.Lookup PUBLIC_LOOKUP
        The MethodHandles.Lookup for MethodHandles.publicLookup()
    • Method Detail

      • lookup

        public static java.lang.invoke.MethodHandles.Lookup lookup​(java.lang.Class<?> requestedClass)
        Create an instance of MethodHandles.Lookup by the specified lookup class with all accesses (public, private, protected and package)
        Parameters:
        requestedClass - the class to be looked up
        Returns:
        non-null
      • findVirtual

        public static java.lang.invoke.MethodHandle findVirtual​(java.lang.Class<?> requestedClass,
                                                                java.lang.String methodName,
                                                                java.lang.Class... parameterTypes)
        The convenient method to find MethodHandles.Lookup.findVirtual(Class, String, MethodType)
        Parameters:
        requestedClass - the class to be looked up
        methodName - the target method name
        parameterTypes - the types of target method parameters
        Returns:
        MethodHandle
      • findStatic

        public static java.lang.invoke.MethodHandle findStatic​(java.lang.Class<?> requestedClass,
                                                               java.lang.String methodName,
                                                               java.lang.Class... parameterTypes)
        The convenient method to find MethodHandles.Lookup.findStatic(Class, String, MethodType)
        Parameters:
        requestedClass - the class to be looked up
        methodName - the target method name
        parameterTypes - the types of target method parameters
        Returns:
        MethodHandle
      • handleInvokeExactFailure

        public static void handleInvokeExactFailure​(java.lang.Throwable e,
                                                    java.lang.invoke.MethodHandle methodHandle,
                                                    java.lang.Object... args)
        handle the failure of MethodHandle.invokeExact(Object...)
        Parameters:
        e - Throwable
        methodHandle - MethodHandle
        args - the arguments of MethodHandle.invokeExact(Object...)
      • find

        protected static java.lang.invoke.MethodHandle find​(java.lang.Class<?> requestedClass,
                                                            java.lang.String methodName,
                                                            java.lang.Class[] parameterTypes,
                                                            ThrowableBiFunction<java.lang.invoke.MethodHandles.Lookup,​java.lang.invoke.MethodType,​java.lang.invoke.MethodHandle> function)