java - 如何查找 ASTNode 的所有子节点(子节点和子节点的子节点)

标签 java eclipse recursion eclipse-rcp abstract-syntax-tree

我试图通过获取 ExpressionStatements 并返回它们的子节点及其子子节点来获取 AST 节点的所有子节点,但算法卡在第一个 ExpStat 中,我找不到原因。

首先,我创建了一个访问者函数来查找我的类的所有 ExpressionStatements,然后调用该函数来查找您的子项

private void analyseClass(ICompilationUnit classe) throws JavaModelException {
    // ICompilationUnit unit == class
    // now create the AST for the ICompilationUnits
    CompilationUnit parse = parse(classe);

    // Calls the method for visit node in AST e return your information
    ExpressionStatementVisitor visitor = new ExpressionStatementVisitor();
    parse.accept(visitor);

    // Write in the screen: ExpressionStatement and your type next
    for (ExpressionStatement method : visitor.getExpression()) {
        //String t = null;

        // 32 -> METHOD_INVOCATION type
        if (method.getExpression().getNodeType() == 32) {
            getChildren(method);
            results.append("\n\n");
        }

        // 48 -> SUPER_METHOD_INVOCATION type
        else if  (method.getExpression().getNodeType() == 48) {
            // results.append("\n SuperMethodInvocation: " + t);
            //getChildren(method);
            //results.append("\n\n");
        } else {
            //getChildren(method);
            //results.append("\n\n");
        }
    }
}

递归查找子级的函数:

public static void getChildren(ASTNode node) {
    if (node != null) {
        List<ASTNode> children = new ArrayList<ASTNode>();
        List list = node.structuralPropertiesForType();
        for (int i = 0; i < list.size(); i++) {
            Object child = node.getStructuralProperty((StructuralPropertyDescriptor) list.get(i));
            if (child instanceof ASTNode) {
                children.add((ASTNode) child);
            }               
            if (children.get(0) != null) {
                String c = children.toString();
                results.append("Children Node: " + c + "\n");
                getChildren(children.get(0));
            } 
        }
    }    else {
        return; 
    }       
}

假设类里面有:

a.getTheDataA().getTheDataB().getTheDataC().getTheData();
b.getTheDataA().getTheDataB().getTheDataC().getTheData();
c.getTheE(a,b).getTheF(getTheDataB).getTheH();

getChildren 函数仅读取 a.getTheDataA().getTheDataB().getTheDataC().getTheData();并像这样返回他的 child 和 child 的 child :

print screen

有一天我被困在这个问题上,我需要递归方面的帮助

最佳答案

据我所知,您只能获得 children 的第一个元素,我认为您需要取出语句检查以查看 childrenelement 是否不为 null进入一个单独的 for 循环,并检查其中的每个元素。

类似于:

public static void getChildren(ASTNode node) {
    if (node != null) {
        List<ASTNode> children = new ArrayList<ASTNode>();
        List list = node.structuralPropertiesForType();
        for (int i = 0; i < list.size(); i++) {
            Object child = node.getStructuralProperty((StructuralPropertyDescriptor) list.get(i));
            if (child instanceof ASTNode) {
                children.add((ASTNode) child);
            }               
        }
        for(ASTNode node : children){
            if (node != null) {
                String c = children.toString();
                results.append("Children Node: " + c + "\n");
                getChildren(node);
            } 
        }
    }else {
        return; 
    }       
}

我还没有运行代码,但我认为问题是你只得到children的第一个元素

关于java - 如何查找 ASTNode 的所有子节点(子节点和子节点的子节点),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40547714/

相关文章:

java - 如何在 Eclipse IDE 中的 Apache Tomcat 服务器中部署创建的 .jar 文件?

java - Web 应用程序测试中的完整字段从 Python/Eclipse/DyDev 中的映射文档调用数据

c++ - 如何在递归函数中将字符串捕获到变量中?

java.lang.RuntimeException : java.net.UnknownHostException:主机未解析:

java - 如何访问cache2k的内部状态

Java:高效计算大文件的SHA-256哈希值

java - 在 A[0 ... n - 2] 上重复,将结果添加到 A[n - 1] ,然后返回总和

java - 反向链表Java内存

eclipse - 在 Ubuntu 中,使用 sudo 启动 eclipse 是个好主意吗?

javascript - 返回 vector 的递归 C++