public class FunctionInterfaceUtil extends Object
| 构造器和说明 |
|---|
FunctionInterfaceUtil() |
| 限定符和类型 | 方法和说明 |
|---|---|
static <T> Method |
getFunctionInterfaceMethod(Class<?> clz)
Get the method from a function interface
|
static <T> T |
methodToFunctionInterface(Method method,
Object target,
Class<T> functionInterfaceClass,
Class<?>... explicitGenericTypes)
Adapt a method to a function interface.
|
public static <T> Method getFunctionInterfaceMethod(Class<?> clz)
clz - public static <T> T methodToFunctionInterface(Method method, Object target, Class<T> functionInterfaceClass, Class<?>... explicitGenericTypes)
static int increment(int i){
return i+1;
}
Method m = ...
UnaryOperator<Integer> uo = methodToFunctionInterface(m, null, UnaryOperator.class);//work
UnaryOperator<Integer> uo = methodToFunctionInterface(m, null, UnaryOperator.class, Integer.class);//work and more safe
UnaryOperator<Integer> uo = methodToFunctionInterface(m, null, UnaryOperator.class, String.class);//return null
Note that only indeed adapted method can be converted. Integer function(Number) can't adapt to UnaryOperator<Integer>, though any call to a
UnaryOperator<Integer> can delegate by the function. Because you can't define like:
interface Function extends UnaryOperator<Integer> {
@Override
public Integer apply(Number t);//error
}
Now only ignored things is ParameterizedType. That means this method consider return type and parameter types as
raw type.method - The method to adapt. Ensure the method can be access.target - The method's target. If the method is static, target should be null.functionInterfaceClass - The function interface to adapt to.explicitGenericTypes - If the function interface has generic type, you can specify them in order. If a
explicit type is null, it will be ignored.Copyright © 2018. All rights reserved.