Pradeep's Blog

Google

Monday, May 01, 2006

Finding Class Dependencies in Java

A requirement for our project, CyVis, was to find the classes that a particular class is dependent on. Finding the class dependencies for a particular class can be done as follows

1. Get the type of all the variables declared in the class. This includes the global variables and local variables in all the methods.
2. Get the return type for all the methods.
3. The type of exception thrown in all the methods.
4. The class that the particular class extends.
5. The interface that the particular class implements.

These five would give us a list of classes, that the particular class is dependent upon. Given below is a code snippet that shows, the things you have to look for in order to find the dependent classes. How you find that is up to you. I am using ASM to get this information directly from the class files.

The import statements do not necessarily give you the actual list of dependent classes. There may be some imports that are actually not needed. For the above class the dependent classes can be found from the fields that I have circled.

0 Comments:

Post a Comment

<< Home