Browse Source

refactor(SerializedLambda.java):重构部分方法的签名,优化 toString() 显示

HCL 6 năm trước cách đây
mục cha
commit
a45725e80a

+ 4 - 5
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/AbstractLambdaWrapper.java

@@ -15,17 +15,16 @@
  */
  */
 package com.baomidou.mybatisplus.core.conditions;
 package com.baomidou.mybatisplus.core.conditions;
 
 
-import java.util.Map;
-import java.util.Optional;
-
 import com.baomidou.mybatisplus.core.toolkit.Assert;
 import com.baomidou.mybatisplus.core.toolkit.Assert;
 import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
 import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
 import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
 import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
-import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.support.Property;
 import com.baomidou.mybatisplus.core.toolkit.support.Property;
 import com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda;
 import com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda;
 
 
+import java.util.Map;
+import java.util.Optional;
+
 /**
 /**
  * <p>
  * <p>
  * Lambda 语法使用 Wrapper
  * Lambda 语法使用 Wrapper
@@ -50,7 +49,7 @@ public abstract class AbstractLambdaWrapper<T, This extends AbstractLambdaWrappe
     private String getColumn(SerializedLambda lambda) {
     private String getColumn(SerializedLambda lambda) {
         String fieldName = StringUtils.resolveFieldName(lambda.getImplMethodName());
         String fieldName = StringUtils.resolveFieldName(lambda.getImplMethodName());
         if (!initColumnMap || columnMap.get(fieldName) == null) {
         if (!initColumnMap || columnMap.get(fieldName) == null) {
-            String entityClassName = lambda.getImplClass().replace(StringPool.SLASH, StringPool.DOT);
+            String entityClassName = lambda.getImplClassName();
             columnMap = LambdaUtils.getColumnMap(entityClassName);
             columnMap = LambdaUtils.getColumnMap(entityClassName);
             Assert.notEmpty(columnMap, "该模式不能应用于非 baseMapper 的泛型 entity 之外的 entity!");
             Assert.notEmpty(columnMap, "该模式不能应用于非 baseMapper 的泛型 entity 之外的 entity!");
             initColumnMap = true;
             initColumnMap = true;

+ 14 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/ClassUtils.java

@@ -100,4 +100,18 @@ public final class ClassUtils {
         }
         }
     }
     }
 
 
+    /**
+     * 请仅在确定类存在的情况下调用该方法
+     *
+     * @param name 类名称
+     * @return 返回转换后的 Class
+     */
+    public static Class<?> toClassConfident(String name) {
+        try {
+            return Class.forName(name);
+        } catch (ClassNotFoundException e) {
+            throw ExceptionUtils.mpe("找不到指定的class!请仅在明确确定会有 class 的时候,调用该方法", e);
+        }
+    }
+
 }
 }

+ 5 - 3
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/LambdaUtils.java

@@ -28,7 +28,7 @@ import java.util.concurrent.ConcurrentHashMap;
 
 
 /**
 /**
  * <p>
  * <p>
- * Lambda 工具类
+ * Lambda 解析工具类
  * </p>
  * </p>
  *
  *
  * @author HCL
  * @author HCL
@@ -38,6 +38,9 @@ public final class LambdaUtils {
 
 
     private static final Map<String, Map<String, String>> LAMBDA_CACHE = new ConcurrentHashMap<>();
     private static final Map<String, Map<String, String>> LAMBDA_CACHE = new ConcurrentHashMap<>();
 
 
+    /**
+     * SerializedLambda 反序列化缓存
+     */
     private static final Map<Class, WeakReference<SerializedLambda>> FUNC_CACHE = new ConcurrentHashMap<>();
     private static final Map<Class, WeakReference<SerializedLambda>> FUNC_CACHE = new ConcurrentHashMap<>();
 
 
     /**
     /**
@@ -54,7 +57,7 @@ public final class LambdaUtils {
         return Optional.ofNullable(FUNC_CACHE.get(clazz))
         return Optional.ofNullable(FUNC_CACHE.get(clazz))
             .map(WeakReference::get)
             .map(WeakReference::get)
             .orElseGet(() -> {
             .orElseGet(() -> {
-                SerializedLambda lambda = SerializedLambda.convert(func);
+                SerializedLambda lambda = SerializedLambda.resolve(func);
                 FUNC_CACHE.put(clazz, new WeakReference<>(lambda));
                 FUNC_CACHE.put(clazz, new WeakReference<>(lambda));
                 return lambda;
                 return lambda;
             });
             });
@@ -70,7 +73,6 @@ public final class LambdaUtils {
      */
      */
     public static void createCache(Class clazz, TableInfo tableInfo) {
     public static void createCache(Class clazz, TableInfo tableInfo) {
         LAMBDA_CACHE.put(clazz.getName(), createLambdaMap(tableInfo, clazz));
         LAMBDA_CACHE.put(clazz.getName(), createLambdaMap(tableInfo, clazz));
-
     }
     }
 
 
     /**
     /**

+ 57 - 9
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/support/SerializedLambda.java

@@ -16,10 +16,9 @@
  */
  */
 package com.baomidou.mybatisplus.core.toolkit.support;
 package com.baomidou.mybatisplus.core.toolkit.support;
 
 
