通过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);
      }
    }
  }
}
相关推荐
重庆小透明26 分钟前
力扣刷题记录【1】146.LRU缓存
java·后端·学习·算法·leetcode·缓存
lang2015092831 分钟前
Reactor操作符的共享与复用
java
TTc_42 分钟前
@Transactional事务注解的批量回滚机制
java·事务
wei_shuo2 小时前
飞算 JavaAI 开发助手:深度学习驱动下的 Java 全链路智能开发新范式
java·开发语言·飞算javaai
欧阳秦穆2 小时前
apoc-5.24.0-extended.jar 和 apoc-4.4.0.36-all.jar 啥区别
java·jar
岁忧2 小时前
(LeetCode 面试经典 150 题 ) 58. 最后一个单词的长度 (字符串)
java·c++·算法·leetcode·面试·go
Java初学者小白2 小时前
秋招Day14 - Redis - 应用
java·数据库·redis·缓存
代码老y2 小时前
Spring Boot + 本地部署大模型实现:优化与性能提升
java·spring boot·后端
GodKeyNet2 小时前
设计模式-桥接模式
java·设计模式·桥接模式
guojl3 小时前
Java多任务编排技术
java