pinecone
pinecone

Reputation: 81

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

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:

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!

Upvotes: 7

Views: 10741

Answers (2)

zapp
zapp

Reputation: 1553

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

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

And than:

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

Upvotes: 7

artbristol
artbristol

Reputation: 32407

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:

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

Upvotes: 2

Related Questions