通过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);
      }
    }
  }
}
相关推荐
AskHarries3 分钟前
深入探索Java虚拟机的神秘接口:JVMTI
java·jvm
风象南5 分钟前
SpringBoot应用开机自启动与进程守护配置
java·spring boot·后端
字节卷动16 分钟前
【牛客刷题】活动安排
java·算法·牛客
fouryears_2341723 分钟前
Spring核心原理的快速入门:快速了解IoC与DI
java·后端·spring
顽疲34 分钟前
从零用java实现 小红书 springboot vue uniapp(13)模仿抖音视频切换
java·vue.js·spring boot
星辰离彬1 小时前
Java 与 MySQL 性能优化:MySQL连接池参数优化与性能提升
java·服务器·数据库·后端·mysql·性能优化
半桔1 小时前
【Linux手册】从接口到管理:Linux文件系统的核心操作指南
android·java·linux·开发语言·面试·系统架构
nightunderblackcat1 小时前
新手向:实现ATM模拟系统
java·开发语言·spring boot·spring cloud·tomcat·maven·intellij-idea
Bug退退退1231 小时前
RabbitMQ 高级特性之延迟队列
java·spring·rabbitmq·java-rabbitmq