Search code examples
javareflectionruntime

Is it possible to get all the subclasses of a class?


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

Hi,

I would like to get a list of classes that implement an interface in Java at runtime so I can do a lookup service without having to hard code it. Is there a simple way of doing this? I fear not.


Solution

  • The short answer is no.

    The long answer is that subclasses can come into existence in many ways, which basically makes it impossible to categorically find them all.

    You can't do it at runtime but you can't find classes until they're loaded and how do you know they're loaded? You could scan every JAR and class file but that's not definitive. Plus there are things like URL class loaders.

    Inner classes (static and non-static) are another case to consider. Named inner classes are easier to find. Anonymous inner classes are potentially much more difficult to find.

    You also have to consider that if a class has subclasses then new subclasses can be created at a later point.