I want to write a refactoring tool for the Java programming language.
For this reason I need to do structural queries against the abstract syntax tree. Given this AST how do I get answers to the following statements:
- Get superclasses of an object.
- Give me the expression where I call a method x() on type XClass
- Is method x() is called without arguments (check overloading)
- Is method x() called anywhere in the execution path of object of type XClass (eg. is close() called anywhere)
8
It rather depends on what generated your AST and how it looks.
But, if it’s an XML format with a decent XSD/XMLSchema then I think “groovy” would be the best bet as it has excellent built in support for XML.
Actually it has two built in supports — one API for xpath and one API for DOM.
Groovy is a scripting language built on top of Java. All valid java is valid Groovy, but, groovy also has many Dynamic features such a Dynamic data type, closures, Dictionaries as a built in datatype, native SQL support, native XML support, multiline string literals and lots of other goodies.
4
There are many Java libraries that provide ASTs parsed from Java source code or bytecode. Some examples:
- Javac AST (used by JavaDoc, you can look at the JavaDoc source code for inspiration)
- Soot
- JastAddJ
- Polyglot
I suggest that you pick one library after some research to see which one you prefer. There may be other tools out there, for example Eclipse JDT, which fit better. How you do each of the things you listed will vary between library/tool.
I’m currenty writting a tool that do this type of function. Currently you can try the LGTM tool, used to explore code quality. link: https://lgtm.com/help/lgtm/console/ql-java-basic-example