2

I'm a beginner to the Java programming language. I want to extract the AST from java source code and print the AST to a file or standard output.

I followed this tutorial to learn how to work with AST. http://www.programcreek.com/2011/01/a-complete-standalone-example-of-astparser/

So according to that the code I have so far is as follows.

import java.util.HashSet;
import java.util.Set;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;

public class Test {
    public static void main(String args[]){
        ASTParser parser = ASTParser.newParser(AST.JLS3);
        parser.setSource("public class A { int i = 9;  \n int j; \n ArrayList<Integer> al = new ArrayList<Integer>();j=1000; }".toCharArray());
        //parser.setSource("/*abc*/".toCharArray());
        parser.setKind(ASTParser.K_COMPILATION_UNIT);
        //ASTNode node = parser.createAST(null);


        final CompilationUnit cu = (CompilationUnit) parser.createAST(null);

    }
}

I tried the following code snippet to print it out to the standard output but it didn't give me the result I expected,

System.out.println(cu.getAST().toString());

If somebody could help me to print the AST out to a file it would be a great help.

Thanks in advance.

hbn1991
  • 264
  • 3
  • 10
  • And which result did you expect? – Andremoniy Mar 10 '15 at 11:13
  • I wanted the whole AST printed out but the result was something like this, "org.eclipse.jdt.core.dom.AST@6eebc39e" – hbn1991 Mar 10 '15 at 11:16
  • You have to manually go through `cu.getAST()` object's structure and print what you want. Nobody (I hope) will do it instead of you self. – Andremoniy Mar 10 '15 at 11:17
  • @hbn1991 it means that `AST` class doesn't override `toString()` method itself. You should iterate on its content and print it manually. – Everv0id Mar 10 '15 at 11:26
  • Thanks for the reply both of you. But actually is there an easier way to just dump the ast. Because i actually dont want to do any processing on the AST. – hbn1991 Mar 10 '15 at 13:21
  • If all you want to do is see an AST, you can look here: http://stackoverflow.com/a/6378997/120163 – Ira Baxter Mar 10 '15 at 13:36
  • you can check the code for the AstView plugin. It depicts a very detailed AST strcuture for a java file. https://eclipse.org/jdt/ui/astview/index.php – Unni Kris Mar 11 '15 at 09:44

1 Answers1

3

Here is an example of transforming the AST to JSON. You can modify the JSONStyleASTPrinter.java file to produce XML instead JSON etc.

Based on example from How To Train the JDT Dragon combined.pdf:

private void print(ASTNode node) {
    List properties = node.structuralPropertiesForType();
    for (Iterator iterator = properties.iterator(); iterator.hasNext();) {
        Object descriptor = iterator.next();
        if (descriptor instanceof SimplePropertyDescriptor) {
            SimplePropertyDescriptor simple = (SimplePropertyDescriptor) descriptor;
            Object value = node.getStructuralProperty(simple);
            System.out.println(simple.getId() + " (" + value.toString() + ")");
        } else if (descriptor instanceof ChildPropertyDescriptor) {
            ChildPropertyDescriptor child = (ChildPropertyDescriptor) descriptor;
            ASTNode childNode = (ASTNode) node.getStructuralProperty(child);
            if (childNode != null) {
                System.out.println("Child (" + child.getId() + ") {");
                print(childNode);
                System.out.println("}");
            }
        } else {
            ChildListPropertyDescriptor list = (ChildListPropertyDescriptor) descriptor;
            System.out.println("List (" + list.getId() + "){");
            print((List) node.getStructuralProperty(list));
            System.out.println("}");
        }
    }
}

private void print(List nodes) {
    for (Iterator iterator = nodes.iterator(); iterator.hasNext();) {
        print((ASTNode) iterator.next());
    }
}
worldofjr
  • 3,868
  • 8
  • 37
  • 49
Oleg Mazko
  • 1,720
  • 15
  • 10