从JAVA提取AST并将AST打印到文件

从JAVA提取AST并将AST打印到文件,java,debugging,abstract-syntax-tree,extraction,compilationunit,Java,Debugging,Abstract Syntax Tree,Extraction,Compilationunit,我是Java编程语言的初学者。我想从java源代码中提取AST,并将AST打印到文件或标准输出中 我按照本教程学习如何使用AST。 因此,根据我目前掌握的代码如下 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.ASTVis

我是Java编程语言的初学者。我想从java源代码中提取AST,并将AST打印到文件或标准输出中

我按照本教程学习如何使用AST。

因此,根据我目前掌握的代码如下

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);

    }
}
如果有人能帮我把AST打印成一个文件,那将是一个很大的帮助

提前谢谢。

给你。您可以修改该文件以生成XML,而不是JSON等

基于以下示例:


你希望得到什么样的结果?我想把整个AST打印出来,但结果是这样的,“org.eclipse.jdt.core.dom。AST@6eebc39e“您必须手动检查
cu.getAST()
对象的结构并打印所需内容。没有人(我希望)会代替你自己去做。@hbn1991这意味着
AST
类不会重写
toString()
方法本身。你应该迭代它的内容并手动打印。谢谢你们的回复。但实际上有没有更简单的方法来转储ast。因为我实际上不想在AST上做任何处理。
System.out.println(cu.getAST().toString());
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());
    }
}