Search code examples
javacall-graph

How to generate a call graph for a java project


Is it possible to generate a global call graph of an application?

Basically I am trying to find the most important class of an application.

I am looking for options for Java.

I have tried Doxy Gen, but it only generates inheritance graphs.

My current script:

#! /bin/bash

echo "digraph G
{"
find $1 -name \*.class |
    sed s/\\.class$// |
    while read x
    do
        javap -v $x | grep " = class" | sed "s%.*// *%\"$x\" -> %" | sed "s/$1\///" | sed "s/-> \(.*\)$/-> \"\1\"/"
    done
echo "}"

Solution

  • javap -v and a bit of perl will get you dependencies between classes. You can make your parser slightly more sophisticated and get dependencies between methods.

    Update: or if you have either a *nix or cygwin you can get a list of dependencies as

    find com/akshor/pjt33/image -name \*.class |
      sed s/\\.class$// |
        while read x
        do
          javap -v $x | grep " = class" | sed "s%.*// *%$x -> %"
        done
    

    Add a header and a footer and you can pass it to dot to render a graph. If you just want to know which classes are used by the most other classes, as your question implies, then

    find com/akshor/pjt33/image -name \*.class |
      sed s/\\.class$// |
        while read x
        do
          javap -v $x | grep " = class" | sed "s%.*// *%%"
        done |
          sort | uniq -c | sort -n