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:
java.lang.Exception
and java.lang.RuntimeException
in the result? I used the prefix "org" .java.lang.NullPointerException
not in the result? It is in the package "java.lang" too. The Javadoc for that Reflections
library 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.java
The Javadoc for Reflections.expandSuperTypes()
seems to imply that the prefix
passed to Reflections
is for what needs to be found, not what is scanned which has to include the supertypes in order to find your particular package targets
That's why it's not listing NullPointerException
because it's not scanned by expandSuperTypes()
explained in #2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.