ict1991
ict1991

Reputation: 2100

How to create a simple static call graph in Java?

I would like to generate a simple call graph in order to be able to analyze the flow of execution in methods in java.

So for example, if in the code I'm able to go from method X to method Y in a number of method calls, then I would like to generate the call graph which shows the flow from method X to method Y.

I tried using SOOT however I did not manage to get this required call graph. Does anyone know of an API which I can use to generate such call graphs? I'm using Eclipse, however I cannot use plugins, as this tool is needed to be used across a number of projects

Thanks a lot for your help

Upvotes: 3

Views: 8151

Answers (4)

jbranchaud
jbranchaud

Reputation: 6087

I have been looking into what static analysis libraries are available for Java recently. Here are some of them that I have come across:

  • Chord: seems to be a robust framework that is still being maintained/updated. Here is a link to some of the documentation on their predefined analysis (includes Call Graph stuff).
  • DepFinder: this tool is used for generating a dependency graph from a project, I imagine you could extract static call graph information from it.

Upvotes: 3

rrevo
rrevo

Reputation: 1767

JTracer is a tool for understanding execution flow of Java programs by generating and visualizing call graphs. Various events like method entry and method exit are logged and visualized. No modification of code is required.

Upvotes: 0

Sridhar Sarnobat
Sridhar Sarnobat

Reputation: 25196

This is as good an API as I've seen:

https://github.com/gousiosg/java-callgraph

Upvotes: 3

Heiko Rupp
Heiko Rupp

Reputation: 30934

You could in Y throw an Exception that is caught in X and then analyze the Exceptions stack trace

// in X: 
try {
   do your calls ...
} catch (Exception e )  {
     StackTraceElement[] elements =  e.getStackTrace();
     for (StackTraceElement e : elements ) {
       // ... evaluate / print ...
       System.out.println(e);
     }
}

Upvotes: 0

Related Questions