使用 Google Reflections 获取所有类的列表——但 java.* 似乎丢失了

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14314789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:51:11  来源:igfitidea点击:

Using Google Reflections to get a list of all classes -- but java.* seems to be missing

javaclassgoogle-reflections

提问by pinecone

I am using the google Reflections package to build an index of all classes that are available for calling. The following code is supposed to return all classes that are loaded in the JVM:

我正在使用 google Reflections 包来构建可供调用的所有类的索引。以下代码应该返回在 JVM 中加载的所有类:

List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
classLoadersList.add(ClasspathHelper.contextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader());                      
Reflections reflections = new Reflections(new ConfigurationBuilder()
         .setScanners(new SubTypesScanner(false), new ResourcesScanner())
         .setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0]))));
Set<Class<? extends Object>> allClasses = 
         reflections.getSubTypesOf(Object.class);

I note that the set it returns does not contain anything in the java.* domain. Can someone familiar with the Reflections package advise me on how to get these as well? Thanks!

我注意到它返回的集合在 java.* 域中不包含任何内容。熟悉 Reflections 包的人能否也告诉我如何获得这些?谢谢!

采纳答案by artbristol

Not all classes are loaded by a normal classloader; some are loaded by the bootstrap classloader to speed things up, and this can be coded natively (and hence inaccessible from Java code). See this message:

并非所有类都由普通类加载器加载;有些是由引导类加载器加载以加快速度,这可以本地编码(因此无法从 Java 代码访问)。看到这条消息:

http://lists.jboss.org/pipermail/jboss-development/2008-April/011943.html

http://lists.jboss.org/pipermail/jboss-development/2008-April/011943.html

See this question for alternatives

请参阅此问题以获取替代方案

Java - Get a list of all Classes loaded in the JVM

Java - 获取 JVM 中加载的所有类的列表

回答by zapp

Google Reflections can be used to get all classes, including java.*, although it's not its primarily use.

Google Reflections 可用于获取所有类,包括 java.*,尽管它不是主要用途。

Reflections reflections = new Reflections(
    ClasspathHelper.forClass(Object.class), 
    new SubTypesScanner(false));

And than:

然后:

Set<String> allClasses = 
    reflections.getStore().getSubTypesOf(Object.class.getName());