zoukankan      html  css  js  c++  java
  • java8 Lambda 获取方法引用的方法名

    用过mybatis plus都知道,使用LambdaQueryWrapper可以直接引用类的方法,非常的方便,其原理是使用序列化lambda和反序列化。但是有更简单的获取方式

    人狠话不多,直接上代码!!!!!!!(以下代码为根据参考的文章进行了优化调整。参考文章:https://blog.csdn.net/u012503481/article/details/100896507

        @FunctionalInterface
        public interface MyFun<T, R> extends java.util.function.Function<T, R>, Serializable {
    
        }
    
        public static void main(String[] args) throws Exception {
            test(MyTeacher::getAge);
        }
    
        private static<T> void test(MyFun<T, Object> myFun) throws Exception {
            // 直接调用writeReplace
            Method writeReplace = myFun.getClass().getDeclaredMethod("writeReplace");
            writeReplace.setAccessible(true);
            Object sl = writeReplace.invoke(myFun);
            SerializedLambda serializedLambda = (SerializedLambda) sl;
            System.out.println("serializedLambda数据为:"+serializedLambda);
            System.out.println("传入的方法名为:" + serializedLambda.getImplMethodName());
        }

    运行结果如下:

    serializedLambda数据为:SerializedLambda[capturingClass=class com.shotgun.my.api.po.pojos.defaultGroup.subGroup.MyTeacher, functionalInterfaceMethod=com/shotgun/my/api/po/pojos/defaultGroup/subGroup/MyTeacher$MyFun.apply:(Ljava/lang/Object;)Ljava/lang/Object;, implementation=invokeVirtual com/shotgun/my/api/po/pojos/defaultGroup/subGroup/MyTeacher.getAge:()Ljava/lang/Integer;, instantiatedMethodType=(Lcom/shotgun/my/api/po/pojos/defaultGroup/subGroup/MyTeacher;)Ljava/lang/Object;, numCaptured=0]
    传入的方法名为:getAge

    !!!白嫖警告!!!

    点个赞再走吧...

  • 相关阅读:
    PCL:描述三维离散点的ROPS特征(Code)
    veket智能机器人
    三维重建:SLAM的粒度和工程化问题
    DNN:逻辑回归与 SoftMax 回归方法
    人工智能:一种现代方法 第四版 翻译序言
    编程低级错误记录
    apache服务器配置防盗链(centos7)
    Linux下命令行中的复制和粘贴
    rm: cannot remove `libtoolT’: No such file or directory
    switch范围判断
  • 原文地址:https://www.cnblogs.com/wulm/p/13033052.html
Copyright © 2011-2022 走看看