mercredi 28 décembre 2022

How to find all sub classes of java.lang.Throwable in the JVM?

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:

  1. Why are java.lang.Exception and java.lang.RuntimeException in the result? I used the prefix "org".
  2. Why is java.lang.NullPointerException not in the result? It is in the package "java.lang" too.




Aucun commentaire:

Enregistrer un commentaire