gpt4 book ai didi

java - 递归列出包中定义的所有枚举

转载 作者:行者123 更新时间:2023-11-30 01:42:27 26 4
gpt4 key购买 nike

使用org.reflections API发现它不适用于Enums:

List<ClassLoader> classLoadersList = new LinkedList<>();
classLoadersList.add(ClasspathHelper.contextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader());
Reflections reflections = new Reflections(new org.reflections.util.ConfigurationBuilder()
.setScanners(new SubTypesScanner(false), new ResourcesScanner())
.setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0])))
.filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("my.package"))));

Set<Class<? extends Object>> allClasses = reflections.getSubTypesOf(Enum.class);

// Above line should've returned all enums recursively, doesn't work!

还有其他方法吗?

最佳答案

您不需要那么多仪式代码,只需尝试以下代码片段即可。

Enums 扩展了 java.lang.Enum 而 Object 只是 Java.lang

修改后的代码才能正常工作应该是:

 Reflections reflections = new Reflections("newPackaes");

Set<Class<? extends Enum>> allClasses =
reflections.getSubTypesOf(Enum.class);
System.out.println(allClasses);

关于java - 递归列出包中定义的所有枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59486326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com