Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java list all the child classes of a parent [duplicate]

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?

like image 565
MBZ Avatar asked Apr 23 '12 08:04

MBZ


1 Answers

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.*

like image 100
Peter Lawrey Avatar answered Sep 23 '22 21:09

Peter Lawrey