通过Ant-style风格路径生成graalVM的reflection-config.config文件

如下

java 复制代码
import org.springframework.util.AntPathMatcher;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * generate reflection-config.json for native-image
 * @author cjbi
 */
public class ReflectionConfigGenerator {

  public static void main(String[] args) throws Exception {
    String[] patterns = {
      "tech.wetech.metacode.application.dto.*",
      "tech.wetech.metacode.domain.**.model.*",
      "tech.wetech.metacode.domain.**.event.*",
      "tech.wetech.metacode.infrastructure.jackson.JacksonUtils",
      "tech.wetech.metacode.application.dto.SessionDataDTO",
      "tech.wetech.metacode.infrastructure.jackson.BaseEntitySerializer",
      "tech.wetech.metacode.infrastructure.jsonlogic.JsonLogicUtils",
      "tech.wetech.metacode.infrastructure.pinyin4j.Pinyin4jUtil",
      "tech.wetech.metacode.infrastructure.cache.NotABeanKeyGen"
    };
    ReflectionConfigGenerator generator = new ReflectionConfigGenerator();
    generator.generateAndPrintConsole(patterns);

  }

  private void generateAndPrintConsole(String... patterns) throws Exception {
    List<File> classsFileList = getClasssFileList();
    AntPathMatcher antPathMatcher = new AntPathMatcher();
    StringBuilder sb = new StringBuilder("[");
    for (File file : classsFileList) {
      String classPath = ReflectionConfigGenerator.class.getClassLoader()
        .getResource("").getFile()
        .replaceFirst("/", "")
        .replace("/", "\\");
      String className = file.getPath().replace(classPath, "").replace("\\", ".").replaceAll(".class", "");
      for (String pattern : patterns) {
        if (antPathMatcher.match(pattern, className)) {
          sb.append(String.format("""
            {
              "name" : "%s",
              "allDeclaredConstructors" : true,
              "allPublicConstructors" : true,
              "allDeclaredMethods" : true,
              "allPublicMethods" : true,
              "allDeclaredFields" : true,
              "allPublicFields" : true
            },""", className));
        }
      }
    }
    sb.deleteCharAt(sb.length() - 1);
    sb.append("]");
    System.out.println(sb);
  }

  private List<File> getClasssFileList() throws URISyntaxException, IOException {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    Enumeration<URL> urls = classLoader.getResources("");
    List<File> fileList = new ArrayList<>();
    while (urls.hasMoreElements()) {
      URL url = urls.nextElement();
      if (url.getProtocol().equals("file")) {
        File file = new File(url.toURI());
        listFiles(file, fileList);
      }
    }
    return fileList;
  }


  private void listFiles(File rootFile, List<File> classFiles) {
    File[] files = rootFile.listFiles();
    assert files != null;
    for (File file : files) {
      if (file.isFile() && file.getName().endsWith(".class")) {
        classFiles.add(file);
      } else if (file.isDirectory()) {
        listFiles(file, classFiles);
      }
    }
  }
}
相关推荐
雪球工程师团队7 分钟前
代码“蝴蝶效应”终结者:AI Review + AST 联展,构建智能测试防御新体系
java·ai编程·测试
你喜欢喝可乐吗?14 分钟前
RuoYi-Cloud ruoyi-gateway 网关模块
java·spring cloud·gateway
典孝赢麻崩乐急35 分钟前
Java学习---JVM(1)
java·jvm·学习
m0_5973453144 分钟前
【Android】安卓四大组件之广播接收器(Broadcast Receiver):从基础到进阶
android·java·boradcast·安卓四大组件
程序员的世界你不懂1 小时前
基于Java+Maven+Testng+Selenium+Log4j+Allure+Jenkins搭建一个WebUI自动化框架(5)失败用例截图与重试
java·selenium·maven
喧星Aries1 小时前
进程调度的时机,切换与过程方式(操作系统OS)
java·服务器·前端·操作系统·进程调度
JouJz1 小时前
Spring事务管理深度解析:原理、实践与陷阱
java·spring
此乃大忽悠1 小时前
身份认证缺陷
java·数据库·webgoat·身份认证缺陷
Honyee1 小时前
java使用UCanAccess操作Access
java·后端