Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 218
pinecone Avatar asked Jan 14 '13 07:01

pinecone


People also ask

How to list methods of a class using Java reflection?

List methods of a class using Java Reflection. The methods of a class can be listed using the java.lang.Class.getDeclaredMethods () method. This method returns an array that contains all the Method objects with public, private, protected and default access. However, the inherited methods are not included.

How to find classes in a package in Java?

Hence, finding classes in a package is essentially a file system operation rather than one done by using Java Reflection. However, we can write our own class loaders or examine the classpath to find classes inside a package. 3. Finding Classes in a Java Package

What is the Google Guava Classpath utility class?

Google Guava provides a ClassPath utility class that scans the source of the class loader and finds all loadable classes and resources. First, let's add the guava dependency to our project:

How to find classes in a package using a classloader?

Class loaders are dynamic. They are not required to tell the JVM which classes it can provide at runtime. Hence, finding classes in a package is essentially a file system operation rather than one done by using Java Reflection. However, we can write our own class loaders or examine the classpath to find classes inside a package.


2 Answers

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());
like image 72
zapp Avatar answered Sep 17 '22 20:09

zapp


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

like image 30
artbristol Avatar answered Sep 19 '22 20:09

artbristol