import java.lang.invoke.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
/**
* Lambda 元工厂抽象基类(包私有,仅内部复用)。
* <p>
* 封装所有底层公共能力,供上层工具类继承复用,实现逻辑零冗余、缓存全局共享。
* 包含以下核心能力:
* <ul>
* <li>双层弱引用 Lambda 实例缓存,随类卸载自动清理,避免 Metaspace 溢出</li>
* <li>Trusted Lookup 权限突破,支持 private 方法访问与跨模块调用</li>
* <li>统一 Lambda 生成核心逻辑,自动处理签名适配与实例化</li>
* <li>标准化前置校验:静态/实例校验、参数数量校验、返回值类型校验、参数类型校验</li>
* </ul>
* <b>注意:</b>此类为内部抽象实现,禁止外部直接实例化或继承。
*/
abstract class AbstractLambdaMetaFactory {
// ==================== 公共常量 ====================
/**
* Runnable 系列接口单抽象方法名
*/
protected static final String RUN = "run";
/**
* Supplier 系列接口单抽象方法名
*/
protected static final String GET = "get";
/**
* Function / BiFunction 系列接口单抽象方法名
*/
protected static final String APPLY = "apply";
/**
* Consumer 系列接口单抽象方法名
*/
protected static final String ACCEPT = "accept";
/**
* Predicate 系列接口单抽象方法名
*/
protected static final String TEST = "test";
/**
* IntSupplier / LongSupplier 等供给型接口单抽象方法名
*/
protected static final String GET_AS_INT = "getAsInt";
protected static final String GET_AS_LONG = "getAsLong";
protected static final String GET_AS_DOUBLE = "getAsDouble";
protected static final String GET_AS_BOOLEAN = "getAsBoolean";
/**
* 原生基本类型运算/转换接口单抽象方法名
*/
protected static final String APPLY_AS_INT = "applyAsInt";
protected static final String APPLY_AS_LONG = "applyAsLong";
protected static final String APPLY_AS_DOUBLE = "applyAsDouble";
// ==================== 全局共享缓存 ====================
/**
* 缓存复合键:由「目标方法 + 函数式接口」唯一确定一个 Lambda 实例。
* <p>
* 不可变对象,equals/hashCode 基于两个字段计算,严格保证语义正确。
*/
private static final class CacheKey {
private final Method method;
private final Class<?> functionalInterface;
private final int hash; // 预缓存哈希值
CacheKey(Method method, Class<?> functionalInterface) {
this.method = method;
this.functionalInterface = functionalInterface;
// 构造时一次性计算完成
int result = method.hashCode();
result = 31 * result + functionalInterface.hashCode();
this.hash = result;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof CacheKey)) {
return false;
}
CacheKey cacheKey = (CacheKey) o;
// 可加哈希快速失败:哈希不同直接返回false,减少字段比较
if (hash != cacheKey.hash) {
return false;
}
return method.equals(cacheKey.method)
&& functionalInterface.equals(cacheKey.functionalInterface);
}
@Override
public int hashCode() {
return hash;
}
}
/**
* Lambda 实例全局缓存(扁平化版本)。
* <p>
* 基于 ClassValue 实现类级隔离,每个类对应一个并发 Map;
* 以「方法 + 函数式接口」作为复合键,直接映射到 Lambda 实例,
* 消除内层 Map 开销,结构更简洁,查找路径更短。
*/
protected static final ClassValue<ConcurrentHashMap<CacheKey, Object>> LAMBDA_CACHE
= new ClassValue<ConcurrentHashMap<CacheKey, Object>>() {
@Override
protected ConcurrentHashMap<CacheKey, Object> computeValue(Class<?> type) {
return new ConcurrentHashMap<>();
}
};
/**
* Trusted Lookup 全局缓存。
* <p>
* 基于 {@link ClassValue} 实现,生命周期与目标类完全绑定,
* 避免弱引用反向持有导致的内存泄漏,类卸载时自动释放。
*
* <br><br>
* TODO: JDK 9+ 移除 LOOKUP_CACHE 的定义,避免跨模块调用失败
*/
protected static final ClassValue<MethodHandles.Lookup> LOOKUP_CACHE
= new ClassValue<MethodHandles.Lookup>() {
@Override
protected MethodHandles.Lookup computeValue(Class<?> type) {
return createTrustedLookup(type, MethodHandles.lookup());
}
};
/**
* 受保护构造方法,禁止外部直接实例化。
*/
protected AbstractLambdaMetaFactory() {
}
// ==================== 核心生成方法 ====================
/**
* 核心 Lambda 实例生成方法(全局复用,缓存共享)。
* <p>
* 基于 {@link LambdaMetafactory#metafactory} 动态生成函数式接口实现类,
* 采用双重检查锁保证并发安全,生成后的实例写入全局缓存复用。
* <br><br>
* <tt>JDK 9+ 移除 LOOKUP_CACHE 的定义,避免跨模块调用失败<tt/>
* <pre>{@code
* // JDK 8 配合 LOOKUP_CACHE 缓存 MethodHandles.Lookup
* MethodHandles.Lookup lookup = LOOKUP_CACHE.get(method.getDeclaringClass());
*
* // JDK 9+ 获取当前调用者的 Lookup 对象,将 callerLookup 传入,以业务代码的身份去获取目标类的 Trusted Lookup
* MethodHandles.Lookup callerLookup = MethodHandles.lookup();
* MethodHandles.Lookup lookup = createTrustedLookup(method.getDeclaringClass(), callerLookup);
* }</pre>
*
* @param method 目标反射方法
* @param functionalInterface 目标函数式接口类型
* @param samMethodName 函数式接口单抽象方法(SAM)名称
* @param samMethodFunc SAM 方法签名构造函数(泛型擦除后的签名)
* @param instantiatedMethodFunc 具体化方法签名构造函数(真实类型签名)
* @param <T> 函数式接口泛型
* @return 生成的函数式接口实例
* @throws RuntimeException 若 Lambda 生成失败(签名不匹配、权限不足等)
*/
@SuppressWarnings("unchecked")
protected static <T> T createLambda(Method method,
Class<T> functionalInterface,
String samMethodName,
Function<MethodHandle, MethodType> samMethodFunc,
Function<MethodHandle, MethodType> instantiatedMethodFunc) {
// 获取当前类对应的缓存
ConcurrentHashMap<CacheKey, Object> classCache = LAMBDA_CACHE.get(method.getDeclaringClass());
// 构造查询键
CacheKey key = new CacheKey(method, functionalInterface);
// 一次原子操作完成「命中返回 / 不存在则生成」
return (T) classCache.computeIfAbsent(key, k -> {
try {
// TODO: JDK 9+ 需要修复此处,获取当前调用者的 Lookup 对象,必须在这里获取,
// 以获取调用方(Caller)的权限上下文,而不是工具类的上下文
// 使用传入的 callerLookup 创建 Trusted Lookup,解决跨模块访问问题
MethodHandles.Lookup lookup = LOOKUP_CACHE.get(method.getDeclaringClass());
MethodHandle implMethod = lookup.unreflect(method);
MethodType samMethodType = samMethodFunc.apply(implMethod);
MethodType instantiatedMethodType = instantiatedMethodFunc.apply(implMethod);
CallSite callSite = LambdaMetafactory.metafactory(
lookup,
samMethodName,
MethodType.methodType(functionalInterface),
samMethodType,
implMethod,
instantiatedMethodType
);
return callSite.getTarget().invoke();
} catch (Throwable e) {
throw new RuntimeException("Failed to create lambda proxy for method: " + method, e);
}
});
}
// ==================== 权限突破 ====================
/**
* 创建 Trusted Lookup 对象,支持访问 private 方法与跨模块调用。
* <tt>优先使用 JDK 9+ 官方 API {@link MethodHandles#privateLookupIn},
* 失败后降级为 JDK 8 兼容的反射构造 TRUSTED 模式 Lookup。</tt>
*
* @param targetClass 目标类
* @param caller 调用者的 Lookup 对象,通常由调用方通过 MethodHandles.lookup() 传入
* @return 拥有最高访问权限的 Lookup 对象
* @throws RuntimeException 若 Lookup 初始化失败
*/
private static MethodHandles.Lookup createTrustedLookup(Class<?> targetClass, MethodHandles.Lookup caller) {
// 优先尝试 JDK 9+ 官方 API,避免非法反射警告,使用传入的 ‘caller’ 作为查找的发起者
try {
Method privateLookupIn = MethodHandles.class.getMethod("privateLookupIn", Class.class, MethodHandles.Lookup.class);
return (MethodHandles.Lookup) privateLookupIn.invoke(null, targetClass, caller);
} catch (Throwable ignore) {
// 降级处理
}
// JDK 8 兼容方案:反射构造最高权限 Lookup
try {
Constructor<MethodHandles.Lookup> constructor = MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, int.class);
constructor.setAccessible(true);
// -1 代表 TRUSTED 模式,拥有全部访问权限
return constructor.newInstance(targetClass, -1);
} catch (Exception ex) {
throw new RuntimeException("Cannot initialize trusted lookup for " + targetClass, ex);
}
}
// ==================== 通用校验方法 ====================
/**
* 校验静态方法:修饰符 + 参数数量 + 返回值是否为 void。
*
* @param method 目标方法
* @param expectedParamCount 期望参数数量
* @param returnVoid 期望返回值是否为 void
* @throws IllegalArgumentException 若校验不通过
*/
protected static void checkStatic(Method method, int expectedParamCount, boolean returnVoid) {
if (!Modifier.isStatic(method.getModifiers())) {
throw new IllegalArgumentException("Method must be static: " + method);
}
if (method.getParameterCount() != expectedParamCount) {
throw new IllegalArgumentException("Static method parameter count mismatch. Expected: "
+ expectedParamCount + ", Actual: " + method.getParameterCount());
}
if (returnVoid && method.getReturnType() != void.class) {
throw new IllegalArgumentException("Method must return void: " + method);
}
if (!returnVoid && method.getReturnType() == void.class) {
throw new IllegalArgumentException("Method must not return void: " + method);
}
}
/**
* 校验静态方法:修饰符 + 参数数量 + 指定返回值类型。
*
* @param method 目标方法
* @param expectedParamCount 期望参数数量
* @param expectedReturnType 期望返回值类型
* @throws IllegalArgumentException 若校验不通过
*/
protected static void checkStatic(Method method, int expectedParamCount, Class<?> expectedReturnType) {
checkStatic(method, expectedParamCount, expectedReturnType == void.class);
if (method.getReturnType() != expectedReturnType) {
throw new IllegalArgumentException("Method return type mismatch. Expected: "
+ expectedReturnType + ", Actual: " + method.getReturnType());
}
}
/**
* 校验实例方法:修饰符 + 参数数量 + 返回值是否为 void。
*
* @param method 目标方法
* @param expectedParamCount 期望参数数量(不含 this)
* @param returnVoid 期望返回值是否为 void
* @throws IllegalArgumentException 若校验不通过
*/
protected static void checkInstance(Method method, int expectedParamCount, boolean returnVoid) {
if (Modifier.isStatic(method.getModifiers())) {
throw new IllegalArgumentException("Method must be an instance method: " + method);
}
if (method.getParameterCount() != expectedParamCount) {
throw new IllegalArgumentException("Instance method parameter count mismatch. Expected: "
+ expectedParamCount + ", Actual: " + method.getParameterCount());
}
if (returnVoid && method.getReturnType() != void.class) {
throw new IllegalArgumentException("Method must return void: " + method);
}
if (!returnVoid && method.getReturnType() == void.class) {
throw new IllegalArgumentException("Method must not return void: " + method);
}
}
/**
* 校验实例方法:修饰符 + 参数数量 + 指定返回值类型。
*
* @param method 目标方法
* @param expectedParamCount 期望参数数量(不含 this)
* @param expectedReturnType 期望返回值类型
* @throws IllegalArgumentException 若校验不通过
*/
protected static void checkInstance(Method method, int expectedParamCount, Class<?> expectedReturnType) {
checkInstance(method, expectedParamCount, expectedReturnType == void.class);
if (method.getReturnType() != expectedReturnType) {
throw new IllegalArgumentException("Method return type mismatch. Expected: "
+ expectedReturnType + ", Actual: " + method.getReturnType());
}
}
/**
* 校验指定位置的参数类型是否匹配。
*
* @param method 目标方法
* @param index 参数索引(从 0 开始)
* @param expectedType 期望参数类型
* @throws IllegalArgumentException 若参数类型不匹配
*/
protected static void checkParamType(Method method, int index, Class<?> expectedType) {
if (method.getParameterTypes()[index] != expectedType) {
throw new IllegalArgumentException("Parameter " + index + " type mismatch. Expected: "
+ expectedType + ", Actual: " + method.getParameterTypes()[index]);
}
}
}
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.function.*;
/**
* Lambda 元工厂通用工具类。
* <p>
* 将 {@link Method} 转换为标准函数式接口实例,替代传统 {@link Method#invoke} 反射调用,
* 一次性生成字节码后,调用性能接近原生方法调用。
*
* <h3>核心能力</h3>
* <ul>
* <li>突破 JDK 模块化与 private 访问权限限制(Trusted Lookup)</li>
* <li>自动处理基本类型拆装箱,适配泛型函数式接口</li>
* <li>双层弱引用缓存,避免 Metaspace 溢出与重复生成开销</li>
* <li>严格区分静态/实例方法,前置校验杜绝签名不匹配异常</li>
* <li>支持 3~9 参自定义函数式接口,突破 JDK 原生参数数量限制</li>
* </ul>
*
* <h3>实例方法转换规则</h3>
* 实例方法的接收者(this)将作为函数式接口的第一个参数,例如:
* <pre>
* 无参实例方法 → Function<BEAN, R> :apply(bean) 等价于 bean.method()
* 单参实例方法 → BiFunction<BEAN, T, R> :apply(bean, arg) 等价于 bean.method(arg)
* </pre>
*
* @see PrimitiveLambdaMetaFactoryKit 原生基本类型零拆装箱扩展版本
*/
public final class LambdaMetaFactoryKit extends AbstractLambdaMetaFactory {
/**
* 私有构造方法,工具类禁止实例化。
*/
private LambdaMetaFactoryKit() {
}
// region 1. 静态方法转换 API
/**
* 将静态无参 void 方法转换为 {@link Runnable}。
*
* @param method 目标静态无参 void 方法
* @return 转换后的 Runnable 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 0 或返回值非 void
*/
public static Runnable toStaticRunnable(Method method) {
checkStatic(method, 0, true);
return createLambda(method, Runnable.class, RUN,
methodHandle -> MethodType.methodType(void.class),
MethodHandle::type
);
}
/**
* 将静态无参方法转换为 {@link Supplier}。
*
* @param method 目标静态无参方法(非 void 返回)
* @param <R> 返回值类型
* @return 转换后的 Supplier 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 0 或返回值为 void
*/
public static <R> Supplier<R> toStaticSupplier(Method method) {
checkStatic(method, 0, false);
return createLambda(method, Supplier.class, GET,
methodHandle -> methodHandle.type().generic(),
MethodHandle::type
);
}
/**
* 将静态单参方法转换为 {@link Function}。
*
* @param method 目标静态单参方法(非 void 返回)
* @param <T> 参数类型
* @param <R> 返回值类型
* @return 转换后的 Function 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 1 或返回值为 void
*/
public static <T, R> Function<T, R> toStaticFunction(Method method) {
checkStatic(method, 1, false);
return createLambda(method, Function.class, APPLY,
methodHandle -> methodHandle.type().generic(),
MethodHandle::type
);
}
/**
* 将静态双参方法转换为 {@link BiFunction}。
*
* @param method 目标静态双参方法(非 void 返回)
* @param <T> 第一个参数类型
* @param <U> 第二个参数类型
* @param <R> 返回值类型
* @return 转换后的 BiFunction 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 2 或返回值为 void
*/
public static <T, U, R> BiFunction<T, U, R> toStaticBiFunction(Method method) {
checkStatic(method, 2, false);
return createLambda(method, BiFunction.class, APPLY,
methodHandle -> methodHandle.type().generic(),
MethodHandle::type
);
}
/**
* 将静态单参 void 方法转换为 {@link Consumer}。
*
* @param method 目标静态单参 void 方法
* @param <T> 参数类型
* @return 转换后的 Consumer 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 1 或返回值非 void
*/
public static <T> Consumer<T> toStaticConsumer(Method method) {
checkStatic(method, 1, true);
return createLambda(method, Consumer.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, Object.class),
MethodHandle::type
);
}
/**
* 将静态双参 void 方法转换为 {@link BiConsumer}。
*
* @param method 目标静态双参 void 方法
* @param <T> 第一个参数类型
* @param <U> 第二个参数类型
* @return 转换后的 BiConsumer 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 2 或返回值非 void
*/
public static <T, U> BiConsumer<T, U> toStaticBiConsumer(Method method) {
checkStatic(method, 2, true);
return createLambda(method, BiConsumer.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, Object.class, Object.class),
MethodHandle::type
);
}
// ---------- 静态多参 Function(3~8参) ----------
/**
* 将静态三参方法转换为 {@link Function3}。
*
* @param method 目标静态三参方法(非 void 返回)
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <R> 返回值类型
* @return 转换后的 Function3 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 3 或返回值为 void
*/
public static <A, B, C, R> Function3<A, B, C, R> toStaticFunction3(Method method) {
checkStatic(method, 3, false);
return createLambda(method, Function3.class, APPLY,
methodHandle -> methodHandle.type().generic(),
MethodHandle::type
);
}
/**
* 将静态四参方法转换为 {@link Function4}。
*
* @param method 目标静态四参方法(非 void 返回)
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
* @param <R> 返回值类型
* @return 转换后的 Function4 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 4 或返回值为 void
*/
public static <A, B, C, D, R> Function4<A, B, C, D, R> toStaticFunction4(Method method) {
checkStatic(method, 4, false);
return createLambda(method, Function4.class, APPLY,
methodHandle -> methodHandle.type().generic(),
MethodHandle::type
);
}
/**
* 将静态五参方法转换为 {@link Function5}。
*
* @param method 目标静态五参方法(非 void 返回)
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
* @param <E> 第五个参数类型
* @param <R> 返回值类型
* @return 转换后的 Function5 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 5 或返回值为 void
*/
public static <A, B, C, D, E, R> Function5<A, B, C, D, E, R> toStaticFunction5(Method method) {
checkStatic(method, 5, false);
return createLambda(method, Function5.class, APPLY,
methodHandle -> methodHandle.type().generic(),
MethodHandle::type
);
}
/**
* 将静态六参方法转换为 {@link Function6}。
*
* @param method 目标静态六参方法(非 void 返回)
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
* @param <E> 第五个参数类型
* @param <F> 第六个参数类型
* @param <R> 返回值类型
* @return 转换后的 Function6 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 6 或返回值为 void
*/
public static <A, B, C, D, E, F, R> Function6<A, B, C, D, E, F, R> toStaticFunction6(Method method) {
checkStatic(method, 6, false);
return createLambda(method, Function6.class, APPLY,
methodHandle -> methodHandle.type().generic(),
MethodHandle::type
);
}
/**
* 将静态七参方法转换为 {@link Function7}。
*
* @param method 目标静态七参方法(非 void 返回)
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
* @param <E> 第五个参数类型
* @param <F> 第六个参数类型
* @param <G> 第七个参数类型
* @param <R> 返回值类型
* @return 转换后的 Function7 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 7 或返回值为 void
*/
public static <A, B, C, D, E, F, G, R> Function7<A, B, C, D, E, F, G, R> toStaticFunction7(Method method) {
checkStatic(method, 7, false);
return createLambda(method, Function7.class, APPLY,
methodHandle -> methodHandle.type().generic(),
MethodHandle::type
);
}
/**
* 将静态八参方法转换为 {@link Function8}。
*
* @param method 目标静态八参方法(非 void 返回)
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
* @param <E> 第五个参数类型
* @param <F> 第六个参数类型
* @param <G> 第七个参数类型
* @param <H> 第八个参数类型
* @param <R> 返回值类型
* @return 转换后的 Function8 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 8 或返回值为 void
*/
public static <A, B, C, D, E, F, G, H, R> Function8<A, B, C, D, E, F, G, H, R> toStaticFunction8(Method method) {
checkStatic(method, 8, false);
return createLambda(method, Function8.class, APPLY,
methodHandle -> methodHandle.type().generic(),
MethodHandle::type
);
}
// ---------- 静态多参 Consumer(3~8参) ----------
/**
* 将静态三参 void 方法转换为 {@link Consumer3}。
*
* @param method 目标静态三参 void 方法
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @return 转换后的 Consumer3 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 3 或返回值非 void
*/
public static <A, B, C> Consumer3<A, B, C> toStaticConsumer3(Method method) {
checkStatic(method, 3, true);
return createLambda(method, Consumer3.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, Object.class, Object.class, Object.class),
MethodHandle::type
);
}
/**
* 将静态四参 void 方法转换为 {@link Consumer4}。
*
* @param method 目标静态四参 void 方法
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
* @return 转换后的 Consumer4 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 4 或返回值非 void
*/
public static <A, B, C, D> Consumer4<A, B, C, D> toStaticConsumer4(Method method) {
checkStatic(method, 4, true);
return createLambda(method, Consumer4.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, Object.class, Object.class, Object.class, Object.class),
MethodHandle::type
);
}
/**
* 将静态五参 void 方法转换为 {@link Consumer5}。
*
* @param method 目标静态五参 void 方法
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
* @param <E> 第五个参数类型
* @return 转换后的 Consumer5 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 5 或返回值非 void
*/
public static <A, B, C, D, E> Consumer5<A, B, C, D, E> toStaticConsumer5(Method method) {
checkStatic(method, 5, true);
return createLambda(method, Consumer5.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, Object.class, Object.class, Object.class, Object.class, Object.class),
MethodHandle::type
);
}
/**
* 将静态六参 void 方法转换为 {@link Consumer6}。
*
* @param method 目标静态六参 void 方法
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
* @param <E> 第五个参数类型
* @param <F> 第六个参数类型
* @return 转换后的 Consumer6 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 6 或返回值非 void
*/
public static <A, B, C, D, E, F> Consumer6<A, B, C, D, E, F> toStaticConsumer6(Method method) {
checkStatic(method, 6, true);
return createLambda(method, Consumer6.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class),
MethodHandle::type
);
}
/**
* 将静态七参 void 方法转换为 {@link Consumer7}。
*
* @param method 目标静态七参 void 方法
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
* @param <E> 第五个参数类型
* @param <F> 第六个参数类型
* @param <G> 第七个参数类型
* @return 转换后的 Consumer7 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 7 或返回值非 void
*/
public static <A, B, C, D, E, F, G> Consumer7<A, B, C, D, E, F, G> toStaticConsumer7(Method method) {
checkStatic(method, 7, true);
return createLambda(method, Consumer7.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class),
MethodHandle::type
);
}
/**
* 将静态八参 void 方法转换为 {@link Consumer8}。
*
* @param method 目标静态八参 void 方法
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
* @param <E> 第五个参数类型
* @param <F> 第六个参数类型
* @param <G> 第七个参数类型
* @param <H> 第八个参数类型
* @return 转换后的 Consumer8 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 8 或返回值非 void
*/
public static <A, B, C, D, E, F, G, H> Consumer8<A, B, C, D, E, F, G, H> toStaticConsumer8(Method method) {
checkStatic(method, 8, true);
return createLambda(method, Consumer8.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class),
MethodHandle::type
);
}
// endregion
// region 2. 实例方法转换 API (将 this 作为第一个参数)
/**
* 将实例无参方法转换为 {@link Function}。
* <p>
* 方法接收者(this)作为 Function 的第一个参数,调用 {@code apply(bean)} 等价于 {@code bean.method()}。
*
* @param method 目标实例无参方法(非 void 返回)
* @param <BEAN> 实例对象类型
* @param <R> 返回值类型
* @return 转换后的 Function 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 0 或返回值为 void
*/
public static <BEAN, R> Function<BEAN, R> toInstanceFunction(Method method) {
checkInstance(method, 0, false);
return createLambda(method, Function.class, APPLY,
methodHandle -> methodHandle.type().generic(),
MethodHandle::type
);
}
/**
* 将实例单参方法转换为 {@link BiFunction}。
* <p>
* 方法接收者(this)作为 BiFunction 的第一个参数,调用 {@code apply(bean, arg)} 等价于 {@code bean.method(arg)}。
*
* @param method 目标实例单参方法(非 void 返回)
* @param <BEAN> 实例对象类型
* @param <U> 方法参数类型
* @param <R> 返回值类型
* @return 转换后的 BiFunction 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 1 或返回值为 void
*/
public static <BEAN, U, R> BiFunction<BEAN, U, R> toInstanceBiFunction(Method method) {
checkInstance(method, 1, false);
return createLambda(method, BiFunction.class, APPLY,
methodHandle -> methodHandle.type().generic(),
MethodHandle::type
);
}
/**
* 将实例无参 void 方法转换为 {@link Consumer}。
* <p>
* 方法接收者(this)作为 Consumer 的参数,调用 {@code accept(bean)} 等价于 {@code bean.method()}。
*
* @param method 目标实例无参 void 方法
* @param <BEAN> 实例对象类型
* @return 转换后的 Consumer 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 0 或返回值非 void
*/
public static <BEAN> Consumer<BEAN> toInstanceConsumer(Method method) {
checkInstance(method, 0, true);
return createLambda(method, Consumer.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, Object.class),
MethodHandle::type
);
}
/**
* 将实例单参 void 方法转换为 {@link BiConsumer}。
* <p>
* 方法接收者(this)作为 BiConsumer 的第一个参数,调用 {@code accept(bean, arg)} 等价于 {@code bean.method(arg)}。
*
* @param method 目标实例单参 void 方法
* @param <BEAN> 实例对象类型
* @param <U> 方法参数类型
* @return 转换后的 BiConsumer 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 1 或返回值非 void
*/
public static <BEAN, U> BiConsumer<BEAN, U> toInstanceBiConsumer(Method method) {
checkInstance(method, 1, true);
return createLambda(method, BiConsumer.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, Object.class, Object.class),
MethodHandle::type
);
}
// ---------- 实例多参 Function(3~9参) ----------
/**
* 将实例双参方法转换为 {@link Function3}。
* <p>
* 方法接收者(this)作为第一个参数。
*
* @param method 目标实例双参方法(非 void 返回)
* @param <BEAN> 实例对象类型
* @param <A> 第一个方法参数类型
* @param <B> 第二个方法参数类型
* @param <R> 返回值类型
* @return 转换后的 Function3 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 2 或返回值为 void
*/
public static <BEAN, A, B, R> Function3<BEAN, A, B, R> toInstanceFunction3(Method method) {
checkInstance(method, 2, false);
return createLambda(method, Function3.class, APPLY,
methodHandle -> methodHandle.type().generic(),
MethodHandle::type
);
}
/**
* 将实例三参方法转换为 {@link Function4}。
*
* @param method 目标实例三参方法(非 void 返回)
* @param <BEAN> 实例对象类型
* @param <A> 第一个方法参数类型
* @param <B> 第二个方法参数类型
* @param <C> 第三个方法参数类型
* @param <R> 返回值类型
* @return 转换后的 Function4 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 3 或返回值为 void
*/
public static <BEAN, A, B, C, R> Function4<BEAN, A, B, C, R> toInstanceFunction4(Method method) {
checkInstance(method, 3, false);
return createLambda(method, Function4.class, APPLY,
methodHandle -> methodHandle.type().generic(),
MethodHandle::type
);
}
/**
* 将实例四参方法转换为 {@link Function5}。
*
* @param method 目标实例四参方法(非 void 返回)
* @param <BEAN> 实例对象类型
* @param <A> 第一个方法参数类型
* @param <B> 第二个方法参数类型
* @param <C> 第三个方法参数类型
* @param <D> 第四个方法参数类型
* @param <R> 返回值类型
* @return 转换后的 Function5 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 4 或返回值为 void
*/
public static <BEAN, A, B, C, D, R> Function5<BEAN, A, B, C, D, R> toInstanceFunction5(Method method) {
checkInstance(method, 4, false);
return createLambda(method, Function5.class, APPLY,
methodHandle -> methodHandle.type().generic(),
MethodHandle::type
);
}
/**
* 将实例五参方法转换为 {@link Function6}。
*
* @param method 目标实例五参方法(非 void 返回)
* @param <BEAN> 实例对象类型
* @param <A> 第一个方法参数类型
* @param <B> 第二个方法参数类型
* @param <C> 第三个方法参数类型
* @param <D> 第四个方法参数类型
* @param <E> 第五个方法参数类型
* @param <R> 返回值类型
* @return 转换后的 Function6 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 5 或返回值为 void
*/
public static <BEAN, A, B, C, D, E, R> Function6<BEAN, A, B, C, D, E, R> toInstanceFunction6(Method method) {
checkInstance(method, 5, false);
return createLambda(method, Function6.class, APPLY,
methodHandle -> methodHandle.type().generic(),
MethodHandle::type
);
}
/**
* 将实例六参方法转换为 {@link Function7}。
*
* @param method 目标实例六参方法(非 void 返回)
* @param <BEAN> 实例对象类型
* @param <A> 第一个方法参数类型
* @param <B> 第二个方法参数类型
* @param <C> 第三个方法参数类型
* @param <D> 第四个方法参数类型
* @param <E> 第五个方法参数类型
* @param <F> 第六个方法参数类型
* @param <R> 返回值类型
* @return 转换后的 Function7 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 6 或返回值为 void
*/
public static <BEAN, A, B, C, D, E, F, R> Function7<BEAN, A, B, C, D, E, F, R> toInstanceFunction7(Method method) {
checkInstance(method, 6, false);
return createLambda(method, Function7.class, APPLY,
methodHandle -> methodHandle.type().generic(),
MethodHandle::type
);
}
/**
* 将实例七参方法转换为 {@link Function8}。
*
* @param method 目标实例七参方法(非 void 返回)
* @param <BEAN> 实例对象类型
* @param <A> 第一个方法参数类型
* @param <B> 第二个方法参数类型
* @param <C> 第三个方法参数类型
* @param <D> 第四个方法参数类型
* @param <E> 第五个方法参数类型
* @param <F> 第六个方法参数类型
* @param <G> 第七个方法参数类型
* @param <R> 返回值类型
* @return 转换后的 Function8 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 7 或返回值为 void
*/
public static <BEAN, A, B, C, D, E, F, G, R> Function8<BEAN, A, B, C, D, E, F, G, R> toInstanceFunction8(Method method) {
checkInstance(method, 7, false);
return createLambda(method, Function8.class, APPLY,
methodHandle -> methodHandle.type().generic(),
MethodHandle::type
);
}
/**
* 将实例八参方法转换为 {@link Function9}。
*
* @param method 目标实例八参方法(非 void 返回)
* @param <BEAN> 实例对象类型
* @param <A> 第一个方法参数类型
* @param <B> 第二个方法参数类型
* @param <C> 第三个方法参数类型
* @param <D> 第四个方法参数类型
* @param <E> 第五个方法参数类型
* @param <F> 第六个方法参数类型
* @param <G> 第七个方法参数类型
* @param <H> 第八个方法参数类型
* @param <R> 返回值类型
* @return 转换后的 Function9 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 8 或返回值为 void
*/
public static <BEAN, A, B, C, D, E, F, G, H, R> Function9<BEAN, A, B, C, D, E, F, G, H, R> toInstanceFunction9(Method method) {
checkInstance(method, 8, false);
return createLambda(method, Function9.class, APPLY,
methodHandle -> methodHandle.type().generic(),
MethodHandle::type
);
}
// ---------- 实例多参 Consumer(3~9参) ----------
/**
* 将实例双参 void 方法转换为 {@link Consumer3}。
* <p>
* 方法接收者(this)作为第一个参数。
*
* @param method 目标实例双参 void 方法
* @param <BEAN> 实例对象类型
* @param <A> 第一个方法参数类型
* @param <B> 第二个方法参数类型
* @return 转换后的 Consumer3 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 2 或返回值非 void
*/
public static <BEAN, A, B> Consumer3<BEAN, A, B> toInstanceConsumer3(Method method) {
checkInstance(method, 2, true);
return createLambda(method, Consumer3.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, Object.class, Object.class, Object.class),
MethodHandle::type
);
}
/**
* 将实例三参 void 方法转换为 {@link Consumer4}。
*
* @param method 目标实例三参 void 方法
* @param <BEAN> 实例对象类型
* @param <A> 第一个方法参数类型
* @param <B> 第二个方法参数类型
* @param <C> 第三个方法参数类型
* @return 转换后的 Consumer4 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 3 或返回值非 void
*/
public static <BEAN, A, B, C> Consumer4<BEAN, A, B, C> toInstanceConsumer4(Method method) {
checkInstance(method, 3, true);
return createLambda(method, Consumer4.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, Object.class, Object.class, Object.class, Object.class),
MethodHandle::type
);
}
/**
* 将实例四参 void 方法转换为 {@link Consumer5}。
*
* @param method 目标实例四参 void 方法
* @param <BEAN> 实例对象类型
* @param <A> 第一个方法参数类型
* @param <B> 第二个方法参数类型
* @param <C> 第三个方法参数类型
* @param <D> 第四个方法参数类型
* @return 转换后的 Consumer5 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 4 或返回值非 void
*/
public static <BEAN, A, B, C, D> Consumer5<BEAN, A, B, C, D> toInstanceConsumer5(Method method) {
checkInstance(method, 4, true);
return createLambda(method, Consumer5.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, Object.class, Object.class, Object.class, Object.class, Object.class),
MethodHandle::type
);
}
/**
* 将实例五参 void 方法转换为 {@link Consumer6}。
*
* @param method 目标实例五参 void 方法
* @param <BEAN> 实例对象类型
* @param <A> 第一个方法参数类型
* @param <B> 第二个方法参数类型
* @param <C> 第三个方法参数类型
* @param <D> 第四个方法参数类型
* @param <E> 第五个方法参数类型
* @return 转换后的 Consumer6 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 5 或返回值非 void
*/
public static <BEAN, A, B, C, D, E> Consumer6<BEAN, A, B, C, D, E> toInstanceConsumer6(Method method) {
checkInstance(method, 5, true);
return createLambda(method, Consumer6.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class),
MethodHandle::type
);
}
/**
* 将实例六参 void 方法转换为 {@link Consumer7}。
*
* @param method 目标实例六参 void 方法
* @param <BEAN> 实例对象类型
* @param <A> 第一个方法参数类型
* @param <B> 第二个方法参数类型
* @param <C> 第三个方法参数类型
* @param <D> 第四个方法参数类型
* @param <E> 第五个方法参数类型
* @param <F> 第六个方法参数类型
* @return 转换后的 Consumer7 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 6 或返回值非 void
*/
public static <BEAN, A, B, C, D, E, F> Consumer7<BEAN, A, B, C, D, E, F> toInstanceConsumer7(Method method) {
checkInstance(method, 6, true);
return createLambda(method, Consumer7.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class),
MethodHandle::type
);
}
/**
* 将实例七参 void 方法转换为 {@link Consumer8}。
*
* @param method 目标实例七参 void 方法
* @param <BEAN> 实例对象类型
* @param <A> 第一个方法参数类型
* @param <B> 第二个方法参数类型
* @param <C> 第三个方法参数类型
* @param <D> 第四个方法参数类型
* @param <E> 第五个方法参数类型
* @param <F> 第六个方法参数类型
* @param <G> 第七个方法参数类型
* @return 转换后的 Consumer8 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 7 或返回值非 void
*/
public static <BEAN, A, B, C, D, E, F, G> Consumer8<BEAN, A, B, C, D, E, F, G> toInstanceConsumer8(Method method) {
checkInstance(method, 7, true);
return createLambda(method, Consumer8.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class),
MethodHandle::type
);
}
/**
* 将实例八参 void 方法转换为 {@link Consumer9}。
*
* @param method 目标实例八参 void 方法
* @param <BEAN> 实例对象类型
* @param <A> 第一个方法参数类型
* @param <B> 第二个方法参数类型
* @param <C> 第三个方法参数类型
* @param <D> 第四个方法参数类型
* @param <E> 第五个方法参数类型
* @param <F> 第六个方法参数类型
* @param <G> 第七个方法参数类型
* @param <H> 第八个方法参数类型
* @return 转换后的 Consumer9 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 8 或返回值非 void
*/
public static <BEAN, A, B, C, D, E, F, G, H> Consumer9<BEAN, A, B, C, D, E, F, G, H> toInstanceConsumer9(Method method) {
checkInstance(method, 8, true);
return createLambda(method, Consumer9.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class),
MethodHandle::type
);
}
// endregion
// region 自定义多参 Consumer 接口
/**
* 三参数消费型函数式接口。
*
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
*/
@FunctionalInterface
public interface Consumer3<A, B, C> {
/**
* 对给定参数执行操作。
*
* @param a 第一个参数
* @param b 第二个参数
* @param c 第三个参数
*/
void accept(A a, B b, C c);
/**
* 返回组合操作,先执行当前操作,再执行 after 操作。
*
* @param after 后续执行的操作
* @return 组合后的 Consumer3
* @throws NullPointerException 如果 after 为 null
*/
default Consumer3<A, B, C> andThen(Consumer3<? super A, ? super B, ? super C> after) {
Objects.requireNonNull(after);
return (a, b, c) -> {
accept(a, b, c);
after.accept(a, b, c);
};
}
}
/**
* 四参数消费型函数式接口。
*
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
*/
@FunctionalInterface
public interface Consumer4<A, B, C, D> {
/**
* 对给定参数执行操作。
*
* @param a 第一个参数
* @param b 第二个参数
* @param c 第三个参数
* @param d 第四个参数
*/
void accept(A a, B b, C c, D d);
/**
* 返回组合操作,先执行当前操作,再执行 after 操作。
*
* @param after 后续执行的操作
* @return 组合后的 Consumer4
* @throws NullPointerException 如果 after 为 null
*/
default Consumer4<A, B, C, D> andThen(Consumer4<? super A, ? super B, ? super C, ? super D> after) {
Objects.requireNonNull(after);
return (a, b, c, d) -> {
accept(a, b, c, d);
after.accept(a, b, c, d);
};
}
}
/**
* 五参数消费型函数式接口。
*
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
* @param <E> 第五个参数类型
*/
@FunctionalInterface
public interface Consumer5<A, B, C, D, E> {
void accept(A a, B b, C c, D d, E e);
default Consumer5<A, B, C, D, E> andThen(Consumer5<? super A, ? super B, ? super C, ? super D, ? super E> after) {
Objects.requireNonNull(after);
return (a, b, c, d, e) -> {
accept(a, b, c, d, e);
after.accept(a, b, c, d, e);
};
}
}
/**
* 六参数消费型函数式接口。
*
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
* @param <E> 第五个参数类型
* @param <F> 第六个参数类型
*/
@FunctionalInterface
public interface Consumer6<A, B, C, D, E, F> {
void accept(A a, B b, C c, D d, E e, F f);
default Consumer6<A, B, C, D, E, F> andThen(Consumer6<? super A, ? super B, ? super C, ? super D, ? super E, ? super F> after) {
Objects.requireNonNull(after);
return (a, b, c, d, e, f) -> {
accept(a, b, c, d, e, f);
after.accept(a, b, c, d, e, f);
};
}
}
/**
* 七参数消费型函数式接口。
*
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
* @param <E> 第五个参数类型
* @param <F> 第六个参数类型
* @param <G> 第七个参数类型
*/
@FunctionalInterface
public interface Consumer7<A, B, C, D, E, F, G> {
void accept(A a, B b, C c, D d, E e, F f, G g);
default Consumer7<A, B, C, D, E, F, G> andThen(Consumer7<? super A, ? super B, ? super C, ? super D, ? super E, ? super F, ? super G> after) {
Objects.requireNonNull(after);
return (a, b, c, d, e, f, g) -> {
accept(a, b, c, d, e, f, g);
after.accept(a, b, c, d, e, f, g);
};
}
}
/**
* 八参数消费型函数式接口。
*
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
* @param <E> 第五个参数类型
* @param <F> 第六个参数类型
* @param <G> 第七个参数类型
* @param <H> 第八个参数类型
*/
@FunctionalInterface
public interface Consumer8<A, B, C, D, E, F, G, H> {
void accept(A a, B b, C c, D d, E e, F f, G g, H h);
default Consumer8<A, B, C, D, E, F, G, H> andThen(Consumer8<? super A, ? super B, ? super C, ? super D, ? super E, ? super F, ? super G, ? super H> after) {
Objects.requireNonNull(after);
return (a, b, c, d, e, f, g, h) -> {
accept(a, b, c, d, e, f, g, h);
after.accept(a, b, c, d, e, f, g, h);
};
}
}
/**
* 九参数消费型函数式接口。
*
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
* @param <E> 第五个参数类型
* @param <F> 第六个参数类型
* @param <G> 第七个参数类型
* @param <H> 第八个参数类型
* @param <I> 第九个参数类型
*/
@FunctionalInterface
public interface Consumer9<A, B, C, D, E, F, G, H, I> {
void accept(A a, B b, C c, D d, E e, F f, G g, H h, I i);
default Consumer9<A, B, C, D, E, F, G, H, I> andThen(Consumer9<? super A, ? super B, ? super C, ? super D, ? super E, ? super F, ? super G, ? super H, ? super I> after) {
Objects.requireNonNull(after);
return (a, b, c, d, e, f, g, h, i) -> {
accept(a, b, c, d, e, f, g, h, i);
after.accept(a, b, c, d, e, f, g, h, i);
};
}
}
// endregion
// region 自定义多参 Function 接口
/**
* 三参数函数型函数式接口。
*
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <R> 返回值类型
*/
@FunctionalInterface
public interface Function3<A, B, C, R> {
/**
* 对给定参数执行函数并返回结果。
*
* @param a 第一个参数
* @param b 第二个参数
* @param c 第三个参数
* @return 函数结果
*/
R apply(A a, B b, C c);
/**
* 返回组合函数,先执行当前函数,再将结果传入 after 函数。
*
* @param after 后续执行的函数
* @param <V> 最终返回值类型
* @return 组合后的 Function3
* @throws NullPointerException 如果 after 为 null
*/
default <V> Function3<A, B, C, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (A a, B b, C c) -> after.apply(apply(a, b, c));
}
}
/**
* 四参数函数型函数式接口。
*
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
* @param <R> 返回值类型
*/
@FunctionalInterface
public interface Function4<A, B, C, D, R> {
R apply(A a, B b, C c, D d);
default <V> Function4<A, B, C, D, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (A a, B b, C c, D d) -> after.apply(apply(a, b, c, d));
}
}
/**
* 五参数函数型函数式接口。
*
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
* @param <E> 第五个参数类型
* @param <R> 返回值类型
*/
@FunctionalInterface
public interface Function5<A, B, C, D, E, R> {
R apply(A a, B b, C c, D d, E e);
default <V> Function5<A, B, C, D, E, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (A a, B b, C c, D d, E e) -> after.apply(apply(a, b, c, d, e));
}
}
/**
* 六参数函数型函数式接口。
*
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
* @param <E> 第五个参数类型
* @param <F> 第六个参数类型
* @param <R> 返回值类型
*/
@FunctionalInterface
public interface Function6<A, B, C, D, E, F, R> {
R apply(A a, B b, C c, D d, E e, F f);
default <V> Function6<A, B, C, D, E, F, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (A a, B b, C c, D d, E e, F f) -> after.apply(apply(a, b, c, d, e, f));
}
}
/**
* 七参数函数型函数式接口。
*
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
* @param <E> 第五个参数类型
* @param <F> 第六个参数类型
* @param <G> 第七个参数类型
* @param <R> 返回值类型
*/
@FunctionalInterface
public interface Function7<A, B, C, D, E, F, G, R> {
R apply(A a, B b, C c, D d, E e, F f, G g);
default <V> Function7<A, B, C, D, E, F, G, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (A a, B b, C c, D d, E e, F f, G g) -> after.apply(apply(a, b, c, d, e, f, g));
}
}
/**
* 八参数函数型函数式接口。
*
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
* @param <E> 第五个参数类型
* @param <F> 第六个参数类型
* @param <G> 第七个参数类型
* @param <H> 第八个参数类型
* @param <R> 返回值类型
*/
@FunctionalInterface
public interface Function8<A, B, C, D, E, F, G, H, R> {
R apply(A a, B b, C c, D d, E e, F f, G g, H h);
default <V> Function8<A, B, C, D, E, F, G, H, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (A a, B b, C c, D d, E e, F f, G g, H h) -> after.apply(apply(a, b, c, d, e, f, g, h));
}
}
/**
* 九参数函数型函数式接口。
*
* @param <A> 第一个参数类型
* @param <B> 第二个参数类型
* @param <C> 第三个参数类型
* @param <D> 第四个参数类型
* @param <E> 第五个参数类型
* @param <F> 第六个参数类型
* @param <G> 第七个参数类型
* @param <H> 第八个参数类型
* @param <I> 第九个参数类型
* @param <R> 返回值类型
*/
@FunctionalInterface
public interface Function9<A, B, C, D, E, F, G, H, I, R> {
R apply(A a, B b, C c, D d, E e, F f, G g, H h, I i);
default <V> Function9<A, B, C, D, E, F, G, H, I, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (A a, B b, C c, D d, E e, F f, G g, H h, I i) -> after.apply(apply(a, b, c, d, e, f, g, h, i));
}
}
// endregion
}
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;
import java.util.function.*;
/**
* 原生基本类型 Lambda 元工厂扩展工具类。
* <p>
* 专门用于高性能数值计算场景,基于 JDK 原生基本类型函数式接口实现<b>零拆装箱</b>调用,
* 相比泛型接口版本可获得 1~2 倍的吞吐量提升,调用性能无限接近原生方法。
*
* <h3>支持的接口分类</h3>
* <ul>
* <li><b>供给型</b>:IntSupplier / LongSupplier / DoubleSupplier / BooleanSupplier</li>
* <li><b>消费型</b>:IntConsumer / LongConsumer / DoubleConsumer</li>
* <li><b>一元运算</b>:IntUnaryOperator / LongUnaryOperator / DoubleUnaryOperator</li>
* <li><b>二元运算</b>:IntBinaryOperator / LongBinaryOperator / DoubleBinaryOperator</li>
* <li><b>跨类型转换</b>:IntToLongFunction / LongToIntFunction 等 6 种转换接口</li>
* <li><b>断言型</b>:IntPredicate / LongPredicate / DoublePredicate</li>
* <li><b>实例场景</b>:ToIntFunction / ObjIntConsumer 等对象+基本类型组合接口</li>
* </ul>
*
* <b>注意:</b>所有方法均为静态方法,与 {@link LambdaMetaFactoryKit} 共享全局缓存,
* 同一方法在两个工具类中转换不会重复生成字节码。
*
* @see LambdaMetaFactoryKit 通用泛型版本
*/
public final class PrimitiveLambdaMetaFactoryKit extends AbstractLambdaMetaFactory {
/**
* 私有构造方法,工具类禁止实例化。
*/
private PrimitiveLambdaMetaFactoryKit() {
}
// region 1. 静态方法转换 API
/**
* 将静态单参 int 返回引用类型方法转换为 {@link IntFunction}(入参零拆装箱)。
* <p>
* 目标方法签名要求:<b>{@code static R methodName(int)}</b>,其中 {@code R} 为任意引用类型。
* 转换后的 {@link IntFunction#apply(int)} 调用将直接映射到目标静态方法,无 {@code Integer} 装箱开销。
*
* <p><b>转换示例:</b>
* <pre>{@code
* // 目标方法
* public static String intToHex(int value) {
* return Integer.toHexString(value);
* }
*
* // 转换调用
* Method method = Foo.class.getMethod("intToHex", int.class);
* IntFunction<String> func = PrimitiveLambdaMetaFactoryKit.toStaticIntFunction(method);
* String hex = func.apply(255); // 等价于 Foo.intToHex(255),返回 "ff"
* }</pre>
*
* @param method 目标静态方法,必须满足:
* <ul>
* <li>修饰符为 {@code static}</li>
* <li>参数数量为 1</li>
* <li>第 0 个参数类型为 {@code int}</li>
* <li>返回值类型<b>非</b> {@code void}(必须为引用类型)</li>
* </ul>
* @param <R> 目标方法的返回值类型(引用类型)
* @return 转换后的 {@link IntFunction} 实例,调用性能接近原生静态方法
* @throws IllegalArgumentException 若方法非静态、参数数量不为 1、第 0 个参数非 {@code int} 或返回值为 {@code void}
* @throws RuntimeException 若 Lambda 元工厂生成失败(如签名不匹配、权限不足等)
* @see IntFunction
* @see LambdaMetaFactoryKit#toStaticFunction(Method) 泛型装箱版本
*/
public static <R> IntFunction<R> toStaticIntFunction(Method method) {
checkStatic(method, 1, false);
checkParamType(method, 0, int.class);
return createLambda(method, IntFunction.class, APPLY,
methodHandle -> MethodType.methodType(Object.class, int.class),
MethodHandle::type
);
}
/**
* 将静态单参 long 返回引用类型方法转换为 {@link LongFunction}(入参零拆装箱)。
* <p>
* 目标方法签名要求:<b>{@code static R methodName(long)}</b>,其中 {@code R} 为任意引用类型。
* 转换后的 {@link LongFunction#apply(long)} 调用将直接映射到目标静态方法,无 {@code Long} 装箱开销。
*
* <p><b>转换示例:</b>
* <pre>{@code
* // 目标方法
* public static String formatTimestamp(long epochMillis) {
* return new java.util.Date(epochMillis).toString();
* }
*
* // 转换调用
* Method method = Foo.class.getMethod("formatTimestamp", long.class);
* LongFunction<String> func = PrimitiveLambdaMetaFactoryKit.toStaticLongFunction(method);
* String dateStr = func.apply(System.currentTimeMillis()); // 等价于 Foo.formatTimestamp(ts)
* }</pre>
*
* @param method 目标静态方法,必须满足:
* <ul>
* <li>修饰符为 {@code static}</li>
* <li>参数数量为 1</li>
* <li>第 0 个参数类型为 {@code long}</li>
* <li>返回值类型<b>非</b> {@code void}(必须为引用类型)</li>
* </ul>
* @param <R> 目标方法的返回值类型(引用类型)
* @return 转换后的 {@link LongFunction} 实例,调用性能接近原生静态方法
* @throws IllegalArgumentException 若方法非静态、参数数量不为 1、第 0 个参数非 {@code long} 或返回值为 {@code void}
* @throws RuntimeException 若 Lambda 元工厂生成失败(如签名不匹配、权限不足等)
* @see LongFunction
*/
public static <R> LongFunction<R> toStaticLongFunction(Method method) {
checkStatic(method, 1, false);
checkParamType(method, 0, long.class);
return createLambda(method, LongFunction.class, APPLY,
methodHandle -> MethodType.methodType(Object.class, long.class),
MethodHandle::type
);
}
/**
* 将静态单参 double 返回引用类型方法转换为 {@link DoubleFunction}(入参零拆装箱)。
* <p>
* 目标方法签名要求:<b>{@code static R methodName(double)}</b>,其中 {@code R} 为任意引用类型。
* 转换后的 {@link DoubleFunction#apply(double)} 调用将直接映射到目标静态方法,无 {@code Double} 装箱开销。
*
* <p><b>转换示例:</b>
* <pre>{@code
* // 目标方法
* public static String formatDouble(double value) {
* return String.format("%.4f", value);
* }
*
* // 转换调用
* Method method = Foo.class.getMethod("formatDouble", double.class);
* DoubleFunction<String> func = PrimitiveLambdaMetaFactoryKit.toStaticDoubleFunction(method);
* String str = func.apply(3.1415926); // 等价于 Foo.formatDouble(3.1415926),返回 "3.1416"
* }</pre>
*
* @param method 目标静态方法,必须满足:
* <ul>
* <li>修饰符为 {@code static}</li>
* <li>参数数量为 1</li>
* <li>第 0 个参数类型为 {@code double}</li>
* <li>返回值类型<b>非</b> {@code void}(必须为引用类型)</li>
* </ul>
* @param <R> 目标方法的返回值类型(引用类型)
* @return 转换后的 {@link DoubleFunction} 实例,调用性能接近原生静态方法
* @throws IllegalArgumentException 若方法非静态、参数数量不为 1、第 0 个参数非 {@code double} 或返回值为 {@code void}
* @throws RuntimeException 若 Lambda 元工厂生成失败(如签名不匹配、权限不足等)
* @see DoubleFunction
*/
public static <R> DoubleFunction<R> toStaticDoubleFunction(Method method) {
checkStatic(method, 1, false);
checkParamType(method, 0, double.class); // 修复:原代码误写为 long.class
return createLambda(method, DoubleFunction.class, APPLY,
methodHandle -> MethodType.methodType(Object.class, double.class),
MethodHandle::type
);
}
// ==================== 静态:供给型接口 ====================
/**
* 将静态无参 int 返回方法转换为 {@link IntSupplier}(零拆装箱)。
*
* @param method 目标静态无参方法,返回值必须为 int
* @return 转换后的 IntSupplier 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 0 或返回值非 int
*/
public static IntSupplier toStaticIntSupplier(Method method) {
checkStatic(method, 0, int.class);
return createLambda(method, IntSupplier.class, GET_AS_INT,
methodHandle -> MethodType.methodType(int.class),
MethodHandle::type
);
}
/**
* 将静态无参 long 返回方法转换为 {@link LongSupplier}(零拆装箱)。
*
* @param method 目标静态无参方法,返回值必须为 long
* @return 转换后的 LongSupplier 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 0 或返回值非 long
*/
public static LongSupplier toStaticLongSupplier(Method method) {
checkStatic(method, 0, long.class);
return createLambda(method, LongSupplier.class, GET_AS_LONG,
methodHandle -> MethodType.methodType(long.class),
MethodHandle::type
);
}
/**
* 将静态无参 double 返回方法转换为 {@link DoubleSupplier}(零拆装箱)。
*
* @param method 目标静态无参方法,返回值必须为 double
* @return 转换后的 DoubleSupplier 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 0 或返回值非 double
*/
public static DoubleSupplier toStaticDoubleSupplier(Method method) {
checkStatic(method, 0, double.class);
return createLambda(method, DoubleSupplier.class, GET_AS_DOUBLE,
methodHandle -> MethodType.methodType(double.class),
MethodHandle::type
);
}
/**
* 将静态无参 boolean 返回方法转换为 {@link BooleanSupplier}(零拆装箱)。
*
* @param method 目标静态无参方法,返回值必须为 boolean
* @return 转换后的 BooleanSupplier 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 0 或返回值非 boolean
*/
public static BooleanSupplier toStaticBooleanSupplier(Method method) {
checkStatic(method, 0, boolean.class);
return createLambda(method, BooleanSupplier.class, GET_AS_BOOLEAN,
methodHandle -> MethodType.methodType(boolean.class),
MethodHandle::type
);
}
// ==================== 静态:消费型接口 ====================
/**
* 将静态单参 int void 方法转换为 {@link IntConsumer}(零拆装箱)。
*
* @param method 目标静态单参 void 方法,参数必须为 int
* @return 转换后的 IntConsumer 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 1、返回值非 void 或参数类型非 int
*/
public static IntConsumer toStaticIntConsumer(Method method) {
checkStatic(method, 1, void.class);
checkParamType(method, 0, int.class);
return createLambda(method, IntConsumer.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, int.class),
MethodHandle::type
);
}
/**
* 将静态单参 long void 方法转换为 {@link LongConsumer}(零拆装箱)。
*
* @param method 目标静态单参 void 方法,参数必须为 long
* @return 转换后的 LongConsumer 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 1、返回值非 void 或参数类型非 long
*/
public static LongConsumer toStaticLongConsumer(Method method) {
checkStatic(method, 1, void.class);
checkParamType(method, 0, long.class);
return createLambda(method, LongConsumer.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, long.class),
MethodHandle::type
);
}
/**
* 将静态单参 double void 方法转换为 {@link DoubleConsumer}(零拆装箱)。
*
* @param method 目标静态单参 void 方法,参数必须为 double
* @return 转换后的 DoubleConsumer 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 1、返回值非 void 或参数类型非 double
*/
public static DoubleConsumer toStaticDoubleConsumer(Method method) {
checkStatic(method, 1, void.class);
checkParamType(method, 0, double.class);
return createLambda(method, DoubleConsumer.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, double.class),
MethodHandle::type
);
}
// ==================== 静态:一元运算接口 ====================
/**
* 将静态单参 int 返回 int 方法转换为 {@link IntUnaryOperator}(零拆装箱)。
*
* @param method 目标静态单参方法,参数和返回值均为 int
* @return 转换后的 IntUnaryOperator 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 1 或参数/返回值类型不匹配
*/
public static IntUnaryOperator toStaticIntUnaryOperator(Method method) {
checkStatic(method, 1, int.class);
checkParamType(method, 0, int.class);
return createLambda(method, IntUnaryOperator.class, APPLY_AS_INT,
methodHandle -> MethodType.methodType(int.class, int.class),
MethodHandle::type
);
}
/**
* 将静态单参 long 返回 long 方法转换为 {@link LongUnaryOperator}(零拆装箱)。
*
* @param method 目标静态单参方法,参数和返回值均为 long
* @return 转换后的 LongUnaryOperator 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 1 或参数/返回值类型不匹配
*/
public static LongUnaryOperator toStaticLongUnaryOperator(Method method) {
checkStatic(method, 1, long.class);
checkParamType(method, 0, long.class);
return createLambda(method, LongUnaryOperator.class, APPLY_AS_LONG,
methodHandle -> MethodType.methodType(long.class, long.class),
MethodHandle::type
);
}
/**
* 将静态单参 double 返回 double 方法转换为 {@link DoubleUnaryOperator}(零拆装箱)。
*
* @param method 目标静态单参方法,参数和返回值均为 double
* @return 转换后的 DoubleUnaryOperator 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 1 或参数/返回值类型不匹配
*/
public static DoubleUnaryOperator toStaticDoubleUnaryOperator(Method method) {
checkStatic(method, 1, double.class);
checkParamType(method, 0, double.class);
return createLambda(method, DoubleUnaryOperator.class, APPLY_AS_DOUBLE,
methodHandle -> MethodType.methodType(double.class, double.class),
MethodHandle::type
);
}
// ==================== 静态:二元运算接口 ====================
/**
* 将静态双参 int 返回 int 方法转换为 {@link IntBinaryOperator}(零拆装箱)。
*
* @param method 目标静态双参方法,两个参数和返回值均为 int
* @return 转换后的 IntBinaryOperator 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 2 或参数/返回值类型不匹配
*/
public static IntBinaryOperator toStaticIntBinaryOperator(Method method) {
checkStatic(method, 2, int.class);
checkParamType(method, 0, int.class);
checkParamType(method, 1, int.class);
return createLambda(method, IntBinaryOperator.class, APPLY_AS_INT,
methodHandle -> MethodType.methodType(int.class, int.class, int.class),
MethodHandle::type
);
}
/**
* 将静态双参 long 返回 long 方法转换为 {@link LongBinaryOperator}(零拆装箱)。
*
* @param method 目标静态双参方法,两个参数和返回值均为 long
* @return 转换后的 LongBinaryOperator 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 2 或参数/返回值类型不匹配
*/
public static LongBinaryOperator toStaticLongBinaryOperator(Method method) {
checkStatic(method, 2, long.class);
checkParamType(method, 0, long.class);
checkParamType(method, 1, long.class);
return createLambda(method, LongBinaryOperator.class, APPLY_AS_LONG,
methodHandle -> MethodType.methodType(long.class, long.class, long.class),
MethodHandle::type
);
}
/**
* 将静态双参 double 返回 double 方法转换为 {@link DoubleBinaryOperator}(零拆装箱)。
*
* @param method 目标静态双参方法,两个参数和返回值均为 double
* @return 转换后的 DoubleBinaryOperator 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 2 或参数/返回值类型不匹配
*/
public static DoubleBinaryOperator toStaticDoubleBinaryOperator(Method method) {
checkStatic(method, 2, double.class);
checkParamType(method, 0, double.class);
checkParamType(method, 1, double.class);
return createLambda(method, DoubleBinaryOperator.class, APPLY_AS_DOUBLE,
methodHandle -> MethodType.methodType(double.class, double.class, double.class),
MethodHandle::type
);
}
// ==================== 静态:跨类型转换接口 ====================
/**
* 将静态单参 int 返回 long 方法转换为 {@link IntToLongFunction}(零拆装箱)。
*
* @param method 目标静态单参方法,参数为 int,返回值为 long
* @return 转换后的 IntToLongFunction 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 1 或参数/返回值类型不匹配
*/
public static IntToLongFunction toStaticIntToLongFunction(Method method) {
checkStatic(method, 1, long.class);
checkParamType(method, 0, int.class);
return createLambda(method, IntToLongFunction.class, APPLY_AS_LONG,
methodHandle -> MethodType.methodType(long.class, int.class),
MethodHandle::type
);
}
/**
* 将静态单参 int 返回 double 方法转换为 {@link IntToDoubleFunction}(零拆装箱)。
*
* @param method 目标静态单参方法,参数为 int,返回值为 double
* @return 转换后的 IntToDoubleFunction 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 1 或参数/返回值类型不匹配
*/
public static IntToDoubleFunction toStaticIntToDoubleFunction(Method method) {
checkStatic(method, 1, double.class);
checkParamType(method, 0, int.class);
return createLambda(method, IntToDoubleFunction.class, APPLY_AS_DOUBLE,
methodHandle -> MethodType.methodType(double.class, int.class),
MethodHandle::type
);
}
/**
* 将静态单参 long 返回 int 方法转换为 {@link LongToIntFunction}(零拆装箱)。
*
* @param method 目标静态单参方法,参数为 long,返回值为 int
* @return 转换后的 LongToIntFunction 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 1 或参数/返回值类型不匹配
*/
public static LongToIntFunction toStaticLongToIntFunction(Method method) {
checkStatic(method, 1, int.class);
checkParamType(method, 0, long.class);
return createLambda(method, LongToIntFunction.class, APPLY_AS_INT,
methodHandle -> MethodType.methodType(int.class, long.class),
MethodHandle::type
);
}
/**
* 将静态单参 long 返回 double 方法转换为 {@link LongToDoubleFunction}(零拆装箱)。
*
* @param method 目标静态单参方法,参数为 long,返回值为 double
* @return 转换后的 LongToDoubleFunction 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 1 或参数/返回值类型不匹配
*/
public static LongToDoubleFunction toStaticLongToDoubleFunction(Method method) {
checkStatic(method, 1, double.class);
checkParamType(method, 0, long.class);
return createLambda(method, LongToDoubleFunction.class, APPLY_AS_DOUBLE,
methodHandle -> MethodType.methodType(double.class, long.class),
MethodHandle::type
);
}
/**
* 将静态单参 double 返回 int 方法转换为 {@link DoubleToIntFunction}(零拆装箱)。
*
* @param method 目标静态单参方法,参数为 double,返回值为 int
* @return 转换后的 DoubleToIntFunction 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 1 或参数/返回值类型不匹配
*/
public static DoubleToIntFunction toStaticDoubleToIntFunction(Method method) {
checkStatic(method, 1, int.class);
checkParamType(method, 0, double.class);
return createLambda(method, DoubleToIntFunction.class, APPLY_AS_INT,
methodHandle -> MethodType.methodType(int.class, double.class),
MethodHandle::type
);
}
/**
* 将静态单参 double 返回 long 方法转换为 {@link DoubleToLongFunction}(零拆装箱)。
*
* @param method 目标静态单参方法,参数为 double,返回值为 long
* @return 转换后的 DoubleToLongFunction 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 1 或参数/返回值类型不匹配
*/
public static DoubleToLongFunction toStaticDoubleToLongFunction(Method method) {
checkStatic(method, 1, long.class);
checkParamType(method, 0, double.class);
return createLambda(method, DoubleToLongFunction.class, APPLY_AS_LONG,
methodHandle -> MethodType.methodType(long.class, double.class),
MethodHandle::type
);
}
// ==================== 静态:断言型接口 ====================
/**
* 将静态单参 int 返回 boolean 方法转换为 {@link IntPredicate}(零拆装箱)。
*
* @param method 目标静态单参方法,参数为 int,返回值为 boolean
* @return 转换后的 IntPredicate 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 1 或参数/返回值类型不匹配
*/
public static IntPredicate toStaticIntPredicate(Method method) {
checkStatic(method, 1, boolean.class);
checkParamType(method, 0, int.class);
return createLambda(method, IntPredicate.class, TEST,
methodHandle -> MethodType.methodType(boolean.class, int.class),
MethodHandle::type
);
}
/**
* 将静态单参 long 返回 boolean 方法转换为 {@link LongPredicate}(零拆装箱)。
*
* @param method 目标静态单参方法,参数为 long,返回值为 boolean
* @return 转换后的 LongPredicate 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 1 或参数/返回值类型不匹配
*/
public static LongPredicate toStaticLongPredicate(Method method) {
checkStatic(method, 1, boolean.class);
checkParamType(method, 0, long.class);
return createLambda(method, LongPredicate.class, TEST,
methodHandle -> MethodType.methodType(boolean.class, long.class),
MethodHandle::type
);
}
/**
* 将静态单参 double 返回 boolean 方法转换为 {@link DoublePredicate}(零拆装箱)。
*
* @param method 目标静态单参方法,参数为 double,返回值为 boolean
* @return 转换后的 DoublePredicate 实例
* @throws IllegalArgumentException 若方法非静态、参数数量不为 1 或参数/返回值类型不匹配
*/
public static DoublePredicate toStaticDoublePredicate(Method method) {
checkStatic(method, 1, boolean.class);
checkParamType(method, 0, double.class);
return createLambda(method, DoublePredicate.class, TEST,
methodHandle -> MethodType.methodType(boolean.class, double.class),
MethodHandle::type
);
}
// endregion
// region 2. 实例方法转换 API (将 this 作为第一个参数)
// ==================== 实例:原生返回值接口 ====================
/**
* 将实例无参 int 返回方法转换为 {@link ToIntFunction}(返回值零拆装箱)。
* <p>
* 方法接收者(this)作为函数参数,调用 {@code applyAsInt(bean)} 等价于 {@code bean.method()}。
*
* @param method 目标实例无参方法,返回值必须为 int
* @param <BEAN> 实例对象类型
* @return 转换后的 ToIntFunction 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 0 或返回值非 int
*/
public static <BEAN> ToIntFunction<BEAN> toInstanceToIntFunction(Method method) {
checkInstance(method, 0, int.class);
return createLambda(method, ToIntFunction.class, APPLY_AS_INT,
methodHandle -> MethodType.methodType(int.class, Object.class),
MethodHandle::type
);
}
/**
* 将实例无参 long 返回方法转换为 {@link ToLongFunction}(返回值零拆装箱)。
*
* @param method 目标实例无参方法,返回值必须为 long
* @param <BEAN> 实例对象类型
* @return 转换后的 ToLongFunction 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 0 或返回值非 long
*/
public static <BEAN> ToLongFunction<BEAN> toInstanceToLongFunction(Method method) {
checkInstance(method, 0, long.class);
return createLambda(method, ToLongFunction.class, APPLY_AS_LONG,
methodHandle -> MethodType.methodType(long.class, Object.class),
MethodHandle::type
);
}
/**
* 将实例无参 double 返回方法转换为 {@link ToDoubleFunction}(返回值零拆装箱)。
*
* @param method 目标实例无参方法,返回值必须为 double
* @param <BEAN> 实例对象类型
* @return 转换后的 ToDoubleFunction 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 0 或返回值非 double
*/
public static <BEAN> ToDoubleFunction<BEAN> toInstanceToDoubleFunction(Method method) {
checkInstance(method, 0, double.class);
return createLambda(method, ToDoubleFunction.class, APPLY_AS_DOUBLE,
methodHandle -> MethodType.methodType(double.class, Object.class),
MethodHandle::type
);
}
// ==================== 实例:原生入参消费接口 ====================
/**
* 将实例单参 int void 方法转换为 {@link ObjIntConsumer}(入参零拆装箱)。
* <p>
* 方法接收者(this)作为第一个参数,调用 {@code accept(bean, value)} 等价于 {@code bean.method(value)}。
*
* @param method 目标实例单参 void 方法,参数必须为 int
* @param <BEAN> 实例对象类型
* @return 转换后的 ObjIntConsumer 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 1、返回值非 void 或参数类型非 int
*/
public static <BEAN> ObjIntConsumer<BEAN> toInstanceObjIntConsumer(Method method) {
checkInstance(method, 1, void.class);
checkParamType(method, 0, int.class);
return createLambda(method, ObjIntConsumer.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, Object.class, int.class),
MethodHandle::type
);
}
/**
* 将实例单参 long void 方法转换为 {@link ObjLongConsumer}(入参零拆装箱)。
*
* @param method 目标实例单参 void 方法,参数必须为 long
* @param <BEAN> 实例对象类型
* @return 转换后的 ObjLongConsumer 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 1、返回值非 void 或参数类型非 long
*/
public static <BEAN> ObjLongConsumer<BEAN> toInstanceObjLongConsumer(Method method) {
checkInstance(method, 1, void.class);
checkParamType(method, 0, long.class);
return createLambda(method, ObjLongConsumer.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, Object.class, long.class),
MethodHandle::type
);
}
/**
* 将实例单参 double void 方法转换为 {@link ObjDoubleConsumer}(入参零拆装箱)。
* <p>
* 方法接收者(this)作为第一个参数,调用 {@code accept(bean, value)} 等价于 {@code bean.method(value)}。
*
* @param method 目标实例单参 void 方法,参数必须为 double
* @param <BEAN> 实例对象类型
* @return 转换后的 ObjDoubleConsumer 实例
* @throws IllegalArgumentException 若方法为静态、参数数量不为 1、返回值非 void 或参数类型非 double
*/
public static <BEAN> ObjDoubleConsumer<BEAN> toInstanceObjDoubleConsumer(Method method) {
checkInstance(method, 1, void.class);
checkParamType(method, 0, double.class);
return createLambda(method, ObjDoubleConsumer.class, ACCEPT,
methodHandle -> MethodType.methodType(void.class, Object.class, double.class),
MethodHandle::type
);
}
// endregion
}