I want to find all sub classes of java.lang.Throwable in the JVM. The classes may or may not have been already loaded in the JVM.
I have read the similar question How do you find all subclasses of a given class in Java?, and I think org.reflections can solve my question.
I tried the following code:
public static void main(String[] args) {
    Reflections reflections = new Reflections( "org" ) ;
    Set<Class<? extends Throwable>> set = reflections.getSubTypesOf( Throwable.class ) ;
    for (Class<? extends Throwable> t : set) {
        System.out.println( t ) ;
    }
}
But the result is not what I expected:
class java.lang.Exception
class java.lang.RuntimeException
class org.reflections.ReflectionsException
I have two doubts:
- Why are java.lang.Exceptionandjava.lang.RuntimeExceptionin the result? I used the prefix "org".
- Why is java.lang.NullPointerExceptionnot in the result? It is in the package "java.lang" too.
 
                        
The Javadoc for that
Reflectionslibrary says "Reflections scans and indexes your project's classpath" so it's not looking "in the JVM", it's looking for files in your classpath and loading them with the Class Loader https://github.com/ronmamo/reflections/blob/master/src/main/java/org/reflections/Reflections.javaThe Javadoc for
Reflections.expandSuperTypes()seems to imply that theprefixpassed toReflectionsis for what needs to be found, not what is scanned which has to include the supertypes in order to find your particular package targetsThat's why it's not listing
NullPointerExceptionbecause it's not scanned byexpandSuperTypes()explained in #2