28

What I'd like to do is scan a set of Java classes, and trace all method calls from a specific method of an Abstract Class, and within that context, build a list of all code which performs some operation (in this case, instantiates an instance of a certain class). I want to know, the line number, and the arguments supplied.

I've begun looking at BCEL, but it doesn't seem to have call graph tracing built in? I'm hesitant to write my own because getting the overloading, type signatures and polymorphic dispatch right might be be tricky.

I half expected a tool or example code to exist, but I haven't found anything yet. It really feels like I'm about to reinvent a wheel. But if I do it will be an open source wheel and available on GitHub ;-)

PS: You will find the existing question "How to Generator a Java Call Graph", because it sounds identical, but it's not at all what I need.

Christophe Roussy
  • 13,732
  • 2
  • 75
  • 75
Mark Renouf
  • 29,573
  • 19
  • 89
  • 120
  • 1
    Actually, it seems like the second part of the accepted answer (about finding all references) is, in fact, what you want. – Anon. Feb 09 '11 at 22:54
  • I need code that does this as part of an automated proces. – Mark Renouf Feb 10 '11 at 03:41
  • 1
    Mark, did you every get this working with Soot? I unfortunately did not succeed as I couldn't set the entry point successfully in a class that has no main method. How did you get on with this? – Joeblackdev Jul 26 '11 at 14:58
  • Hi Mark, have you found an acceptable solution after all these years? -- we can up-vote yours. – xpt Jan 27 '20 at 14:26

6 Answers6

8

You can use the java-callgraph tool suite to create accurate enough static and dynamic callgraphs for Java.

Georgios Gousios
  • 2,315
  • 1
  • 23
  • 31
6

You can try JavaDepend , it gives many features needed for dependencies and metrics, it provides also a CQL like SQL to request your code base.

Disclosure: it's a commercial software.

xpt
  • 13,224
  • 19
  • 76
  • 149
6

You can use Doxygen with Graphviz. It is easy to install and use.

albert
  • 5,966
  • 3
  • 13
  • 29
metdos
  • 8,991
  • 13
  • 62
  • 101
5

Soot should allow you to easily achieve what you are looking for: http://www.sable.mcgill.ca/soot/

It can construct precise call graphs fully automatically.

You can find all necessary documentation here: http://www.sable.mcgill.ca/soot/tutorial/index.html

Also, there's an active mailing list for Soot.

Eric
  • 1,247
  • 1
  • 10
  • 19
2

It sounds like you want something that provides access to the abstract syntax and a complete symbol table. Then a custom scan of the ASTs of the functions in the call graph rooted in each implementing method (as indicated by the symbol tables) of an abstract method gives you a chance to locate a new operation whose type is the specific class of interest.

The DMS Software Reengineering Toolkit is generalized compiler technology providing basic services of parsing, AST building/navigation, symbol table building/navigation, control flow, data flow and call graph construction. DMS has an optional Java Front End that provides a full Java parser, builds Java ASTs and symbol tables, and can construct a call graph. The Java Front End can also read .class files; you weren't clear as to whether you wanted to climb into class files, too, hunting for information.

The answer you want isn't off the shelf. You need to build some custom code to implement the ideas in the first paragraph, but DMS can provide most of the raw material. It doesn't provide much detail from the .class files (these are used mostly to resolve types in source code).

Ira Baxter
  • 88,629
  • 18
  • 158
  • 311
0

For a 'recent' Eclipse install (relative to the question), see Certiv CallGraph.

CallGraph enables graphical analysis of program call relations and flow sequencing. Also enables exploration of extended class inheritance hierarchies.

Call-path analysis and class hieararchy resolution are performed using the JDT platform Search and Call Hierarchy mechanisms.

Sequence diagrams are generated from a static analysis of of the JDT platform AST for any selected class or method.

Uses Zest as the graphics visualization engine.

You can install it via the Eclipse marketplace. I am not involved in making this. You cannot zoom out which is not very practical but has support for Sequence Diagram which is nice and allows to open/close nodes on demand to dig further.

Requirements:

Eclipse 4.6 (Neon) on Java 8 VM
Eclipse Zest Visualization Toolkit 1.7

Eclipse Public License v1.0

Community
  • 1
  • 1
Christophe Roussy
  • 13,732
  • 2
  • 75
  • 75