Possible Duplicate:
At runtime, find all classes in a Java application that extend a base class
I need to get list of all the classes (child) which extends a particular class (parent) and then create an instance of them all.
how should I do this in Java?
I would use http://code.google.com/p/reflections/
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
This can be used in an indexed or cached mode. As @Jagger suggests, a brute force search of every class is relatively slow. You can also limit the search by package e.g. com.mycom.*
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With