I need to parse java method, looking inside all other method calls inside it (and inside them and deeper and so on) in order to find all occurences of some string, let say "System.out.println ("Blabla");"
How can I use JDT (http://www.programcreek.com/2011/01/best-java-development-tooling-jdt-and-astparser-tutorials) for that and what are the other alternatives?
If I have a code like this:
public void A() {
"System.out.println ("Blabla");
B();
}
public void B() {
"System.out.println ("Blabla");
C();
}
public void C() {
"System.out.println ("Blabla");
}
I would like just to specify the name of the method ("A") and as an output I want:
"System.out.println ("Blabla");
"System.out.println ("Blabla");
"System.out.println ("Blabla");
This is possible with JDT/AST. I have done similar things. My favorite approach is to create a plugin for Eclipse and process files (compilation units) inside of the projects.
The basic steps to follow are:
You can use AST to parse the code (i.e. of a JDT ICompilationUnit) into an AST model.
You can then use the visitor pattern to find the MethodDeclaration you want to start your search.
Once you have that, you can use the visitor again on the MethodDeclaration to find all MethodInvocations inside the method body.
The binding of the MethodInvocation will point you to the MethodDeclaration pf the method beeing called. (Be sure to enable bindings while parsing.)
A realy nice tool to use, when working with AST, is the AST View. It shows you the AST model of files opened with the Eclipse Java editor. You can install the AST View from this update site: http://www.eclipse.org/jdt/ui/update-site