简体   繁体   English

如何以编程方式查找继承了 Java 中特定 class 的类的列表

[英]How to programmatically find list of classes that have inherited a particular class in Java

I have a class called MyClass.我有一个名为 MyClass 的 class。 Is it possible to programmatically find a list of classes that have inherited MyClass?是否可以以编程方式查找继承了 MyClass 的类的列表? I know we can use reflection to discover all the superclasses of a given class, but can we find the subclasses?我知道我们可以使用反射来发现给定 class 的所有超类,但是我们能找到子类吗?

Is it even theoretically possible to do so (as I haven't heard of a class being aware of when it is being extended)?理论上是否有可能这样做(因为我还没有听说过 class 知道它何时被扩展)?

You might want to look at the reflections library您可能想查看反射


Using Reflections you can query your metadata such as:使用反射,您可以查询元数据,例如:

  • get all subtypes of some type获取某种类型的所有子类型
  • get all types/methods/fields annotated with some annotation, w/o annotation parameters matching获取所有带有一些注释的类型/方法/字段,没有注释参数匹配
  • get all resources matching matching a regular expression获取匹配正则表达式的所有资源

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

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