Getting class hierarchy in Java?

I have a specific problem that Eclipse is perfect for, but I need a software solution. What I want to do is get the "type hierarchy" of any class that I provide. Eclipse really shows the solution when you press Ctrl + T , but how is this achieved? Are there any APIs so that I can use them?

+7
source share
3 answers

You can use the Java reflection API to get type information at runtime.

For example, you can use Class.getSuperclass () to go up the type tree and find the parents of the class.

+7
source

Java has a Reflection API, which you can use to determine the base class of any class that you have, as well as for any interfaces that a particular class implements. However, determining which classes inherit from this class will be a bit more complicated. The reflection API also allows you to do many other things, as well as determine what the members of this class are, and even call methods of that class, etc.

public void DisplaySuperClass(Class c) { System.out.println(c.getSuperclass().getName()); } 
+2
source

However, there is no easy way to find all subclasses of a class. To do this, you will have to load all the classes in your class path (this can be many thousands) and create this tree yourself, perhaps through a custom ClassLoader (and only if you are resistant to the pain you want to go there) / p>

+1
source

All Articles