浏览代码

内置泛型提取,支持非Spring体系项目使用.

nieqiurong 1 年之前
父节点
当前提交
741320dc3c

+ 7 - 3
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/ReflectionKit.java

@@ -16,6 +16,7 @@
 package com.baomidou.mybatisplus.core.toolkit;
 
 import com.baomidou.mybatisplus.core.toolkit.reflect.GenericTypeUtils;
+import com.baomidou.mybatisplus.core.toolkit.reflect.TypeParameterResolver;
 
 import java.lang.reflect.AccessibleObject;
 import java.lang.reflect.Field;
@@ -93,9 +94,12 @@ public final class ReflectionKit {
      * @return Class
      */
     public static Class<?> getSuperClassGenericType(final Class<?> clazz, final Class<?> genericIfc, final int index) {
-        //update by noear @2021-09-03
-        Class<?>[] typeArguments = GenericTypeUtils.resolveTypeArguments(ClassUtils.getUserClass(clazz), genericIfc);
-        return null == typeArguments ? null : typeArguments[index];
+        // 这里泛型逻辑提取进行了调整,如果在Spring项目情况或者自定义了泛型提取,那就优先走这里,否则使用框架内置的进行泛型提取.
+        if (GenericTypeUtils.hasGenericTypeResolver()) {
+            Class<?>[] typeArguments = GenericTypeUtils.resolveTypeArguments(ClassUtils.getUserClass(clazz), genericIfc);
+            return null == typeArguments ? null : typeArguments[index];
+        }
+        return (Class<?>) TypeParameterResolver.resolveClassIndexedParameter(clazz, genericIfc, index);
     }
 
     /**

+ 28 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/reflect/GenericTypeUtils.java

@@ -15,6 +15,7 @@
  */
 package com.baomidou.mybatisplus.core.toolkit.reflect;
 
+import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
 /**
  * 泛型类工具(用于隔离Spring的代码)
  *
@@ -23,6 +24,22 @@ package com.baomidou.mybatisplus.core.toolkit.reflect;
  * @since 2021-09-03
  */
 public class GenericTypeUtils {
+
+    /**
+     * 能否加载SpringCore包
+     *
+     * @since 3.5.4
+     */
+    private static boolean loadSpringCore = false;
+
+    static {
+        try {
+            ClassUtils.toClassConfident("org.springframework.core.GenericTypeResolver");
+            loadSpringCore = true;
+        } catch (Exception exception) {
+            // ignore
+        }
+    }
     private static IGenericTypeResolver GENERIC_TYPE_RESOLVER;
 
     /**
@@ -42,4 +59,15 @@ public class GenericTypeUtils {
     public static void setGenericTypeResolver(IGenericTypeResolver genericTypeResolver) {
         GENERIC_TYPE_RESOLVER = genericTypeResolver;
     }
+
+    /**
+     * 判断是否有自定泛型提取类或能否加载SpringCore进行泛型提取
+     *
+     * @return 是否有实现
+     * @since 3.5.4
+     */
+    public static boolean hasGenericTypeResolver() {
+        return GENERIC_TYPE_RESOLVER != null || loadSpringCore;
+    }
+
 }

+ 15 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/reflect/TypeParameterResolver.java

@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2011-2023, baomidou (jobob@qq.com).
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package com.baomidou.mybatisplus.core.toolkit.reflect;
 
 import java.lang.reflect.GenericDeclaration;