public final class MethodInvokers
extends java.lang.Object
Method
objects to lambdas.
More specifically, produces instances of single-method interfaces which redirect calls to methods; see
asInterfaceInstance(Class, Method)
.
If the interface's single-method defines no arguments, use asFunction(Method)
and then apply the function
passing in the object to receive the method call.
For example to invoke String.length()
:
final Method method = String.class.getMethod("length"); final Function<String, Integer> function = MethodInvokers.asFunction(method); assertEquals(3, function.apply("ABC"));
If the interface's single-method defines one argument, use asBiFunction(Method)
and then apply the function
passing in the object to receive the method call. The second argument to the function is the only argument to the
method.
For example to invoke String.charAt(int)
:
final Method method = String.class.getMethod("charAt", int.class); final BiFunction<String, Integer, Character> function = MethodInvokers.asBiFunction(method); assertEquals('C', function.apply("ABC", 2));
Modifier and Type | Method and Description |
---|---|
static <T,U> java.util.function.BiConsumer<T,U> |
asBiConsumer(java.lang.reflect.Method method)
Produces a
BiConsumer for a given consumer Method. |
static <T,U,R> java.util.function.BiFunction<T,U,R> |
asBiFunction(java.lang.reflect.Method method)
Produces a
BiFunction for a given a function Method. |
static <T,U> FailableBiConsumer<T,U,java.lang.Throwable> |
asFailableBiConsumer(java.lang.reflect.Method method)
Produces a
FailableBiConsumer for a given consumer Method. |
static <T,U,R> FailableBiFunction<T,U,R,java.lang.Throwable> |
asFailableBiFunction(java.lang.reflect.Method method)
Produces a
FailableBiFunction for a given a function Method. |
static <T,R> FailableFunction<T,R,java.lang.Throwable> |
asFailableFunction(java.lang.reflect.Method method)
Produces a
FailableFunction for a given a supplier Method. |
static <R> FailableSupplier<R,java.lang.Throwable> |
asFailableSupplier(java.lang.reflect.Method method)
Produces a
FailableSupplier for a given a supplier Method. |
static <T,R> java.util.function.Function<T,R> |
asFunction(java.lang.reflect.Method method)
Produces a
Function for a given a supplier Method. |
static <T> T |
asInterfaceInstance(java.lang.Class<T> interfaceClass,
java.lang.reflect.Method method)
Produces an instance of the given single-method interface which redirects its calls to the given method.
|
static <R> java.util.function.Supplier<R> |
asSupplier(java.lang.reflect.Method method)
Produces a
Supplier for a given a supplier Method. |
public static <T,U> java.util.function.BiConsumer<T,U> asBiConsumer(java.lang.reflect.Method method)
BiConsumer
for a given consumer Method. For example, a classic setter method (as opposed
to a fluent setter). You call the BiConsumer with two arguments: (1) the object receiving the method call, and (2)
the method argument.T
- the type of the first argument to the operation: The type containing the Method.U
- the type of the second argument to the operation: The type of the method argument.method
- the method to invoke.public static <T,U,R> java.util.function.BiFunction<T,U,R> asBiFunction(java.lang.reflect.Method method)
BiFunction
for a given a function Method. You call the BiFunction with two arguments: (1)
the object receiving the method call, and (2) the method argument. The BiFunction return type must match the method's
return type.
For example to invoke String.charAt(int)
:
final Method method = String.class.getMethod("charAt", int.class); final BiFunction<String, Integer, Character> function = MethodInvokers.asBiFunction(method); assertEquals('C', function.apply("ABC", 2));
T
- the type of the first argument to the function: The type containing the method.U
- the type of the second argument to the function: the method argument type.R
- the type of the result of the function: The method return type.method
- the method to invoke.public static <T,U> FailableBiConsumer<T,U,java.lang.Throwable> asFailableBiConsumer(java.lang.reflect.Method method)
FailableBiConsumer
for a given consumer Method. For example, a classic setter method (as
opposed to a fluent setter). You call the FailableBiConsumer with two arguments: (1) the object receiving the method
call, and (2) the method argument.T
- the type of the first argument to the operation: The type containing the Method.U
- the type of the second argument to the operation: The type of the method argument.method
- the method to invoke.public static <T,U,R> FailableBiFunction<T,U,R,java.lang.Throwable> asFailableBiFunction(java.lang.reflect.Method method)
FailableBiFunction
for a given a function Method. You call the FailableBiFunction with
two arguments: (1) the object receiving the method call, and (2) the method argument. The BiFunction return type must
match the method's return type.T
- the type of the first argument to the function: The type containing the method.U
- the type of the second argument to the function: the method argument type.R
- the type of the result of the function: The method return type.method
- the method to invoke.public static <T,R> FailableFunction<T,R,java.lang.Throwable> asFailableFunction(java.lang.reflect.Method method)
FailableFunction
for a given a supplier Method. You call the Function with one argument:
the object receiving the method call. The FailableFunction return type must match the method's return type.T
- the type of the first argument to the function: The type containing the method.R
- the type of the result of the function: The method return type.method
- the method to invoke.public static <R> FailableSupplier<R,java.lang.Throwable> asFailableSupplier(java.lang.reflect.Method method)
FailableSupplier
for a given a supplier Method. The FailableSupplier return type must
match the method's return type.
Only works with static methods.
R
- The Method return type.method
- the method to invoke.public static <T,R> java.util.function.Function<T,R> asFunction(java.lang.reflect.Method method)
Function
for a given a supplier Method. You call the Function with one argument: the
object receiving the method call. The Function return type must match the method's return type.
For example to invoke String.length()
:
final Method method = String.class.getMethod("length"); final Function<String, Integer> function = MethodInvokers.asFunction(method); assertEquals(3, function.apply("ABC"));
T
- the type of the first argument to the function: The type containing the method.R
- the type of the result of the function: The method return type.method
- the method to invoke.public static <T> T asInterfaceInstance(java.lang.Class<T> interfaceClass, java.lang.reflect.Method method)
For the definition of "single-method", see MethodHandleProxies.asInterfaceInstance(Class, MethodHandle).
T
- The interface type.interfaceClass
- a class object representing T
.method
- the method to invoke.MethodHandleProxies.asInterfaceInstance(Class, MethodHandle)
public static <R> java.util.function.Supplier<R> asSupplier(java.lang.reflect.Method method)
Supplier
for a given a supplier Method. The Supplier return type must match the method's
return type.
Only works with static methods.
R
- The Method return type.method
- the method to invoke.Copyright © 2010 - 2023 Adobe. All Rights Reserved