使用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而对象只是Java.lang

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

 Reflections reflections = new Reflections("newPackaes");

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

10-07 21:27