Inspecting an AST

Inspecting an AST

Two of the more common questions on JavaParser are:

  • If I have this piece of code, how do I access that part of it?
  • If I want this piece of code, how do I create the AST for it?

and both can be answered the same way: by looking at the AST that is generated when you parse the code you have or want.

We offer several ways to do this.

First: parse the code

This is something like:

Option 1: your IDE’s debugger

You’ll need to figure out how your IDE works to get this to work.

Put a breakpoint on System.out.println(cu); and run the program in debug mode. When it stops at the breakpoint, you should be able to see the known variables. Look for cu and you can click it open, and now you can inspect the whole generated AST.

Option 2: use one the structure printers

Thanks to Ryan Beckett there are a few printers that will output the structure of the AST with only one purpose: for you to look at.

My favourite one outputs Yaml:

Here’s the output. You can see property names (isInterface, identifier, …) node types (Type=CompilationUnit) and values ("false", "x"). You can see how a type has members, how int x; is one of those members, that it has type FieldDeclaration and can contain multiple variables, but there is only one now: "x".

If you like looking at XML, try:

You will need to load this into an editor that can format XML.

Finally an extra cool one – it outputs the AST as a dot file which can be visualized as a graph with Graphviz!

I’ve sent the output to Graphiz this way:

Be the first to comment

Leave a Reply