1

Possible Duplicate:
How do you find all subclasses of a given class in Java?

I have a super class, MyClass, and it is abstract. Anyone can implement it. At runtime, I need to determine which classes have inherited from this class, using java reflection. How can one do this?

Community
  • 1
  • 1
maress
  • 121
  • 1
  • 2
  • 4

3 Answers3

4

You can use 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
Community
  • 1
  • 1
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

A tutorial about that is Java Tip 113: Identify subclasses at runtime.

Chris
  • 483
  • 2
  • 5
  • 16
Jerome Cance
  • 8,103
  • 12
  • 53
  • 106
0

The only way to do it (without resorting to external libraries) is to loop through all your classes and check them individually - there's no efficient method built in (nor can there really be, how else would you do it than scanning all the classes since they can be loaded dynamically?)

See Stack Overflow question How do you find all subclasses of a given class in Java? for much more detail.

Community
  • 1
  • 1
Michael Berry
  • 70,193
  • 21
  • 157
  • 216