Class SmartHandle


  • public class SmartHandle
    extends java.lang.Object
    A tuple of a Signature and a java.lang.invoke.MethodHandle, providing features of both plus a number of MethodHandles.* methods in a simpler form. SmartHandle is provided as a way to couple a given MethodHandle to a Signature, allowing future adaptation of the MethodHandle to proceed using Signature's various shortcuts and conveniences. Example: // A signature that only wants the "context" and "args" arguments public static final Signature ARG_COUNT_CHECK_FOLD = Signature .returning(void.class) .appendArg("args", Object[].class); // The actual target signature for arg count checking, with min and max ints public static final Signature ARG_COUNT_CHECK_SIGNATURE = Signature .returning(int.class) .appendArg("args", Object[].class) .appendArg("min", int.class) .appendArg("max", int.class); // A SmartHandle for the arity-checking method, using the fold and signature // from above and inserting 1, 3 for min, max SmartHandle arityCheck = SmartBinder .from(ARITY_CHECK_FOLD) .append("min", 1) .append("max", 3) .cast(ARITY_CHECK_SIGNATURE) .invokeStaticQuiet(LOOKUP, ArgCountChecker.class, "checkArgumentCount"); // The variable-arity call contaings other arguments plus the Object[] args. // Here, we can just fold with our arityCheck SmartHandle, which drops args // we are not interested in, passes along the args array, and ignores the // return value. variableCall = SmartBinder .from(VARIABLE_ARITY_SIGNATURE) .foldVoid(arityCheck) .invoke(directCall);
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.invoke.MethodHandle handle
      The MethodHandle associated with this smart handle
      private Signature signature
      The signature associated with this smart handle
    • Constructor Summary

      Constructors 
      Constructor Description
      SmartHandle​(Signature signature, java.lang.invoke.MethodHandle handle)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      SmartHandle apply​(int index, java.lang.Object arg)
      Apply an argument into the handle at the given index, returning a new SmartHandle.
      SmartHandle apply​(java.lang.String name, java.lang.Object arg)
      Apply an argument into the handle at the given name, returning a new SmartHandle.
      SmartHandle applyLast​(java.lang.Object arg)
      Apply an argument into the handle at the end, returning a new SmartHandle.
      SmartHandle bindTo​(java.lang.Object obj)
      Bind the first argument of this SmartHandle to the given object, returning a new adapted handle.
      SmartHandle cast​(Signature incoming)
      Create a new SmartHandle that casts arguments from the given signature to the current signature's type with the new argument names.
      SmartHandle cast​(java.lang.Class returnType, java.lang.Class... argTypes)
      Create a new SmartHandle that casts arguments from the given return type and argument types to the current signature's type, using the same argument names.
      SmartHandle cast​(java.lang.invoke.MethodType incoming)
      Create a new SmartHandle that casts arguments from the given type to the current signature's type, using the same argument names.
      SmartHandle convert​(Signature incoming)
      Create a new SmartHandle that converts arguments from the given signature to the current signature's type with the new argument names.
      SmartHandle convert​(java.lang.Class returnType, java.lang.Class... argTypes)
      Create a new SmartHandle that converts arguments from the given return type and argument types to the current signature's type, using the same argument names.
      SmartHandle convert​(java.lang.invoke.MethodType incoming)
      Create a new SmartHandle that converts arguments from the given type to the current signature's type, using the same argument names.
      SmartHandle drop​(int index, java.lang.String newName, java.lang.Class type)
      Insert an argument into the handle at the given index, returning a new SmartHandle.
      SmartHandle drop​(java.lang.String beforeName, java.lang.String newName, java.lang.Class type)
      Insert an argument into the handle at the given index, returning a new SmartHandle.
      SmartHandle dropLast​(java.lang.String newName, java.lang.Class type)
      Insert an argument into the handle at the given index, returning a new SmartHandle.
      static SmartHandle findStaticQuiet​(java.lang.invoke.MethodHandles.Lookup lookup, java.lang.Class target, java.lang.String name, Signature signature)
      Create a new SmartHandle by performing a lookup on the given target class for the given method name with the given signature.
      static SmartHandle from​(Signature signature, java.lang.invoke.MethodHandle handle)
      Create a new SmartHandle from the given Signature and MethodHandle.
      SmartHandle guard​(SmartHandle target, SmartHandle fallback)
      Use this SmartHandle as a test to guard target and fallback handles.
      java.lang.invoke.MethodHandle guard​(java.lang.invoke.MethodHandle target, java.lang.invoke.MethodHandle fallback)
      Use this SmartHandle as a test to guard target and fallback handles.
      java.lang.invoke.MethodHandle handle()
      Get the MethodHandle of this SmartHandle.
      SmartHandle returnValue​(java.lang.Class type, java.lang.Object value)
      Replace the return value with the given value, performing no other processing of the original value.
      Signature signature()
      Get the Signature of this SmartHandle.
      java.lang.String toString()
      A human-readable String representation of this SamrtHandle.
      • Methods inherited from class java.lang.Object

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

      • signature

        private final Signature signature
        The signature associated with this smart handle
      • handle

        private final java.lang.invoke.MethodHandle handle
        The MethodHandle associated with this smart handle
    • Constructor Detail

      • SmartHandle

        SmartHandle​(Signature signature,
                    java.lang.invoke.MethodHandle handle)
    • Method Detail

      • from

        public static SmartHandle from​(Signature signature,
                                       java.lang.invoke.MethodHandle handle)
        Create a new SmartHandle from the given Signature and MethodHandle.
        Parameters:
        signature - the signature for the new smart handle
        handle - the method handle for the new smart handle
        Returns:
        a new SmartHandle
      • findStaticQuiet

        public static SmartHandle findStaticQuiet​(java.lang.invoke.MethodHandles.Lookup lookup,
                                                  java.lang.Class target,
                                                  java.lang.String name,
                                                  Signature signature)
        Create a new SmartHandle by performing a lookup on the given target class for the given method name with the given signature.
        Parameters:
        lookup - the MethodHandles.Lookup object to use
        target - the class where the method is located
        name - the name of the method
        signature - the signature of the method
        Returns:
        a new SmartHandle based on the signature and looked-up MethodHandle
      • signature

        public Signature signature()
        Get the Signature of this SmartHandle.
        Returns:
        the Signature of this SmartHandle
      • handle

        public java.lang.invoke.MethodHandle handle()
        Get the MethodHandle of this SmartHandle.
        Returns:
        the MethodHandle of this SmartHandle
      • apply

        public SmartHandle apply​(int index,
                                 java.lang.Object arg)
        Apply an argument into the handle at the given index, returning a new SmartHandle. The new handle will use the given value for the argument at the given index, accepting one fewer argument as a result. In other words, fix that argument (partial application) into the given handle.
        Parameters:
        name - the name of the argument in the new SmartHandle's Signature
        arg - the argument value
        Returns:
        a new SmartHandle that already has applied the given argument
      • apply

        public SmartHandle apply​(java.lang.String name,
                                 java.lang.Object arg)
        Apply an argument into the handle at the given name, returning a new SmartHandle. The new handle will use the given value for the argument at the given index, accepting one fewer argument as a result. In other words, fix that argument (partial application) into the given handle.
        Parameters:
        name - the name of the argument in the new SmartHandle's Signature
        arg - the argument value
        Returns:
        a new SmartHandle that already has applied the given argument
      • applyLast

        public SmartHandle applyLast​(java.lang.Object arg)
        Apply an argument into the handle at the end, returning a new SmartHandle. The new handle will use the given value for the last argument, accepting one fewer argument as a result. In other words, fix that argument (partial application) into the given handle.
        Parameters:
        name - the name of the argument in the new SmartHandle's Signature
        arg - the argument value
        Returns:
        a new SmartHandle that already has applied the given argument
      • drop

        public SmartHandle drop​(java.lang.String beforeName,
                                java.lang.String newName,
                                java.lang.Class type)
        Insert an argument into the handle at the given index, returning a new SmartHandle.
        Parameters:
        name - the name of the argument in the new SmartHandle's Signature
        arg - the argument value
        Returns:
        a new SmartHandle with the additional argument
      • drop

        public SmartHandle drop​(int index,
                                java.lang.String newName,
                                java.lang.Class type)
        Insert an argument into the handle at the given index, returning a new SmartHandle.
        Parameters:
        name - the name of the argument in the new SmartHandle's Signature
        arg - the argument value
        Returns:
        a new SmartHandle with the additional argument
      • dropLast

        public SmartHandle dropLast​(java.lang.String newName,
                                    java.lang.Class type)
        Insert an argument into the handle at the given index, returning a new SmartHandle.
        Parameters:
        name - the name of the argument in the new SmartHandle's Signature
        arg - the argument value
        Returns:
        a new SmartHandle with the additional argument
      • guard

        public java.lang.invoke.MethodHandle guard​(java.lang.invoke.MethodHandle target,
                                                   java.lang.invoke.MethodHandle fallback)
        Use this SmartHandle as a test to guard target and fallback handles.
        Parameters:
        target - the "true" path for this handle's test
        fallback - the "false" path for this handle's test
        Returns:
        a MethodHandle that performs the test and branch
      • guard

        public SmartHandle guard​(SmartHandle target,
                                 SmartHandle fallback)
        Use this SmartHandle as a test to guard target and fallback handles.
        Parameters:
        target - the "true" path for this handle's test
        fallback - the "false" path for this handle's test
        Returns:
        a new SmartHandle that performs the test and branch
      • bindTo

        public SmartHandle bindTo​(java.lang.Object obj)
        Bind the first argument of this SmartHandle to the given object, returning a new adapted handle.
        Parameters:
        obj - the object to which to bind this handle's first argument
        Returns:
        a new SmartHandle with the first argument dropped in favor of obj
      • convert

        public SmartHandle convert​(java.lang.invoke.MethodType incoming)
        Create a new SmartHandle that converts arguments from the given type to the current signature's type, using the same argument names. This conversion is equivalent to MethodHandle#asType.
        Parameters:
        incoming - the target MethodType from which arguments will be converted
        Returns:
        a new SmartHandle that accepts the given argument types
      • convert

        public SmartHandle convert​(java.lang.Class returnType,
                                   java.lang.Class... argTypes)
        Create a new SmartHandle that converts arguments from the given return type and argument types to the current signature's type, using the same argument names. This conversion is equivalent to MethodHandle#asType.
        Parameters:
        returnType - the return type of the new handle
        argTypes - the argument types of the new handle
        Returns:
        a new SmartHandle that accepts the given argument types
      • convert

        public SmartHandle convert​(Signature incoming)
        Create a new SmartHandle that converts arguments from the given signature to the current signature's type with the new argument names. This conversion is equivalent to MethodHandle#asType.
        Parameters:
        incoming - the target MethodType from which arguments will be converted
        Returns:
        a new SmartHandle that accepts the given argument types
      • cast

        public SmartHandle cast​(java.lang.invoke.MethodType incoming)
        Create a new SmartHandle that casts arguments from the given type to the current signature's type, using the same argument names. This casting is equivalent to MethodHandles#explicitCastArguments.
        Parameters:
        incoming - the target MethodType from which arguments will be converted
        Returns:
        a new SmartHandle that accepts the given argument types
      • cast

        public SmartHandle cast​(Signature incoming)
        Create a new SmartHandle that casts arguments from the given signature to the current signature's type with the new argument names. This casting is equivalent to MethodHandle#asType.
        Parameters:
        incoming - the target MethodType from which arguments will be converted
        Returns:
        a new SmartHandle that accepts the given argument types
      • cast

        public SmartHandle cast​(java.lang.Class returnType,
                                java.lang.Class... argTypes)
        Create a new SmartHandle that casts arguments from the given return type and argument types to the current signature's type, using the same argument names. This casting is equivalent to MethodHandle#asType.
        Parameters:
        returnType - the return type of the new handle
        argTypes - the argument types of the new handle
        Returns:
        a new SmartHandle that accepts the given argument types
      • returnValue

        public SmartHandle returnValue​(java.lang.Class type,
                                       java.lang.Object value)
        Replace the return value with the given value, performing no other processing of the original value.
        Parameters:
        value - the type for the new return value
        value - the new value to return
      • toString

        public java.lang.String toString()
        A human-readable String representation of this SamrtHandle.
        Overrides:
        toString in class java.lang.Object
        Returns:
        a String representation of this handle