Class InvokerUtils


  • public class InvokerUtils
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      InvokerUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.invoke.MethodHandle bindTo​(java.lang.invoke.MethodHandle methodHandle, java.lang.Object... objs)
      Bind optional arguments to provided method handle
      static java.lang.invoke.MethodHandle mutatedInvoker​(java.lang.invoke.MethodHandles.Lookup lookup, java.lang.Class<?> targetClass, java.lang.reflect.Method method, InvokerUtils.Arg... callingArgs)
      Build a MethodHandle that can call the method with the calling args provided.
      static java.lang.invoke.MethodHandle mutatedInvoker​(java.lang.invoke.MethodHandles.Lookup lookup, java.lang.Class<?> targetClass, java.lang.reflect.Method method, InvokerUtils.ParamIdentifier paramIdentifier, java.lang.String[] namedVariables, InvokerUtils.Arg... callingArgs)
      Create a MethodHandle that performs the following layers MethodHandles.permuteArguments(MethodHandle, MethodType, int...) - moving calling Args around to fit actual actual method parameter arguments (in proper order), with remaining (unused) calling args afterwords MethodHandles.dropArguments(MethodHandle, int, Class[]) - to drop the unused calling args MethodHandle.invoke(Object...) - to call the specific method
      static java.lang.invoke.MethodHandle optionalMutatedInvoker​(java.lang.invoke.MethodHandles.Lookup lookup, java.lang.Class<?> targetClass, java.lang.reflect.Method method, InvokerUtils.Arg... callingArgs)  
      static java.lang.invoke.MethodHandle optionalMutatedInvoker​(java.lang.invoke.MethodHandles.Lookup lookup, java.lang.Class<?> targetClass, java.lang.reflect.Method method, InvokerUtils.ParamIdentifier paramIdentifier, java.lang.String[] namedVariables, InvokerUtils.Arg... callingArgs)
      Create an optional MethodHandle that performs the following layers.
      • Methods inherited from class java.lang.Object

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

      • InvokerUtils

        public InvokerUtils()
    • Method Detail

      • bindTo

        public static java.lang.invoke.MethodHandle bindTo​(java.lang.invoke.MethodHandle methodHandle,
                                                           java.lang.Object... objs)
        Bind optional arguments to provided method handle
        Parameters:
        methodHandle - the method handle to bind to
        objs - the list of optional objects to bind to.
        Returns:
        the bound MethodHandle, or null if the provided methodHandle was null.
      • mutatedInvoker

        public static java.lang.invoke.MethodHandle mutatedInvoker​(java.lang.invoke.MethodHandles.Lookup lookup,
                                                                   java.lang.Class<?> targetClass,
                                                                   java.lang.reflect.Method method,
                                                                   InvokerUtils.Arg... callingArgs)
        Build a MethodHandle that can call the method with the calling args provided.

        Might need to drop calling args and/or reorder the calling args to fit the actual method being called.

        Parameters:
        lookup - the MethodHandles.Lookup instance to use.
        targetClass - the target class for invocations of the resulting MethodHandle (also known as parameter 0)
        method - the method to invoke
        callingArgs - the calling arguments. This is the array of arguments that will always be passed into the returned MethodHandle. They will be present in the MethodHandle.type() in the order specified in this array.
      • mutatedInvoker

        public static java.lang.invoke.MethodHandle mutatedInvoker​(java.lang.invoke.MethodHandles.Lookup lookup,
                                                                   java.lang.Class<?> targetClass,
                                                                   java.lang.reflect.Method method,
                                                                   InvokerUtils.ParamIdentifier paramIdentifier,
                                                                   java.lang.String[] namedVariables,
                                                                   InvokerUtils.Arg... callingArgs)
        Create a MethodHandle that performs the following layers
        1. MethodHandles.permuteArguments(MethodHandle, MethodType, int...) - moving calling Args around to fit actual actual method parameter arguments (in proper order), with remaining (unused) calling args afterwords
        2. MethodHandles.dropArguments(MethodHandle, int, Class[]) - to drop the unused calling args
        3. MethodHandle.invoke(Object...) - to call the specific method

        The returned MethodHandle.MethodHandle.type() assumes the following:

        1. Return type will be what the provided Method has.
        2. The first parameter is the type of class provided in the targetClass.
        3. The next parameters are all of the found type's of the named arguments, or type String if not found in provided Method.
        4. The next parameters are all of the provided callingArg types
        Parameters:
        lookup - the MethodHandles.Lookup instance to use.
        targetClass - the target class for invocations of the resulting MethodHandle (also known as parameter 0)
        method - the method to invoke
        paramIdentifier - the mechanism to identify parameters in method
        namedVariables - the array of named variables. This is the array of named arguments that the target method might have. The resulting MethodHandle will include all of these namedVariables as the first non-object arguments in the MethodType found on the returned MethodHandle.type()
        callingArgs - the calling arguments. This is the array of arguments that will always be passed into the returned MethodHandle. They will be present in the MethodHandle.type() in the order specified in this array.
        Returns:
        the MethodHandle for this set of CallingArgs
        Throws:
        java.lang.RuntimeException - when unable to fit Calling Args to Parameter Types
      • optionalMutatedInvoker

        public static java.lang.invoke.MethodHandle optionalMutatedInvoker​(java.lang.invoke.MethodHandles.Lookup lookup,
                                                                           java.lang.Class<?> targetClass,
                                                                           java.lang.reflect.Method method,
                                                                           InvokerUtils.ParamIdentifier paramIdentifier,
                                                                           java.lang.String[] namedVariables,
                                                                           InvokerUtils.Arg... callingArgs)
        Create an optional MethodHandle that performs the following layers.
        1. MethodHandles.permuteArguments(MethodHandle, MethodType, int...) - moving calling Args around to fit actual actual method parameter arguments (in proper order), with remaining (unused) calling args afterwords
        2. MethodHandles.dropArguments(MethodHandle, int, Class[]) - to drop the unused calling args
        3. MethodHandle.invoke(Object...) - to call the specific method
        Parameters:
        lookup - the MethodHandles.Lookup instance to use.
        targetClass - the target class for invocations of the resulting MethodHandle (also known as parameter 0)
        method - the method to invoke
        paramIdentifier - the mechanism to identify parameters in method
        namedVariables - the array of named variables. This is the array of named arguments that the target method might have. The resulting MethodHandle will include all of these namedVariables as the first non-object arguments in the MethodType found on the returned MethodHandle.type()
        callingArgs - the calling arguments. This is the array of arguments that will always be passed into the returned MethodHandle. They will be present in the MethodHandle.type() in the order specified in this array.
        Returns:
        the MethodHandle for this set of CallingArgs, or null if not possible to create MethodHandle with CallingArgs to provided method
      • optionalMutatedInvoker

        public static java.lang.invoke.MethodHandle optionalMutatedInvoker​(java.lang.invoke.MethodHandles.Lookup lookup,
                                                                           java.lang.Class<?> targetClass,
                                                                           java.lang.reflect.Method method,
                                                                           InvokerUtils.Arg... callingArgs)