用過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
!!!白嫖警告!!!
點個贊再走吧...