用过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
!!!白嫖警告!!!
点个赞再走吧...