Class InvokeUtil
java.lang.Object
ru.progrm_jarvis.javacommons.invoke.InvokeUtil
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final @NotNull MethodTypeMethod type of signature:Object()static final @NotNull MethodTypeMethod type of signature:Runnable()static final @NotNull MethodTypestatic final @NotNull MethodTypeMethod type of signature:Supplier()static final @NotNull MethodTypestatic final @NotNull MethodTypeMethod type of signature:void() -
Method Summary
Modifier and TypeMethodDescriptionstatic @NotNull LookupFactoryGets the proxy lookup factory which delegated its calls tolookup(Class)static <F,T> @NotNull InvokeFactory<F, T> Creates newinvoke factoryusingDELEGATING_LOOKUP_FACTORY.static @NotNull MethodHandles.LookupCreates a cacheMethodHandles.Lookupfor the given class.static <V> @NotNull Supplier<V>toBoundGetterSupplier(@NonNull Field field, @NonNull Object target) Creates aSupplierto get the value of the given non-static field of the given target.static @NotNull RunnabletoBoundRunnable(@NonNull Method method, @NonNull Object target) Creates aRunnableto invoke the given non-static method on the given target.static <V> @NotNull Consumer<V>toBoundSetterConsumer(@NonNull Field field, @NonNull Object target) Creates aConsumerto set the value of the given non-static field of the given target.static <R> @NotNull Supplier<R>toBoundSupplier(@NonNull Method method, @NonNull Object target) Creates aSupplierto invoke the given non-static method on the given target getting its returned value.static <T,V> @NotNull Function<T, V> toGetterFunction(@NonNull Field field) Creates aFunctionto get the value of the given non-static field.static @NotNull MethodHandletoGetterMethodHandle(@NonNull Field field) Converts the given field to a getter-MethodHandle.static @NotNull MethodHandletoMethodHandle(@NonNull Constructor<?> constructor) Converts the given constructor to aMethodHandle.static @NotNull MethodHandletoMethodHandle(@NonNull Method method) Converts the given method to aMethodHandle.static <T,V> @NotNull BiConsumer<T, V> toSetterBiConsumer(@NonNull Field field) Creates aBiConsumerto set the value of the given non-static field.static @NotNull MethodHandletoSetterMethodHandle(@NonNull Field field) Converts the given field to a setter-MethodHandle.static <V> @NotNull Supplier<V>toStaticGetterSupplier(@NonNull Field field) Creates aSupplierto get the value of the given static field.static @NotNull RunnabletoStaticRunnable(@NonNull Method method) Creates aRunnableto invoke the given static method.static <V> @NotNull Consumer<V>toStaticSetterConsumer(@NonNull Field field) Creates aConsumerto set the value of the given static field.static <R> @NotNull Supplier<R>toStaticSupplier(@NonNull Method method) Creates aSupplierto invoke the given static method getting its returned value.static <T> @NotNull Supplier<T>toSupplier(@NonNull Constructor<T> constructor) Creates aSupplierto invoke the given constructor.
-
Field Details
-
Method Details
-
getDelegatingLookupFactory
Gets the proxy lookup factory which delegated its calls tolookup(Class)- Returns:
- proxy lookup factory
-
lookup
Creates a cacheMethodHandles.Lookupfor the given class.- Parameters:
clazz- class for which to create a lookup- Returns:
- created cached lookup for the given class
-
invokeFactory
Creates newinvoke factoryusingDELEGATING_LOOKUP_FACTORY.- Type Parameters:
F- type of functional interface implementedT- type of target value- Returns:
- created invoke factory
-
toMethodHandle
Converts the given method to aMethodHandle.- Parameters:
method- method to convert toMethodHandle- Returns:
MethodHandlecreated from the given method
-
toMethodHandle
@NotNull public static @NotNull MethodHandle toMethodHandle(@NonNull @NonNull Constructor<?> constructor) Converts the given constructor to aMethodHandle.- Parameters:
constructor- constructor to convert toMethodHandle- Returns:
MethodHandlecreated from the given constructor
-
toGetterMethodHandle
Converts the given field to a getter-MethodHandle.- Parameters:
field- field to convert to getter-MethodHandle- Returns:
- getter-
MethodHandlecreated from the given field
-
toSetterMethodHandle
Converts the given field to a setter-MethodHandle.- Parameters:
field- field to convert to setter-MethodHandle- Returns:
- setter-
MethodHandlecreated from the given field
-
toStaticRunnable
Creates aRunnableto invoke the given static method.- Parameters:
method- static method from which to create aRunnable- Returns:
- runnable invoking the given method
- Throws:
IllegalArgumentException- if the given method requires parametersIllegalArgumentException- if the given method is not static
-
toBoundRunnable
@NotNull public static @NotNull Runnable toBoundRunnable(@NonNull @NonNull Method method, @NonNull @NonNull Object target) Creates aRunnableto invoke the given non-static method on the given target.- Parameters:
method- non-static method from which to create aRunnabletarget- object on which the method should be invoked- Returns:
- runnable invoking the given method
- Throws:
IllegalArgumentException- if the given method requires parametersIllegalArgumentException- if the given method is static
-
toStaticSupplier
Creates aSupplierto 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 aSupplier- Returns:
- runnable invoking the given method
- Throws:
IllegalArgumentException- if the given method requires parametersIllegalArgumentException- 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 aSupplierto 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 aSuppliertarget- object on which the method should be invoked- Returns:
- supplier invoking the given method
- Throws:
IllegalArgumentException- if the given method requires parametersIllegalArgumentException- if the given method is static
-
toSupplier
@NotNull public static <T> @NotNull Supplier<T> toSupplier(@NonNull @NonNull Constructor<T> constructor) Creates aSupplierto invoke the given constructor.- Type Parameters:
T- type of object instantiated by the constructor- Parameters:
constructor- constructor from which to create aSupplier- 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 aSupplierto get the value of the given static field.- Type Parameters:
V- type of field value- Parameters:
field- static field from which to create aSupplier- 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 aSupplierto 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 aSuppliertarget- 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 aFunctionto get the value of the given non-static field.- Type Parameters:
T- type of target classV- type of field value- Parameters:
field- static field from which to create aFunction- 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 aConsumerto set the value of the given static field.- Type Parameters:
V- type of field value- Parameters:
field- static field from which to create aConsumer- 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 aConsumerto 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 aConsumertarget- 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 aBiConsumerto set the value of the given non-static field.- Type Parameters:
T- type of target classV- type of field value- Parameters:
field- static field from which to create aBiConsumer- Returns:
- bi-consumer setting the value of the field
- Throws:
IllegalArgumentException- if the given field is static
-