+import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
 import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
 import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
 import com.baomidou.mybatisplus.core.toolkit.SerializationUtils;
 import com.baomidou.mybatisplus.core.toolkit.SerializationUtils;
-import com.baomidou.mybatisplus.core.toolkit.StringPool;
-import lombok.Getter;
 
 
 import java.io.*;
 import java.io.*;
 
 
@@ -32,10 +31,11 @@ import java.io.*;
  * @author HCL
  * @author HCL
  * @since 2018/05/10
  * @since 2018/05/10
  */
  */
-@Getter
+@SuppressWarnings("unused")
 public class SerializedLambda implements Serializable {
 public class SerializedLambda implements Serializable {
 
 
     private static final long serialVersionUID = 8025925345765570181L;
     private static final long serialVersionUID = 8025925345765570181L;
+
     private Class<?> capturingClass;
     private Class<?> capturingClass;
     private String functionalInterfaceClass;
     private String functionalInterfaceClass;
     private String functionalInterfaceMethodName;
     private String functionalInterfaceMethodName;
@@ -53,9 +53,8 @@ public class SerializedLambda implements Serializable {
      * @param lambda lambda对象
      * @param lambda lambda对象
      * @return 返回解析后的 SerializedLambda
      * @return 返回解析后的 SerializedLambda
      */
      */
-    public static SerializedLambda convert(Property lambda) {
-        byte[] bytes = SerializationUtils.serialize(lambda);
-        try (ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(bytes)) {
+    public static SerializedLambda resolve(Property lambda) {
+        try (ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(SerializationUtils.serialize(lambda))) {
             @Override
             @Override
             protected Class<?> resolveClass(ObjectStreamClass objectStreamClass) throws IOException, ClassNotFoundException {
             protected Class<?> resolveClass(ObjectStreamClass objectStreamClass) throws IOException, ClassNotFoundException {
                 Class<?> clazz = super.resolveClass(objectStreamClass);
                 Class<?> clazz = super.resolveClass(objectStreamClass);
@@ -68,10 +67,59 @@ public class SerializedLambda implements Serializable {
         }
         }
     }
     }
 
 
+    /**
+     * 获取接口 class
+     *
+     * @return 返回 class 名称
+     */
+    public String getFunctionalInterfaceClassName() {
+        return normalName(functionalInterfaceClass);
+    }
+
+    /**
+     * 获取实现的 class
+     *
+     * @return 实现类
+     */
+    public Class getImplClass() {
+        return ClassUtils.toClassConfident(getImplClassName());
+    }
+
+    /**
+     * 获取 class 的名称
+     *
+     * @return 类名
+     */
+    public String getImplClassName() {
+        return normalName(implClass);
+    }
+
+    /**
+     * 获取实现者的方法名称
+     *
+     * @return 方法名称
+     */
+    public String getImplMethodName() {
+        return implMethodName;
+    }
+
+    /**
+     * 正常化类名称,将类名称中的 / 替换为 .
+     *
+     * @param name 名称
+     * @return 正常的类名
+     */
+    private String normalName(String name) {
+        return name.replace('/', '.');
+    }
+
+    /**
+     * @return 字符串形式
+     */
     @Override
     @Override
     public String toString() {
     public String toString() {
-        return super.toString() +
-            implClass.replace(StringPool.SLASH, StringPool.DOT) +
-            StringPool.HASH + implMethodName;
+        return String.format("%s -> %s::%s", getFunctionalInterfaceClassName(), getImplClass().getSimpleName(),
+            implMethodName);
     }
     }
+
 }
 }

+ 1 - 1
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/toolkit/LambdaUtilsTest.java

@@ -11,7 +11,7 @@ public class LambdaUtilsTest {
     @Test
     @Test
     public void testResolve() {
     public void testResolve() {
         SerializedLambda lambda = LambdaUtils.resolve(TestPojo::getId);
         SerializedLambda lambda = LambdaUtils.resolve(TestPojo::getId);
-        Assert.assertEquals(TestPojo.class.getName(), lambda.getImplClass().replace("/", "."));
+        Assert.assertEquals(TestPojo.class.getName(), lambda.getImplClassName());
         Assert.assertEquals("getId", lambda.getImplMethodName());
         Assert.assertEquals("getId", lambda.getImplMethodName());
         Assert.assertEquals("id", StringUtils.resolveFieldName(lambda.getImplMethodName()));
         Assert.assertEquals("id", StringUtils.resolveFieldName(lambda.getImplMethodName()));