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

后端 未结 2 1881
北荒
北荒 2021-01-03 02:43

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

2条回答
  •  半阙折子戏
    2021-01-03 03:07

    We can retrieve the children as an ASTNode List using the API of :

    ASTNode.getStructureProperty(StructuralPropertyDescriptor property)
    

    It returns the value of the given structural property for this node. The value returned depends on the kind of property:

    SimplePropertyDescriptor - the value of the given simple property, or null if none; primitive values are "boxed"
    ChildPropertyDescriptor - the child node (type ASTNode), or null if none
    ChildListPropertyDescriptor - the list (element type: ASTNode)
    

    However, the ChildListPropertyDescripor is not intended to be instantiated by clients. You can refer to my code to get the list of children:

    public static List getChildren(ASTNode node) {
        List children = new ArrayList();
        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);
            }
        }
        return children;
    }
    

提交回复
热议问题