32 lines
739 B
Java
32 lines
739 B
Java
|
|
package com.newyetai.common.functions;
|
||
|
|
|
||
|
|
import java.io.Serializable;
|
||
|
|
import java.lang.invoke.SerializedLambda;
|
||
|
|
import java.lang.reflect.Method;
|
||
|
|
import java.util.function.Function;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @author chenyun
|
||
|
|
* @description 序列化的Function
|
||
|
|
* @date 2020/9/16 13:38
|
||
|
|
*/
|
||
|
|
@FunctionalInterface
|
||
|
|
public interface SFunction<T, R> extends Function<T, R>, Serializable {
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 解析function为SerializedLambda
|
||
|
|
*
|
||
|
|
* @return
|
||
|
|
*/
|
||
|
|
default SerializedLambda serialized() {
|
||
|
|
try {
|
||
|
|
Method replaceMethod = getClass().getDeclaredMethod("writeReplace");
|
||
|
|
replaceMethod.setAccessible(true);
|
||
|
|
return (SerializedLambda) replaceMethod.invoke(this);
|
||
|
|
} catch (Exception e) {
|
||
|
|
throw new RuntimeException(e);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|