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

标签 java debugging abstract-syntax-tree extract compilationunit

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

我按照本教程学习了如何使用 AST。 http://www.programcreek.com/2011/01/a-complete-standalone-example-of-astparser/

所以根据我到目前为止的代码如下。

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

    }
}

我尝试使用以下代码片段将其打印到标准输出,但它没有给我预期的结果,

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

如果有人可以帮助我将 AST 打印到文件中,那将是一个很大的帮助。

提前致谢。

最佳答案

这里是an example of transforming the AST to JSON 。您可以修改JSONStyleASTPrinter.java文件来生成 XML 而不是 JSON 等

基于 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());
    }
}

关于java - 从 JAVA 中提取 AST 并将 AST 打印到文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28962210/

相关文章:

java - 更新链表

java - 数组作为列表转换为二进制

java - 如何从 Amazon S3 下载图像并将其保存到 ImageView

c# - 调试时在 Watch Window 中查看 Exception.Data

java - 使用 ANTLR 为 java 源代码生成抽象语法树

java - 如何在 Java 中并排打印行?

c++ - 如何创建自定义 Windows 消息?

macos - 如何绘制 RDF 图进行检查?

reactjs - Redux 中的多态性和 Action 冒泡?

java - 如何在 Antlr 中使用访问者创建自定义 AST