Kaynağa Gözat

调整刘西东同学的 PR

hubin 3 yıl önce
ebeveyn
işleme
24b889adb3

+ 0 - 11
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/GenericTypeHelper.java

@@ -1,11 +0,0 @@
-package com.baomidou.mybatisplus.core.toolkit;
-
-/**
- * 泛型类助手(用于隔离Spring的代码)
- *
- * @author noear
- * @since 2021-09-03
- */
-public interface GenericTypeHelper {
-    Class<?>[] resolveTypeArguments(final Class<?> clazz, final Class<?> genericIfc);
-}

+ 0 - 16
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/GenericTypeHelperDefault.java

@@ -1,16 +0,0 @@
-package com.baomidou.mybatisplus.core.toolkit;
-
-import org.springframework.core.GenericTypeResolver;
-
-/**
- * 泛型类助手默认实现
- *
- * @author noear
- * @since 2021-09-03
- */
-public class GenericTypeHelperDefault implements GenericTypeHelper{
-    @Override
-    public Class<?>[] resolveTypeArguments(Class<?> clazz, Class<?> genericIfc) {
-        return GenericTypeResolver.resolveTypeArguments(clazz, genericIfc);
-    }
-}

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

@@ -1,29 +0,0 @@
-package com.baomidou.mybatisplus.core.toolkit;
-
-/**
- * 泛型类工具(用于隔离Spring的代码)
- *
- * @author noear
- * @since 2021-09-03
- */
-public class GenericTypeUtils {
-    private static GenericTypeHelper genericTypeHelper;
-
-    /**
-     * 获取泛型工具助手
-     * */
-    public static GenericTypeHelper getGenericTypeHelper() {
-        if(genericTypeHelper == null){
-            genericTypeHelper = new GenericTypeHelperDefault();
-        }
-
-        return genericTypeHelper;
-    }
-
-    /**
-     * 设置泛型工具助手。如果不想使用Spring封装,可以使用前替换掉
-     * */
-    public static void setGenericTypeHelper(GenericTypeHelper genericTypeHelper) {
-        GenericTypeUtils.genericTypeHelper = genericTypeHelper;
-    }
-}

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

@@ -15,6 +15,8 @@
  */
 package com.baomidou.mybatisplus.core.toolkit;
 
+import com.baomidou.mybatisplus.core.toolkit.reflect.GenericTypeUtils;
+
 import java.lang.reflect.AccessibleObject;
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
@@ -90,7 +92,7 @@ public final class ReflectionKit {
      */
     public static Class<?> getSuperClassGenericType(final Class<?> clazz, final Class<?> genericIfc, final int index) {
         //update by noear @2021-09-03
-        Class<?>[] typeArguments = GenericTypeUtils.getGenericTypeHelper().resolveTypeArguments(ClassUtils.getUserClass(clazz), genericIfc);
+        Class<?>[] typeArguments = GenericTypeUtils.getGenericTypeResolver().resolveTypeArguments(ClassUtils.getUserClass(clazz), genericIfc);
         return null == typeArguments ? null : typeArguments[index];
     }
 

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

@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2011-2021, 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;
+
+/**
+ * 泛型类工具(用于隔离Spring的代码)
+ *
+ * @author noear hubin
+ * @since 2021-09-03
+ */
+public class GenericTypeUtils {
+    private static IGenericTypeResolver GENERIC_TYPE_RESOLVER;
+
+    /**
+     * 获取泛型工具助手
+     */
+    public static IGenericTypeResolver getGenericTypeResolver() {
+        if (null == GENERIC_TYPE_RESOLVER) {
+            GENERIC_TYPE_RESOLVER = new SpringGenericTypeResolver();
+        }
+        return GENERIC_TYPE_RESOLVER;
+    }
+
+    /**
+     * 设置泛型工具助手。如果不想使用Spring封装,可以使用前替换掉
+     */
+    public static void setGenericTypeHelper(IGenericTypeResolver genericTypeResolver) {
+        GENERIC_TYPE_RESOLVER = genericTypeResolver;
+    }
+}

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

@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2011-2021, 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;
+
+/**
+ * 泛型类助手(用于隔离Spring的代码)
+ *
+ * @author noear hubin
+ * @since 2021-09-03
+ */
+public interface IGenericTypeResolver {
+
+    Class<?>[] resolveTypeArguments(final Class<?> clazz, final Class<?> genericIfc);
+}

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

@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2011-2021, 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 org.springframework.core.GenericTypeResolver;
+
+/**
+ * 泛型类助手默认 spring 实现
+ *
+ * @author noear hubin
+ * @since 2021-09-03
+ */
+public class SpringGenericTypeResolver implements IGenericTypeResolver {
+
+    @Override
+    public Class<?>[] resolveTypeArguments(Class<?> clazz, Class<?> genericIfc) {
+        return GenericTypeResolver.resolveTypeArguments(clazz, genericIfc);
+    }
+}