Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Children of org.eclipse.jdt.core.dom.ASTNode

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

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.

like image 681
Joel Avatar asked Aug 07 '12 08:08

Joel


1 Answers

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

Based on the code in

  • 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];
    }
}
like image 94
Deepak Azad Avatar answered Oct 03 '22 09:10

Deepak Azad