3

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 "}"
Tiago Veloso
  • 8,513
  • 17
  • 64
  • 85

2 Answers2

2

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
Peter Taylor
  • 4,918
  • 1
  • 34
  • 59
  • I am not sure this can help. And I know close to nothing about Perl. I know its a shame. – Tiago Veloso Feb 11 '11 at 22:05
  • @Tiago, you can read input and apply regular expression in Java too, it's just rather more verbose. – Peter Taylor Feb 12 '11 at 10:30
  • I'll give this a try. I am guessing that this command will look for files in `com/akshor/pjt33/image` with the `*.class`extension, but shouldn't it be `*.java`? I was under the impression the `javap` command would read java files and not class files. – Tiago Veloso Feb 12 '11 at 15:02
  • Never mind. I am almost done with this, I am tweaking your bash script to my needs. – Tiago Veloso Feb 12 '11 at 16:10
  • @Peter: I am getting some weird symbols like [Z and [B. I have added the current version of the script I am building on my question. – Tiago Veloso Feb 12 '11 at 16:54
  • @Tiago, oops, I should have removed that path: I was just using it for testing. [Z means boolean[], [B means byte[]. javap is the disassembler: it reads class files (and insists on the class name rather than the file name) and emits, depending on the options you give it, anything from a list of public members to the entire constant pool and bytecode in a form which just needs a bit of tweaking to compile it with jasmin. – Peter Taylor Feb 12 '11 at 19:00
1

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)

Community
  • 1
  • 1
Thomas Rawyler
  • 1,075
  • 7
  • 16