Analyzing Code Dependencies (part I)

Posted on by alexandra.rusina

If you have a large project with several modules using a set of libraries, any operation that requires an essential structural change can be a disaster. For example, you may decide to stop using one of the libraries, or extract API, or simply extract a new module. In any of these cases, you need to analyze dependencies within your project. Well, it’s easy to say.
Let’s consider the case when you need to extract a new module and make it dependant only on API. It means that first you need to search through code and find references to resources not included into API. It may take hours of dull work.
IntelliJ IDEA’s solution is the dependency analyzer that can show you all the module dependencies in one convenient window. Furthermore, this analyzer can find not only dependencies between modules, but also between packages. It can even find dependencies of a certain class.
On the screenshot below, you can see how IntelliJ IDEA shows dependencies of a package.
dependencies
On the left panel you can see the classes of the analyzed package (com.intellij.sisyphus.ea.util), while the right panel shows all the libraries and classes on which this package depends.
But that’s only half of the way. You need not only the information about dependencies, but also the exact code that actually “establishes” this dependency (references to the class from the package, usages of the library classes, etc.)
To find all the usages of a certain package, class, or library within the analyzed code, just click the item in the right panel. For example, the following screenshot shows all the usages of the com.intellij.sisyphus.ea.lexer classes within the com.intellij.sisyphus.ea.util package.
dependencies and usages
Now having all the information about dependencies, you can process them according to your needs.

Note   This tip&trick was originally posted at www.javalobby.org.

Technorati tags: ,
Comments below can no longer be edited.

2 Responses to Analyzing Code Dependencies (part I)

  1. Tim Haley says:

    April 12, 2006

    OK, at this point you have a list of the usages of classes/methods in a given package by code in another package.

    You can export that list into a text file, or simply work interactively in the IDE with the dependencies displayed in the toolwindow.

    How hard would it be for IntelliJ IDEA to take this one step further and extract/package an entire API layer for you? It should provide options for extracting Interfaces, which is a currently available refactoring for an individual class, or exposing the classes directly. It should also have the ability to generate an IntelliJ Module for the new API layer and create the appropriate inter-module dependencies.

  2. Chris says:

    May 2, 2006

    Is it possible to have this information generated/exported automatically? I would like a regular report – and possibly to run a specific dependency check against a historic sequence of dependency counts from version control. The basic use case here is to have a view of a major migration effort.

Subscribe

Subscribe for updates