Node
Base class for all nodes of the abstract syntax tree.
Construction
The tree is built by instantiating the required nodes, then adding them to other nodes. If it is the parser who is building the tree, it will use the largest constructor, the one with "range" as the first parameter. If you want to manually instantiate nodes, we suggest to...
- use a convenience method, like "addStatement(...)", or if none are available...
- use a convenient constructor, like ClassOrInterfaceType(String name), or if none are available...
- use the default constructor.
- Alternatively, use one of the JavaParser.parse(snippet) methods.
Parent/child
The parent node field is managed automatically and can be seen as read only. Note that there is only one parent, and trying to use the same node in two places will lead to unexpected behaviour. It is advised to clone() a node before moving it around.
Comments
Each Node can have one associated comment which describes it and a number of "orphan comments" which it contains but are not specifically associated to any child.
Positions
When the parser creates nodes, it sets their source code position in the "range" field. When you manually instantiate nodes, their range is not set. The top left character is position 1, 1. Note that since this is an abstract syntax tree, it leaves out a lot of text from the original source file, like where braces or comma's are exactly. Therefore there is no position information on everything in the original source file.
Observers
It is possible to add observers to the tree. Any change in the tree is sent as an event to any observers watching.
Visitors
The most comfortable way of working with an abstract syntax tree is using visitors. You can use one of the visitors in the visitor package, or extend one of them. A visitor can be "run" by calling accept on a node:
node.accept(visitor, argument);Author
Julio Vilmar Gesser
Inheritors
Types
Properties
Functions
Node is contained within the range of this NodeWithRange.type, or empty() if none is found.type that matches predicate, or empty() if none is found.types, or empty() if none is found.Optional.empty if no parent is set.HasParentNode node is a descendant of the given node.