有什么方法可以从Intellij中的项目中获取所有枚举类及其各自的价值观?

发布于 2025-02-05 02:31:02 字数 99 浏览 1 评论 0原文

我需要创建一个在Intellij中的项目中声明为枚举的类的列表,在每个项目中,每个类别都列出了相应的值。是否有任何自动化方法可以完成此任务,因为它是一个相当大的项目,发生了数百个事件。

I need to create a list of classes declared as enum in a project in IntelliJ and, inside each one, list the respective values. Is there any automated way to accomplish this task, as it is a rather large project, with hundreds of occurrences.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同尘 2025-02-12 02:31:03

是的,您可以使用类型的层次结构视图来执行此操作,并将条目导出到文件。我正在使用Eclipse快捷方式,可以使用f4打开类型层次结构。只需确保过滤生产文件即可。

示例使用guava项目:

“项目中的所有枚举”

对于获取每个枚举的所有值,我认为Intellij中没有这样的功能,但是您可能可以编写一个脚本来处理导出的文件。

编辑

此方法将使用Intellij导出的文件打印枚举值。您可能需要修改它以处理错误,等等。

    private static void printEnumClassesAndValues(String file) throws IOException {
        Files.lines(Path.of(file))
                .filter(line -> !line.contains("java.lang"))
                .map(line -> {
                    String[] tokens = line.replaceAll("[\\(\\),]", "").trim().split(" ");
                    return String.format("%s.%s", tokens[1], tokens[0]); // package + class name
                })
                .forEach(enumClass -> System.out.printf("%s: %s%n", enumClass, Arrays.toString(getEnumValues(enumClass))));
    }

    private static Enum<?>[] getEnumValues(String enumClass) {
        try {
            Method m = Class.forName(enumClass).getDeclaredMethod("values");
            return (Enum<?>[]) m.invoke(null);
        } catch (Exception ex) {
            throw new RuntimeException(ex); // log or handle otherwise
        }
    }

Yes, you can do this using the type hierarchy view and export the entries to a file. I'm using eclipse shortcuts and I can open the type hierarchy using F4. Just make sure to filter Production files.

Example using the Guava project:

All enums within the project

As for getting all the values for each enum, I don't think there's such a feature in IntelliJ, but you could probably write a script to process the exported file.

Edit

This method will print enum values using the file exported from IntelliJ. You might need to modify it to handle errors, etc.

    private static void printEnumClassesAndValues(String file) throws IOException {
        Files.lines(Path.of(file))
                .filter(line -> !line.contains("java.lang"))
                .map(line -> {
                    String[] tokens = line.replaceAll("[\\(\\),]", "").trim().split(" ");
                    return String.format("%s.%s", tokens[1], tokens[0]); // package + class name
                })
                .forEach(enumClass -> System.out.printf("%s: %s%n", enumClass, Arrays.toString(getEnumValues(enumClass))));
    }

    private static Enum<?>[] getEnumValues(String enumClass) {
        try {
            Method m = Class.forName(enumClass).getDeclaredMethod("values");
            return (Enum<?>[]) m.invoke(null);
        } catch (Exception ex) {
            throw new RuntimeException(ex); // log or handle otherwise
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文