org.eclipse.jdt.core.dom.ASTNode的子项 [英] Children of org.eclipse.jdt.core.dom.ASTNode

查看:192
本文介绍了org.eclipse.jdt.core.dom.ASTNode的子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Eclise JDT,我需要检索任何ASTNode的子节点。
有没有一个可以使用的实用方法?

Using Eclise JDT, I need to retrieve the children of any ASTNode. Is there a utility method somewhere that I could use ?

现在我可以想到的唯一方法是将ASTVisitor子类化并手动处理各种节点找到自己的孩子。但是,要研究每一个节点类型都是很多的工作。

The only way I can think of right now is to subclass ASTVisitor and treat each kind of node manually to find its children. But it's a lot of work to study every node type.

推荐答案

我将从 ASTView插件,因为这也是一样的。

I would start by looking at source of ASTView Plugin, since that also does the same thing.

根据


  • org.eclipse.jdt.astview.views.ASTViewContentProvider.getNodeChildren(ASTNode)

  • org.eclipse.jdt.astview.views.NodeProperty.getChildren()

所需的代码应该看起来像这样

the required code should look something like this

public Object[] getChildren(ASTNode node) {
    List list= node.structuralPropertiesForType();
    for (int i= 0; i < list.size(); i++) {
        StructuralPropertyDescriptor curr= (StructuralPropertyDescriptor) list.get(i);
            Object child= node.getStructuralProperty(curr);
        if (child instanceof List) {
                return ((List) child).toArray();
        } else if (child instanceof ASTNode) {
            return new Object[] { child };
            }
        return new Object[0];
    }
}

这篇关于org.eclipse.jdt.core.dom.ASTNode的子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