The Source for Java Technology Collaboration
User: Password:



Tim Boudreau's Blog

February 2008 Archives


See Java code the way javac sees it

Posted by timboudreau on February 28, 2008 at 03:50 PM | Permalink | Comments (22)

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.

syntaxtreebrowser.jpg

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 java -jar.

The source code is available in NetBeans CVS under 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).Update (3/31/08): I've set up a java.net project for the source code, syntaxtreebrowser.dev.java.net. It is pending approval but it should be publicly visible in a few days.

Caveats: (updated 2/29/07)

  1. 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).
  2. This application uses the com.sun.source.tree API 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.

Class visualization module for NetBeans

Posted by timboudreau on February 28, 2008 at 02:13 PM | Permalink | Comments (0)

Last week I wrote a little module that uses the NetBeans Visual Library and the Javac Tree API to create a graph of the contents of a Java file, showing dependencies between class members. It's now available on the NetBeans daily build update center - if you're running a daily build of NetBeans, just go to Tools | Plugins and download Graphical Class Viewer.

My question is, would anybody actually find this useful? It was mostly an experiment to get more fluent with a couple of APIs I need to know well - but it seems like it could be handy if you're going to edit a class you didn't write and want to see where the action is - or if you've got a bunch of encapsulated fields and want to quickly see if anything is not using the getters and setters.

What it does:
  • Shows a graph (see screenshot below)
  • If you hover the mouse over, say, a method, then the path to things that method calls/uses will be shown in red, and things that call that method are shown in blue
  • You can click the arrow button on the widget for any class member and see its source code

Feel free to try it out and let me know if you think it's something worth putting more work into.

Also, if anyone knows of any speedy and good hub-and-spoke or other layout algorithms for the case where you have n nodes each of which can have up to n-1 connections to other nodes, feel free to let me know - what it does now is less than optimal. At some point I may think through the geometry to do it nicely, but I'm not sure when.

The sources are in NetBeans contrib mercurial repository.

Comments? Reply on the NetBeans DZone thread about it.



Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds