MethodInvokers¶
- public final class org.apache.commons.lang3.function.MethodInvokers[source]¶
Converts
Methodobjects to lambdas.More specifically, produces instances of single-method interfaces which redirect calls to methods; see
#asInterfaceInstance(Class, Method).Calling supplier methods with no arguments
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"));
Calling function methods with one argument
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));
- Since:
3.13.0
Methods¶
Produces a |
|
Produces a |
|
Produces a |
|
Produces a |
|
Produces a |
|
Produces a |
|
Produces a |
|
Produces an instance of the given single-method interface which redirects its calls to the given method. |
|
Produces a |
- @SuppressWarnings({"unchecked"}) public static <T, U> BiConsumer<T, U> asBiConsumer(Method method)[source]¶
Produces a
BiConsumerfor 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.- Type parameters:
<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.
- Parameters:
method – The method to invoke.
- Returns:
A correctly-typed wrapper for the given target.
- @SuppressWarnings({"unchecked"}) public static <T, U, R> BiFunction<T, U, R> asBiFunction(Method method)[source]¶
Produces a
BiFunctionfor 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));
- Type parameters:
<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.
- Parameters:
method – The method to invoke.
- Returns:
A correctly-typed wrapper for the given target.
- @SuppressWarnings({"unchecked"}) public static <T, U> FailableBiConsumer<T, U, Throwable> asFailableBiConsumer(Method method)[source]¶
Produces a
FailableBiConsumerfor 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.- Type parameters:
<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.
- Parameters:
method – The method to invoke.
- Returns:
A correctly-typed wrapper for the given target.
- @SuppressWarnings({"unchecked"}) public static <T, U, R> FailableBiFunction<T, U, R, Throwable> asFailableBiFunction(Method method)[source]¶
Produces a
FailableBiFunctionfor 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.- Type parameters:
<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.
- Parameters:
method – The method to invoke.
- Returns:
A correctly-typed wrapper for the given target.
- @SuppressWarnings({"unchecked"}) public static <T, R> FailableFunction<T, R, Throwable> asFailableFunction(Method method)[source]¶
Produces a
FailableFunctionfor 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.- Type parameters:
<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.
- Parameters:
method – The method to invoke.
- Returns:
A correctly-typed wrapper for the given target.
- @SuppressWarnings({"unchecked"}) public static <R> FailableSupplier<R, Throwable> asFailableSupplier(Method method)[source]¶
Produces a
FailableSupplierfor a given a supplier Method. The FailableSupplier return type must match the method’s return type.Only works with static methods.
- Type parameters:
<R> – The Method return type.
- Parameters:
method – The method to invoke.
- Returns:
A correctly-typed wrapper for the given target.
- @SuppressWarnings({"unchecked"}) public static <T, R> Function<T, R> asFunction(Method method)[source]¶
Produces a
Functionfor 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"));
- Type parameters:
<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.
- Parameters:
method – The method to invoke.
- Returns:
A correctly-typed wrapper for the given target.
- public static <T> T asInterfaceInstance(Class<T> interfaceClass, Method method)[source]¶
Produces an instance of the given single-method interface which redirects its calls to the given method.
For the definition of “single-method”, see
MethodHandleProxies#asInterfaceInstance(Class, MethodHandle).- Type parameters:
<T> – The interface type.
- Parameters:
interfaceClass – A class object representing
T.method – The method to invoke.
- Returns:
A correctly-typed wrapper for the given target.
- See also:
MethodHandleProxies#asInterfaceInstance(Class, MethodHandle)
- @SuppressWarnings({"unchecked"}) public static <R> Supplier<R> asSupplier(Method method)[source]¶
Produces a
Supplierfor a given a supplier Method. The Supplier return type must match the method’s return type.Only works with static methods.
- Type parameters:
<R> – The Method return type.
- Parameters:
method – The method to invoke.
- Returns:
A correctly-typed wrapper for the given target.