hubin 6 лет назад
Родитель
Сommit
ae8048e66b

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

@@ -32,6 +32,8 @@ import java.util.List;
  */
 public final class ClassUtils {
 
+    private static final char PACKAGE_SEPARATOR = '.';
+
     /**
      * 代理 class 的名称
      */
@@ -127,4 +129,31 @@ public final class ClassUtils {
         }
     }
 
+
+    /**
+     * Determine the name of the package of the given class,
+     * e.g. "java.lang" for the {@code java.lang.String} class.
+     *
+     * @param clazz the class
+     * @return the package name, or the empty String if the class
+     * is defined in the default package
+     */
+    public static String getPackageName(Class<?> clazz) {
+        Assert.notNull(clazz, "Class must not be null");
+        return getPackageName(clazz.getName());
+    }
+
+    /**
+     * Determine the name of the package of the given fully-qualified class name,
+     * e.g. "java.lang" for the {@code java.lang.String} class name.
+     *
+     * @param fqClassName the fully-qualified class name
+     * @return the package name, or the empty String if the class
+     * is defined in the default package
+     */
+    public static String getPackageName(String fqClassName) {
+        Assert.notNull(fqClassName, "Class name must not be null");
+        int lastDotIndex = fqClassName.lastIndexOf(PACKAGE_SEPARATOR);
+        return (lastDotIndex != -1 ? fqClassName.substring(0, lastDotIndex) : "");
+    }
 }

+ 7 - 6
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/PackageHelper.java

@@ -15,7 +15,10 @@
  */
 package com.baomidou.mybatisplus.extension.toolkit;
 
-import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
+import java.io.File;
+import java.util.HashSet;
+import java.util.Set;
+
 import org.springframework.core.io.Resource;
 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
 import org.springframework.core.io.support.ResourcePatternResolver;
@@ -24,9 +27,7 @@ import org.springframework.core.type.classreading.MetadataReader;
 import org.springframework.core.type.classreading.MetadataReaderFactory;
 import org.springframework.util.ClassUtils;
 
-import java.io.File;
-import java.util.HashSet;
-import java.util.Set;
+import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
 
 /**
  * 包扫描辅助类
@@ -62,12 +63,12 @@ public class PackageHelper {
                 for (Resource resource : resources) {
                     if (resource.isReadable()) {
                         metadataReader = metadataReaderFactory.getMetadataReader(resource);
-                        set.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage().getName());
+                        set.add(ClassUtils.getPackageName(metadataReader.getClassMetadata().getClassName()));
                     }
                 }
             }
             if (!set.isEmpty()) {
-                return set.toArray(new String[] {});
+                return set.toArray(new String[]{});
             }
             return new String[0];
         } catch (Exception e) {