简体   繁体   English

如何在JVM中找到java.lang.Throwable的所有子类?

[英]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.我想在java.lang.Throwable中找到 java.lang.Throwable 的所有子类。这些类可能已经加载也可能没有加载到 JVM 中。

I have read the similar question How do you find all subclasses of a given class in Java?我读过类似的问题How do you find all subclasses of a given class in Java? , and I think org.reflections can solve my question. ,我认为org.reflections可以解决我的问题。

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?为什么结果是java.lang.Exceptionjava.lang.RuntimeException I used the prefix "org" .我使用前缀"org"
  2. Why is java.lang.NullPointerException not in the result?为什么java.lang.NullPointerException不在结果中? It is in the package "java.lang" too.它也在 package“java.lang”中。
  1. 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.javaReflections库的 Javadoc 说“Reflections 扫描并索引项目的类路径”,因此它不是在“JVM 中”查找,而是在类路径中查找文件并使用 Class Loader https://github.com/ronmamo/加载它们反射/blob/master/src/main/java/org/reflections/Reflections.java

  2. 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 Reflections.expandSuperTypes()的 Javadoc 似乎暗示传递给Reflectionsprefix是为了需要找到的东西,而不是扫描的东西,它必须包含超类型才能找到您的特定 package 目标

  3. That's why it's not listing NullPointerException because it's not scanned by expandSuperTypes() explained in #2这就是它没有列出NullPointerException的原因,因为它没有被 #2 中解释的expandSuperTypes()扫描

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM