参照地址 https://blog.csdn.net/leisurelen/article/details/105980615

自我实现方式:也是看上述文章而来

import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
import java.util.function.Function;

public class LambdaTest {


    public static void main(String[] args) throws Exception {
        SerializedLambda serializedLambda = doSFunction(TestDemo::getId);
        System.out.println("方法名:" + serializedLambda.getImplMethodName());
        System.out.println("类名:" + serializedLambda.getImplClass());
    }

    private static <T, R> java.lang.invoke.SerializedLambda doSFunction(MFunction<T, R> func) throws Exception {
        // 直接调用writeReplace
        Method writeReplace = func.getClass().getDeclaredMethod("writeReplace");
        writeReplace.setAccessible(true);
          //反射调用
        Object sl = writeReplace.invoke(func);
        java.lang.invoke.SerializedLambda serializedLambda = (java.lang.invoke.SerializedLambda) sl;
        return serializedLambda;
    }
}
@FunctionalInterface
interface MFunction<T, R> extends Function<T, R>, Serializable {
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-02-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-28
  • 2021-10-24
  • 2022-12-23
  • 2022-02-14
  • 2022-02-11
  • 2021-09-06
  • 2022-12-23
相关资源
相似解决方案