自动生成Java枚举类的工具

发布时间:2023年12月28日

0、展示效果

在这里插入图片描述

1、文档

在这里插入图片描述

ONE,code1,1,描述1
two,code2,2,描述2
THREE,code3,3,描述3

2、工具类

package com.lfsun.generator.util;

import java.io.*;

/**
 * 枚举类生成器
 */
public class EnumGenerator {

    private static final String MODULE_NAME = "lfsun-study-generator"; // 替换为实际的模块名
    private static final String PACKAGE_NAME = "com.lfsun.generator.enums"; // 替换为实际的包名
    private static final String ENUM_NAME = "MyCustomEnum"; // 替换为实际的类名
    private static final String FILE_PATH = "document.txt"; // 替换为实际文档路径 当前为 resources目录下

    private static final String[] FIELD_NAMES = {"code", "seq", "description"}; // 替换为实际的 字段名

    public static void main(String[] args) {
        try (BufferedReader br = getResourceBufferedReader(FILE_PATH);
             BufferedWriter writer = createJavaFileWriter(PACKAGE_NAME, ENUM_NAME)) {

            // 输出枚举类的头部
            writer.write("package " + PACKAGE_NAME + ";\n\n");
            writer.write("import lombok.AllArgsConstructor;\n");
            writer.write("import lombok.Getter;\n\n");
            writer.write("@Getter\n");
            writer.write("@AllArgsConstructor\n");
            writer.write("public enum " + ENUM_NAME + " {\n");

            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split(",");
                generateEnumValue(parts, writer);
            }

            // 输出枚举类的结尾
            writer.write("    ;\n"); // 注意这里加上分号
            writer.write("\n");

            // 枚举值定义
            for (String fieldName : FIELD_NAMES) {
                writer.write("    private final String " + fieldName + ";\n");
            }

            writer.write("}\n");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void generateEnumValue(String[] parts, BufferedWriter writer) throws IOException {
        if (parts.length >= FIELD_NAMES.length) {
            // 输出枚举值的定义
            writer.write("    " + parts[0].toUpperCase() +
                    "(\"" + parts[1] + "\", \"" + parts[2] + "\", \"" + parts[3] +
                    "\"),\n");
        } else {
            System.err.println("Invalid line format: " + String.join(",", parts));
        }
    }

    private static BufferedReader getResourceBufferedReader(String resourcePath) throws IOException {
        // 使用ClassLoader获取资源的输入流
        InputStream inputStream = EnumGenerator.class.getClassLoader().getResourceAsStream(resourcePath);

        if (inputStream != null) {
            return new BufferedReader(new InputStreamReader(inputStream));
        } else {
            throw new IOException("Resource not found: " + resourcePath);
        }
    }

    private static BufferedWriter createJavaFileWriter(String packageName, String enumName) throws IOException {
        String packagePath = packageName.replace('.', '/');
        String filePath = MODULE_NAME + "/src/main/java/" + packagePath + "/" + enumName + ".java";
        File file = new File(filePath);

        // 创建文件及其父目录
        file.getParentFile().mkdirs();

        FileWriter fileWriter = new FileWriter(file);
        return new BufferedWriter(fileWriter);
    }
}

文章来源:https://blog.csdn.net/qq_43116031/article/details/135257233
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。