diff --git a/src/main/java/org/redkale/boot/ClassFilter.java b/src/main/java/org/redkale/boot/ClassFilter.java index d8e9bdb0f..3be9abf13 100644 --- a/src/main/java/org/redkale/boot/ClassFilter.java +++ b/src/main/java/org/redkale/boot/ClassFilter.java @@ -33,6 +33,10 @@ public final class ClassFilter { private static final Logger logger = Logger.getLogger(ClassFilter.class.getName()); // 日志对象 + private static final String dian = new String(new char[] {7}); // . + private static final String dxing = new String(new char[] {8}); // * + private static final String sxing = new String(new char[] {9}); // ** + private final Set> entrys = new HashSet<>(); // 符合条件的结果 private final Set> expectEntrys = new HashSet<>(); // 准备符合条件的结果 @@ -389,7 +393,7 @@ public final class ClassFilter { if (regx == null || regx.trim().isEmpty()) { continue; } - rs[i++] = Pattern.compile(format(regx.trim())); + rs[i++] = Pattern.compile(formatPackageRegx(regx.trim())); } if (i == 0) { return null; @@ -402,18 +406,20 @@ public final class ClassFilter { return ps; } - // 将简化版正则转成标准的正则表达式 - // *.platf.* 转成 ^.*\.platf\..*$ - // .platf. 转成 ^.*\.platf\..*$ - private static String format(String regx) { - String str = regx.replace('.', (char) 8); - if (regx.endsWith("*") || regx.endsWith(".")) { - str = str.substring(0, str.length() - 1) + ".*$"; + /** + * 将简化版正则转成标准的正则表达式
+ * *.platf.** 转成 ^(\w+)\.platf\.(.*)$ + * + * @param regx + * @return Pattern + */ + public static String formatPackageRegx(String regx) { + if (regx.indexOf('^') >= 0 || regx.indexOf('$') >= 0 || regx.indexOf('\\') >= 0) { // 已经是标准正则表达式 + return regx; } - if (regx.startsWith("*") || regx.startsWith(".")) { - str = "^.*" + str.substring(1); - } - return str.replace(new String(new char[] {8}), "\\."); + String str = regx.replace("**", sxing).replace("*", dxing); + str = str.replace("\\.", dian).replace(".", dian); + return "^" + str.replace(dian, "\\.").replace(dxing, "(\\w+)").replace(sxing, "(.*)") + "$"; } public void setSuperClass(Class superClass) { diff --git a/src/test/java/org/redkale/test/util/ClassFilterTest.java b/src/test/java/org/redkale/test/util/ClassFilterTest.java new file mode 100644 index 000000000..f9ca98e49 --- /dev/null +++ b/src/test/java/org/redkale/test/util/ClassFilterTest.java @@ -0,0 +1,40 @@ +/* + +*/ + +package org.redkale.test.util; + +import org.junit.jupiter.api.*; +import org.redkale.boot.ClassFilter; + +/** + * + * @author zhangjx + */ +public class ClassFilterTest { + + public static void main(String[] args) throws Throwable { + ClassFilterTest test = new ClassFilterTest(); + test.run1(); + test.run2(); + test.run3(); + } + + @Test + public void run1() { + String regx = ClassFilter.formatPackageRegx("*.platf.**"); + Assertions.assertEquals("^(\\w+)\\.platf\\.(.*)$", regx); + } + + @Test + public void run2() { + String regx = ClassFilter.formatPackageRegx("com.platf.**.api"); + Assertions.assertEquals("^com\\.platf\\.(.*)\\.api$", regx); + } + + @Test + public void run3() { + String regx = ClassFilter.formatPackageRegx("**.platf.api"); + Assertions.assertEquals("^(.*)\\.platf\\.api$", regx); + } +}