Search code examples
javaeclipseeclipse-pluginabstract-syntax-treeeclipse-jdt

Why does printing parent node of a node returns parent + child nodes?


Given a start position, I can find a node by passing it to org.eclipse.jdt.core.dom.NodeFinder class.

NodeFinder node = new NodeFinder(root, m.getSourceStart(), m.getSourceEnd() - m.getSourceStart() + 1);
ASTNode n = node.getCoveredNode();

Let assume that this node has a parent and get the node's parent n.getParent(); does anyone know why it prints out the parent node and the node?

For instance we know the starting point of foo() in bar.foo() so if I do System.Out.Println(n.getParent().toString()); it prints bar.foo(). Shouldn't it print only bar?

Thanks in advance for your insight.


Solution

  • The behavior you are seeing is expected.

    In this example:

    foo.bar
    

    bar is a SimpleName and its parent is a QualifiedName that contains both foo and bar. So the parent node will contain more than one AST node and calling toString on it will print out all of this node's children.