Top Description Constructors Methods
org.apache.commons.lang3.function

public final Class MethodInvokers

extends Object
Class Inheritance
Imports
java.lang.invoke.MethodHandle, .MethodHandleProxies, .MethodHandles, java.lang.reflect.Method, java.util.Objects, java.util.function.BiConsumer, .BiFunction, .Function, .Supplier, org.apache.commons.lang3.exception.UncheckedIllegalAccessException

Converts Method objects 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

Constructor Summary

AccessConstructor and Description
private
MethodInvokers()

No need to create instances.

Method Summary

Modifier and TypeMethod and Description
public static <
the type of the first argument to the operation: The type containing the Method.
T
,
the type of the second argument to the operation: The type of the method argument.
U
>
BiConsumer<T, U>

Returns:

a correctly-typed wrapper for the given target.
asBiConsumer
(final Method
the method to invoke.
method
)

Produces a BiConsumer for a given consumer Method.

public static <
the type of the first argument to the function: The type containing the method.
T
,
the type of the second argument to the function: the method argument type.
U
,
the type of the result of the function: The method return type.
R
>
BiFunction<T, U, R>

Returns:

a correctly-typed wrapper for the given target.
asBiFunction
(final Method
the method to invoke.
method
)

Produces a BiFunction for a given a function Method.

public static <
the type of the first argument to the operation: The type containing the Method.
T
,
the type of the second argument to the operation: The type of the method argument.
U
>
FailableBiConsumer<T, U, Throwable>

Returns:

a correctly-typed wrapper for the given target.
asFailableBiConsumer
(final Method
the method to invoke.
method
)

Produces a FailableBiConsumer for a given consumer Method.

public static <
the type of the first argument to the function: The type containing the method.
T
,
the type of the second argument to the function: the method argument type.
U
,
the type of the result of the function: The method return type.
R
>
FailableBiFunction<T, U, R, Throwable>

Returns:

a correctly-typed wrapper for the given target.
asFailableBiFunction
(final Method
the method to invoke.
method
)

Produces a FailableBiFunction for a given a function Method.

public static <
the type of the first argument to the function: The type containing the method.
T
,
the type of the result of the function: The method return type.
R
>
FailableFunction<T, R, Throwable>

Returns:

a correctly-typed wrapper for the given target.
asFailableFunction
(final Method
the method to invoke.
method
)

Produces a FailableFunction for a given a supplier Method.

public static <
The Method return type.
R
>
FailableSupplier<R, Throwable>

Returns:

a correctly-typed wrapper for the given target.
asFailableSupplier
(final Method
the method to invoke.
method
)

Produces a FailableSupplier for a given a supplier Method.

public static <
the type of the first argument to the function: The type containing the method.
T
,
the type of the result of the function: The method return type.
R
>
Function<T, R>

Returns:

a correctly-typed wrapper for the given target.
asFunction
(final Method
the method to invoke.
method
)

Produces a Function for a given a supplier Method.

public static <
The interface type.
T
>
T

Returns:

a correctly-typed wrapper for the given target.
asInterfaceInstance
(final Class<T>
a class object representing T.
interfaceClass
,
final Method
the method to invoke.
method
)

Produces an instance of the given single-method interface which redirects its calls to the given method.

public static <
The Method return type.
R
>
Supplier<R>

Returns:

a correctly-typed wrapper for the given target.
asSupplier
(final Method
the method to invoke.
method
)

Produces a Supplier for a given a supplier Method.

private static Method

Returns:

The given method.
requireMethod
(final Method
The method to test.
method
)

Throws NullPointerException if method is null.

private static MethodHandle
unreflect(final Method method)

private static MethodHandle
unreflectUnchecked(final Method method)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

MethodInvokersback to summary
private MethodInvokers()

No need to create instances.

Method Detail

asBiConsumerback to summary
public static <T, U> BiConsumer<T, U> asBiConsumer(final Method method)

Produces a 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.

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.
method:Method

the method to invoke.

Returns:BiConsumer<T, U>

a correctly-typed wrapper for the given target.

Annotations
@SuppressWarnings:unchecked
asBiFunctionback to summary
public static <T, U, R> BiFunction<T, U, R> asBiFunction(final Method method)

Produces a 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));
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.
method:Method

the method to invoke.

Returns:BiFunction<T, U, R>

a correctly-typed wrapper for the given target.

Annotations
@SuppressWarnings:unchecked
asFailableBiConsumerback to summary
public static <T, U> FailableBiConsumer<T, U, Throwable> asFailableBiConsumer(final Method method)

Produces a 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.

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.
method:Method

the method to invoke.

Returns:FailableBiConsumer<T, U, Throwable>

a correctly-typed wrapper for the given target.

Annotations
@SuppressWarnings:unchecked
asFailableBiFunctionback to summary
public static <T, U, R> FailableBiFunction<T, U, R, Throwable> asFailableBiFunction(final Method method)

Produces a 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.

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.
method:Method

the method to invoke.

Returns:FailableBiFunction<T, U, R, Throwable>

a correctly-typed wrapper for the given target.

Annotations
@SuppressWarnings:unchecked
asFailableFunctionback to summary
public static <T, R> FailableFunction<T, R, Throwable> asFailableFunction(final Method method)

Produces a 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.

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.
method:Method

the method to invoke.

Returns:FailableFunction<T, R, Throwable>

a correctly-typed wrapper for the given target.

Annotations
@SuppressWarnings:unchecked
asFailableSupplierback to summary
public static <R> FailableSupplier<R, Throwable> asFailableSupplier(final Method method)

Produces a FailableSupplier for a given a supplier Method. The FailableSupplier return type must match the method's return type.

Only works with static methods.

Parameters
<R>
The Method return type.
method:Method

the method to invoke.

Returns:FailableSupplier<R, Throwable>

a correctly-typed wrapper for the given target.

Annotations
@SuppressWarnings:unchecked
asFunctionback to summary
public static <T, R> Function<T, R> asFunction(final Method method)

Produces a 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"));
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.
method:Method

the method to invoke.

Returns:Function<T, R>

a correctly-typed wrapper for the given target.

Annotations
@SuppressWarnings:unchecked
asInterfaceInstanceback to summary
public static <T> T asInterfaceInstance(final Class<T> interfaceClass, final Method method)

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).

Parameters
<T>
The interface type.
interfaceClass:Class<T>

a class object representing T.

method:Method

the method to invoke.

Returns:T

a correctly-typed wrapper for the given target.

See Also
MethodHandleProxies#asInterfaceInstance(Class, MethodHandle)
asSupplierback to summary
public static <R> Supplier<R> asSupplier(final Method method)

Produces a Supplier for a given a supplier Method. The Supplier return type must match the method's return type.

Only works with static methods.

Parameters
<R>
The Method return type.
method:Method

the method to invoke.

Returns:Supplier<R>

a correctly-typed wrapper for the given target.

Annotations
@SuppressWarnings:unchecked
requireMethodback to summary
private static Method requireMethod(final Method method)

Throws NullPointerException if method is null.

Parameters
method:Method

The method to test.

Returns:Method

The given method.

Exceptions
NullPointerException:
if method is null.
unreflectback to summary
private static MethodHandle unreflect(final Method method) throws IllegalAccessException
unreflectUncheckedback to summary
private static MethodHandle unreflectUnchecked(final Method method)