通过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);
      }
    }
  }
}
相关推荐
浩宇软件开发1 分钟前
Android开发,实现一个简约又好看的登录页
android·java·android studio·android开发
南客先生8 分钟前
多级缓存架构设计与实践经验
java·面试·多级缓存·缓存架构
anqi2711 分钟前
如何在 IntelliJ IDEA 中编写 Speak 程序
java·大数据·开发语言·spark·intellij-idea
m0_7401546717 分钟前
maven相关概念深入介绍
java·maven
fanTuanye30 分钟前
Spring-全面详解(学习总结)
java·spring·ssm框架
Best_Liu~31 分钟前
TransactionTemplate 与@Transactional 注解的使用
java·开发语言·spring boot·后端
胡斌附体1 小时前
idea启动springboot方式及web调用
java·spring boot·intellij-idea
她和夏天一样热1 小时前
【Java面试题04】MySQL 篇
java·mysql·adb
曹天骄1 小时前
设计并实现一个基于 Java + Spring Boot + MySQL 的通用多租户权限系统
java·spring boot·mysql