通过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);
      }
    }
  }
}
相关推荐
学长毕业设计44 分钟前
基于SpringBoot的公益基金管理系统(源码+文档+讲解视频)
java·spring boot·后端
东小西1 小时前
【SAA实战】第 3 篇 · 工具调用全攻略:把业务能力交给 Agent 自己调度
java·后端·spring
东小西1 小时前
【SAA实战】第 4 篇 · Agent 短期记忆:saver 让 Agent 跨轮记得住(threadId 隔离)
java·后端·spring
许彰午1 小时前
22-DataCenter报文序列化
java·低代码·架构·状态模式
2601_962065251 小时前
[MySQL] SQL优化之性能分析
java·sql·mysql
小范同学_2 小时前
JDK1.7 与 JDK1.8 HashMap 底层原理对比 + 数组并发扩容死循环详解
java·开发语言
予昊3 小时前
从零实现“在线五子棋对战“:WebSocket 实时通信 + 段位匹配
java·开发语言·网络·websocket
2601_962203513 小时前
【SpringAI入门】初识SpringAI
java
weixin_461408583 小时前
Mybatis-flex小记
java·开发语言·mybatis
2601_962177133 小时前
【MySQL篇】聚合查询,联合查询
android·java·mysql