See Java code the way javac sees it
I wrote a small Swing app to browse javac's abstract syntax trees (ASTs) of source code. If you've ever wanted to write a tool that analyzes Java source code, it is a big help for understanding the Javac tree API.Last spring I set about learning javac's Tree API (JSR 199) - NetBeans uses it internally as its parser for Java source code. It has a bit of a learning curve, and contains lots of this-tree and that-tree interfaces for different types of structures in Java source code. So to help me develop an intuitive understanding of what all of these classes map to, I wrote a tool - a standalone Swing app that lets you open a Java source file and then browse its AST.
The tree on the left lets you drill through the syntax tree itself. The middle pane shows you the object that is selected, and a tree of all of its fields and methods via introspection, which you can drill as deeply as you want through. The right pane is sensitive to selection in whichever of the other two panes has focus. The top half shows you the list of all types the selected object implements (and boldfaces those that are part of the official API), and the bottom half shows you the value of
toString()for the selected object (if it's part of the syntax tree, it will be the source code text). I found it very useful, and still use it to figure out just how to hunt down a particular thing I want to find in source code. Hopefully it will inspire some others to play with javac as well. It requires JDK 6. You can download the JAR file here and just run it with
contrib/SyntaxTreeNavigator(everything was reorganized for the recent migration to Mercurial, and I haven't figured out where it is in the new source structure yet).
- To use the application, you will need javac on your classpath. If you are running Apple's JDK 1.6 it already is. For other platforms you may need to run with
-Xbootclasspath/p:/PATH/TO/javac.jar(may be in tools.jar).
- This application uses the
com.sun.source.treeAPI from javac. If you are not running Sun's javac, this package will probably not be there.
I have updated the JAR file to produce better diagnostic information, and added a source zip file.