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)
  • 2
    Look up pmd custom rules in xpath.
    – user40980
    Feb 3, 2014 at 14:13
  • 1
    @matcauthon How does one define "easier to use"? There could be many different aspects of usability that could be harder or easier in an entire software package. Rating this would be like trying to give a number rating for a flavor. Further still polling for tools and offsite resources are explicitly off topic.
    – maple_shaft
    Feb 5, 2014 at 13:25
  • 2
    @matcauthon While the OP is polling for tools you will notice that he also presented a unique problem he is facing in his design. You will also notice that the answers to this question are not just offering suggestions for tools but are addressing the OP's actual problem. If you are facing a real design or architectural challenge as well then please feel free to edit your question and add this detail and the question might then be eligible to be reopened.
    – maple_shaft
    Feb 5, 2014 at 15:55
  • 2
    @maple_shaft Ok. So I totally changed my question...
    – matcauthon
    Feb 5, 2014 at 16:00
  • 1
    My question on Stack Overflow: stackoverflow.com/questions/23066670/… Apr 29, 2014 at 18:37

3 Answers 3


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.

  • what is "groovy"?
    – gnat
    Feb 6, 2014 at 6:39
  • 1
    @gnat -- see edit. I did think most people on this site would have at least have heard of it, but thanks for the comment. Feb 6, 2014 at 7:47
  • As i use groovy in every day programming, this is a good tip. Furthermore it integrates very well in other Java platforms
    – matcauthon
    Feb 6, 2014 at 8:41
  • > "All valid java is valid Groovy" There's many occasions where Java doesn't run as Groovy or does something different. The official list from Codehaus listing gotchas like == is glib about the nasty bugs that arise when compiling Java code in Groovy. Then there's the gotchas not listed. When I was using Groovy a lot 5 yrs ago, I would often compile Java code in Groovy and it wouldn't work the same and I just couldn't work out why. Java 8 is likely to bring many more incompatibilities. Codehaus seem to be dragging their feet on this. Feb 21, 2014 at 11:40

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.