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));
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method 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 | Returns: a correctly-typed wrapper for the given target.the method to invoke. method)Produces a |
| 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 | Returns: a correctly-typed wrapper for the given target.the method to invoke. method)Produces a |
| 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 | Returns: a correctly-typed wrapper for the given target.the method to invoke. method)Produces a |
| 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 | Returns: a correctly-typed wrapper for the given target.the method to invoke. method)Produces a |
| 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 | Returns: a correctly-typed wrapper for the given target.the method to invoke. method)Produces a |
| public static < The Method return type. R> FailableSupplier | Returns: a correctly-typed wrapper for the given target.the method to invoke. method)Produces a |
| 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 | Returns: a correctly-typed wrapper for the given target.the method to invoke. method)Produces a |
| public static < The interface type. T> T | Returns: a correctly-typed wrapper for the given target.a class object representing interfaceClass, final Method T.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 | Returns: a correctly-typed wrapper for the given target.the method to invoke. method)Produces a |
| private static Method | Returns: The given method.The method to test. method)Throws NullPointerException if |
| private static MethodHandle | |
| private static MethodHandle |
| MethodInvokers | back to summary |
|---|---|
| private MethodInvokers() No need to create instances. | |
| asBiConsumer | back to summary |
|---|---|
| public static <T, U> BiConsumer Produces a
| |
| asBiFunction | back to summary |
|---|---|
| public static <T, U, R> BiFunction Produces a
For example to invoke
| |
| asFailableBiConsumer | back to summary |
|---|---|
| public static <T, U> FailableBiConsumer Produces a
| |
| asFailableBiFunction | back to summary |
|---|---|
| public static <T, U, R> FailableBiFunction Produces a
| |
| asFailableFunction | back to summary |
|---|---|
| public static <T, R> FailableFunction Produces a
| |
| asFailableSupplier | back to summary |
|---|---|
| public static <R> FailableSupplier Produces a Only works with static methods.
| |
| asFunction | back to summary |
|---|---|
| public static <T, R> Function Produces a
For example to invoke
| |
| asInterfaceInstance | back 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
| |
| asSupplier | back to summary |
|---|---|
| public static <R> Supplier Produces a Only works with static methods.
| |
| requireMethod | back to summary |
|---|---|
| private static Method requireMethod(final Method method) Throws NullPointerException if
| |
| unreflect | back to summary |
|---|---|
| private static MethodHandle unreflect(final Method method) throws IllegalAccessException | |
| unreflectUnchecked | back to summary |
|---|---|
| private static MethodHandle unreflectUnchecked(final Method method) | |