How to generate a call graph for a java project

3.6k views Asked by At

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 "}"
2

There are 2 answers

6
Peter Taylor On BEST ANSWER

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
4
Thomas Rawyler On

For advanced code analysis you might wanna have a look at http://www.moosetechnology.org/

Cheers
Thomas

(edit: moved down here by general request, See: How to generate a Java call graph)