Class MethodHandlesLookupUtils

  • All Implemented Interfaces:
    Utils

    public abstract class MethodHandlesLookupUtils
    extends java.lang.Object
    implements Utils
    Utilities class providing convenient methods for working with MethodHandles.Lookup.

    This class offers various static methods to simplify the process of obtaining and using method handles, particularly for public virtual and static methods. It serves as a central utility to reduce boilerplate code when dealing with reflection and method handle lookup.

    Example Usage

    • Finding a public virtual method:
      
       MethodHandle mh = MethodHandlesLookupUtils.findPublicVirtual(String.class, "toString");
       
    • Finding a public static method:
      
       MethodHandle mh = MethodHandlesLookupUtils.findPublicStatic(Math.class, "abs", int.class);
       
    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    MethodHandles, MethodHandle
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.invoke.MethodHandle NOT_FOUND_METHOD_HANDLE
      MethodHandle for Not-Found
      static java.lang.invoke.MethodHandles.Lookup PUBLIC_LOOKUP
      The MethodHandles.Lookup for MethodHandles.publicLookup()
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      protected static java.lang.invoke.MethodHandle find​(java.lang.invoke.MethodHandles.Lookup lookup, 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)  
      protected static java.lang.invoke.MethodHandle find​(java.lang.invoke.MethodHandles.Lookup lookup, java.lang.reflect.Method method, ThrowableBiFunction<java.lang.invoke.MethodHandles.Lookup,​java.lang.invoke.MethodType,​java.lang.invoke.MethodHandle> function)  
      protected static java.lang.invoke.MethodHandle findPublic​(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)  
      protected static java.lang.invoke.MethodHandle findPublic​(java.lang.reflect.Method method, ThrowableBiFunction<java.lang.invoke.MethodHandles.Lookup,​java.lang.invoke.MethodType,​java.lang.invoke.MethodHandle> function)  
      static java.lang.invoke.MethodHandle findPublicStatic​(java.lang.Class<?> requestedClass, java.lang.String methodName, java.lang.Class... parameterTypes)
      The convenient method to find MethodHandles.Lookup.findStatic(Class, String, MethodType) for public static method
      static java.lang.invoke.MethodHandle findPublicVirtual​(java.lang.Class<?> requestedClass, java.lang.String methodName, java.lang.Class... parameterTypes)
      The convenient method to find MethodHandles.Lookup.findVirtual(Class, String, MethodType) for public method
      • Methods inherited from class java.lang.Object

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

      • NOT_FOUND_METHOD_HANDLE

        public static final java.lang.invoke.MethodHandle NOT_FOUND_METHOD_HANDLE
        MethodHandle for Not-Found
      • PUBLIC_LOOKUP

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

      • findPublicVirtual

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

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

        protected static java.lang.invoke.MethodHandle findPublic​(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)
      • find

        protected static java.lang.invoke.MethodHandle find​(java.lang.invoke.MethodHandles.Lookup lookup,
                                                            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)
      • findPublic

        protected static java.lang.invoke.MethodHandle findPublic​(java.lang.reflect.Method method,
                                                                  ThrowableBiFunction<java.lang.invoke.MethodHandles.Lookup,​java.lang.invoke.MethodType,​java.lang.invoke.MethodHandle> function)
      • find

        protected static java.lang.invoke.MethodHandle find​(java.lang.invoke.MethodHandles.Lookup lookup,
                                                            java.lang.reflect.Method method,
                                                            ThrowableBiFunction<java.lang.invoke.MethodHandles.Lookup,​java.lang.invoke.MethodType,​java.lang.invoke.MethodHandle> function)