Class InvokeUtil

java.lang.Object
ru.progrm_jarvis.javacommons.invoke.InvokeUtil

public final class InvokeUtil extends Object
Utility for common scenarios related to Java Invoke API.

Note that OpenJDK's default LambdaMetafactory is not capable of implementing functional interfaces for fields' method-handle getters / setters

  • Field Details

    • VOID__METHOD_TYPE

      @NotNull public static final @NotNull MethodType VOID__METHOD_TYPE
      Method type of signature: void()
    • OBJECT__METHOD_TYPE

      @NotNull public static final @NotNull MethodType OBJECT__METHOD_TYPE
      Method type of signature: Object()
    • RUNNABLE__METHOD_TYPE

      @NotNull public static final @NotNull MethodType RUNNABLE__METHOD_TYPE
      Method type of signature: Runnable()
    • SUPPLIER__METHOD_TYPE

      @NotNull public static final @NotNull MethodType SUPPLIER__METHOD_TYPE
      Method type of signature: Supplier()
    • RUNNABLE_OBJECT__METHOD_TYPE

      @NotNull public static final @NotNull MethodType RUNNABLE_OBJECT__METHOD_TYPE
      Method type of signature: Runnable(Object)
    • SUPPLIER_OBJECT__METHOD_TYPE

      @NotNull public static final @NotNull MethodType SUPPLIER_OBJECT__METHOD_TYPE
      Method type of signature: Supplier(Object)
  • Method Details

    • getDelegatingLookupFactory

      @NotNull public static @NotNull LookupFactory getDelegatingLookupFactory()
      Gets the proxy lookup factory which delegated its calls to lookup(Class)
      Returns:
      proxy lookup factory
    • lookup

      @NotNull public static @NotNull MethodHandles.Lookup lookup(@NonNull @NonNull Class<?> clazz)
      Creates a cache MethodHandles.Lookup for the given class.
      Parameters:
      clazz - class for which to create a lookup
      Returns:
      created cached lookup for the given class
    • invokeFactory

      @NotNull public static <F, T> @NotNull InvokeFactory<F,T> invokeFactory()
      Creates new invoke factory using DELEGATING_LOOKUP_FACTORY.
      Type Parameters:
      F - type of functional interface implemented
      T - type of target value
      Returns:
      created invoke factory
    • toMethodHandle

      @NotNull public static @NotNull MethodHandle toMethodHandle(@NonNull @NonNull Method method)
      Converts the given method to a MethodHandle.
      Parameters:
      method - method to convert to MethodHandle
      Returns:
      MethodHandle created from the given method
    • toMethodHandle

      @NotNull public static @NotNull MethodHandle toMethodHandle(@NonNull @NonNull Constructor<?> constructor)
      Converts the given constructor to a MethodHandle.
      Parameters:
      constructor - constructor to convert to MethodHandle
      Returns:
      MethodHandle created from the given constructor
    • toGetterMethodHandle

      @NotNull public static @NotNull MethodHandle toGetterMethodHandle(@NonNull @NonNull Field field)
      Converts the given field to a getter-MethodHandle.
      Parameters:
      field - field to convert to getter-MethodHandle
      Returns:
      getter-MethodHandle created from the given field
    • toSetterMethodHandle

      @NotNull public static @NotNull MethodHandle toSetterMethodHandle(@NonNull @NonNull Field field)
      Converts the given field to a setter-MethodHandle.
      Parameters:
      field - field to convert to setter-MethodHandle
      Returns:
      setter-MethodHandle created from the given field
    • toStaticRunnable

      @NotNull public static @NotNull Runnable toStaticRunnable(@NonNull @NonNull Method method)
      Creates a Runnable to invoke the given static method.
      Parameters:
      method - static method from which to create a Runnable
      Returns:
      runnable invoking the given method
      Throws:
      IllegalArgumentException - if the given method requires parameters
      IllegalArgumentException - if the given method is not static
    • toBoundRunnable

      @NotNull public static @NotNull Runnable toBoundRunnable(@NonNull @NonNull Method method, @NonNull @NonNull Object target)
      Creates a Runnable to invoke the given non-static method on the given target.
      Parameters:
      method - non-static method from which to create a Runnable
      target - object on which the method should be invoked
      Returns:
      runnable invoking the given method
      Throws:
      IllegalArgumentException - if the given method requires parameters
      IllegalArgumentException - if the given method is static
    • toStaticSupplier

      @NotNull public static <R> @NotNull Supplier<R> toStaticSupplier(@NonNull @NonNull Method method)
      Creates a Supplier to invoke the given static method getting its returned value.
      Type Parameters:
      R - return type of the method
      Parameters:
      method - static method from which to create a Supplier
      Returns:
      runnable invoking the given method
      Throws:
      IllegalArgumentException - if the given method requires parameters
      IllegalArgumentException - if the given method is not static
    • toBoundSupplier

      @NotNull public static <R> @NotNull Supplier<R> toBoundSupplier(@NonNull @NonNull Method method, @NonNull @NonNull Object target)
      Creates a Supplier to invoke the given non-static method on the given target getting its returned value.
      Type Parameters:
      R - return type of the method
      Parameters:
      method - non-static method from which to create a Supplier
      target - object on which the method should be invoked
      Returns:
      supplier invoking the given method
      Throws:
      IllegalArgumentException - if the given method requires parameters
      IllegalArgumentException - if the given method is static
    • toSupplier

      @NotNull public static <T> @NotNull Supplier<T> toSupplier(@NonNull @NonNull Constructor<T> constructor)
      Creates a Supplier to invoke the given constructor.
      Type Parameters:
      T - type of object instantiated by the constructor
      Parameters:
      constructor - constructor from which to create a Supplier
      Returns:
      supplier invoking the given constructor
      Throws:
      IllegalArgumentException - if the given constructor requires parameters
    • toStaticGetterSupplier

      @NotNull public static <V> @NotNull Supplier<V> toStaticGetterSupplier(@NonNull @NonNull Field field)
      Creates a Supplier to get the value of the given static field.
      Type Parameters:
      V - type of field value
      Parameters:
      field - static field from which to create a Supplier
      Returns:
      supplier getting the value of the field
      Throws:
      IllegalArgumentException - if the given field is not static
    • toBoundGetterSupplier

      @NotNull public static <V> @NotNull Supplier<V> toBoundGetterSupplier(@NonNull @NonNull Field field, @NonNull @NonNull Object target)
      Creates a Supplier to get the value of the given non-static field of the given target.
      Type Parameters:
      V - type of field value
      Parameters:
      field - static field from which to create a Supplier
      target - object whose field value should be got
      Returns:
      supplier getting the value of the field
      Throws:
      IllegalArgumentException - if the given field is static
    • toGetterFunction

      @NotNull public static <T, V> @NotNull Function<T,V> toGetterFunction(@NonNull @NonNull Field field)
      Creates a Function to get the value of the given non-static field.
      Type Parameters:
      T - type of target class
      V - type of field value
      Parameters:
      field - static field from which to create a Function
      Returns:
      function getting the value of the field
      Throws:
      IllegalArgumentException - if the given field is static
    • toStaticSetterConsumer

      @NotNull public static <V> @NotNull Consumer<V> toStaticSetterConsumer(@NonNull @NonNull Field field)
      Creates a Consumer to set the value of the given static field.
      Type Parameters:
      V - type of field value
      Parameters:
      field - static field from which to create a Consumer
      Returns:
      consumer setting the value of the field
      Throws:
      IllegalArgumentException - if the given field is not static
    • toBoundSetterConsumer

      @NotNull public static <V> @NotNull Consumer<V> toBoundSetterConsumer(@NonNull @NonNull Field field, @NonNull @NonNull Object target)
      Creates a Consumer to set the value of the given non-static field of the given target.
      Type Parameters:
      V - type of field value
      Parameters:
      field - static field from which to create a Consumer
      target - object whose field value should be set
      Returns:
      consumer setting the value of the field
      Throws:
      IllegalArgumentException - if the given field is static
    • toSetterBiConsumer

      @NotNull public static <T, V> @NotNull BiConsumer<T,V> toSetterBiConsumer(@NonNull @NonNull Field field)
      Creates a BiConsumer to set the value of the given non-static field.
      Type Parameters:
      T - type of target class
      V - type of field value
      Parameters:
      field - static field from which to create a BiConsumer
      Returns:
      bi-consumer setting the value of the field
      Throws:
      IllegalArgumentException - if the given field is static