Forráskód Böngészése

优化class\xml\includeLib等匹配逻辑支持前缀和正则匹配

(cherry picked from commit b57a50c77a6dd4ec1c44b159f2f4cf9ed2ed257c)
wang 2 éve
szülő
commit
7c31c54bd1

+ 6 - 6
class-winter-core/src/main/java/winter/com/ideaaedi/classwinter/executor/EncryptExecutor.java

@@ -350,12 +350,12 @@ public class EncryptExecutor {
                 })
                 .filter(zipEntryName -> {
                     if (excludeXmlPrefixSet != null) {
-                        boolean shouldExclude = excludeXmlPrefixSet.stream().anyMatch(s -> StrUtil.regMatched(s, zipEntryName));
+                        boolean shouldExclude = excludeXmlPrefixSet.stream().anyMatch(s -> StrUtil.startsWithOrRegMatched(s, zipEntryName));
                         if (shouldExclude) {
                             return false;
                         }
                     }
-                    return includeXmlPrefixSet.stream().anyMatch(s -> StrUtil.regMatched(s, zipEntryName));
+                    return includeXmlPrefixSet.stream().anyMatch(s -> StrUtil.startsWithOrRegMatched(s, zipEntryName));
                 })
                 .collect(Collectors.toSet());
         
@@ -635,7 +635,7 @@ public class EncryptExecutor {
         List<File> allJarFiles = IOUtil.listFileOnly(new File(targetRootDir), Constant.JAR_SUFFIX);
         // 筛选出需要加密的lib包
         List<File> neededEncryptedLibs = allJarFiles.stream().filter(jarFile -> !protectedLibSet.contains(jarFile.getName()) &&
-                includeLibSet.stream().anyMatch(s -> StrUtil.regMatched(s, jarFile.getName()))).collect(Collectors.toList());
+                includeLibSet.stream().anyMatch(s -> StrUtil.startsWithOrRegMatched(s, jarFile.getName()))).collect(Collectors.toList());
         // 依次解密
         neededEncryptedLibs.forEach(jar -> {
             // 假设: jarAbsolutePath为  /xyz/abc.jar
@@ -949,7 +949,7 @@ public class EncryptExecutor {
                     String classLongName = JavassistUtil.resolveClassName(file.getAbsolutePath(), true);
                     if (excludePrefixSet != null) {
                         for (String excludePrefix : excludePrefixSet) {
-                            if (StrUtil.regMatched(excludePrefix, classLongName)) {
+                            if (StrUtil.startsWithOrRegMatched(excludePrefix, classLongName)) {
                                 // 排除
                                 return null;
                             }
@@ -962,7 +962,7 @@ public class EncryptExecutor {
                             String pureIncludePrefix = pair.getLeft();
                             Map<String, String> params = pair.getRight();
 
-                            if (StrUtil.regMatched(pureIncludePrefix, classLongName)) {
+                            if (StrUtil.startsWithOrRegMatched(pureIncludePrefix, classLongName)) {
                                 // 包含
                                 return EncryptClassArgs.create(file,
                                         StrUtil.isTrueDefault(params.get(Constant.CLEAN_CLASS_ANNOTATION_PARAM_NAME), false),
@@ -1294,7 +1294,7 @@ public class EncryptExecutor {
 
             // 如果这次需要加密的lib本身就已经是被加密了的,那么这次不再对其进行加密
             alreadyProtectedLibSet.stream().map(Pair::getLeft).forEach(lib -> {
-                if (includeLibSet.stream().anyMatch(s -> StrUtil.regMatched(s, lib))) {
+                if (includeLibSet.stream().anyMatch(s -> StrUtil.startsWithOrRegMatched(s, lib))) {
                     Logger.warn(EncryptExecutor.class, "Ignore includeLibs item [" + lib + "], because this item already be protected by class-winter.");
                     matchAlreadyProtectedLibSet.add(lib);
                 }

+ 14 - 0
class-winter-core/src/main/java/winter/com/ideaaedi/classwinter/util/StrUtil.java

@@ -192,6 +192,20 @@ public final class StrUtil {
         return bytes;
     }
 
+    /**
+     * 检查字符串是否以指定的前缀或正则表达式匹配。
+     *
+     * @param prefixOrRegex
+     *              前缀或正则表达式字符串
+     * @param text
+     *              待匹配字符串
+     * @return 如果前缀匹配或正则表达式匹配成功,返回 true;否则返回 false。
+     */
+    public static boolean startsWithOrRegMatched(String prefixOrRegex, String text) {
+        return prefixOrRegex.startsWith(text) || regMatched(prefixOrRegex, text);
+    }
+
+
     /**
      * 获取正则表达式匹配次数
      *