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...
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.
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.
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.
It is possible to add observers to the the tree. Any change in the tree is sent as an event to any observers watching.
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);where argument is an object of your choice (often simply null.)
| Modifier and Type | Class and Description |
|---|---|
| public static class | Node.
Performs a breadth-first node traversal starting with a given node. |
| public static class | Node.
Performs a simple traversal over all nodes that have the passed node as their parent. |
| public static enum | Node.
Different registration mode for observers on nodes. |
| public static class | Node.
Iterates over the parent of the node, then the parent's parent, then the parent's parent's parent, until running out of parents. |
| public static enum | |
| public static class | Node.
Performs a post-order (or leaves-first) node traversal starting with a given node. |
| public static class | Node.
Performs a pre-order (or depth-first) node traversal starting with a given node. |
| public static enum |
| Modifier and Type | Field and Description |
|---|---|
| public static final int | |
| public static final int | |
| private List | |
| private Comment | |
| private IdentityHashMap | |
| public static Comparator | NODE_BY_BEGIN_POSITION
This can be used to sort nodes on position. |
| private List | |
| private List | |
| private Node | |
| private Node. | |
| protected static final PrettyPrinterConfiguration | |
| private Range | |
| public static final DataKey | |
| private TokenRange | |
| private static PrettyPrinterConfiguration |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public Node | |
| public boolean | |
| protected void | |
| public boolean | equals(final Object
the reference object with which to compare. obj)Overrides java. |
| public <T extends Node> List | |
| public <T extends Node> List | |
| public Optional | Returns: the containing CompilationUnit, or empty if this node is not inside a compilation unit.
|
| public <T> Optional | |
| public <N extends Node> Optional | |
| public <N extends Node> Optional | |
| public Node | |
| public List | Returns: all Comments within the node as a listThis is the list of Comment which are contained in the Node either because they are properly associated to one of its children or because they are floating around inside the Node |
| public List | Returns: all nodes that have this node as their parent.Contains all nodes that have this node set as their parent. |
| public <N extends Node> List | getChildNodesByType(Class<N>
the type of node to find. clazz)
Deprecated
use
Recursively finds all nodes of a certain type.
Node#findAll(Class) but be aware that findAll also considers the initial node.
|
| public Optional | |
| public < The type of the data. M> M | |
| public Set | |
| public NodeMetaModel | |
| public <N extends Node> List | getNodesByType(Class<N> clazz)
Deprecated
use
Node#findAll(Class) but be aware that findAll also considers the initial node.
|
| public List | Returns: all comments that cannot be attributed to a conceptThis is a list of Comment which are inside the node and are not associated with any meaningful AST Node. |
| public Optional | getParentNode()
Implements com. Optional.empty if no parent is set.
|
| public Node | getParentNodeForChildren()
Implements com. |
| public Node. | Returns: whether this node was successfully parsed or not. If it was not, only the range and tokenRange fields will be valid.
|
| public Optional | Returns: the range of characters in the source code that this node covers.Implements com. |
| protected SymbolResolver | |
| public Optional | Returns: the range of tokens that this node covers.Implements com. |
| public static PrettyPrinterConfiguration | |
| public final int | |
| public boolean | Returns: true if this node is an ancestor of the given node, and false otherwise.the node for which to determine whether it has this node as an ancestor. descendant)Determines whether this node is an ancestor of the given node. |
| public boolean | isRegistered(AstObserver observer)
Implements com. |
| public <P> void | |
| public void | register(AstObserver observer)
Implements com. |
| public void | register(AstObserver observer, Node.
Register a new observer for the given node. |
| public void | registerForSubtree(AstObserver observer)
Register the observer for the current node and all the contained node and nodelists, recursively. |
| public boolean | Returns: true if removed, false if it is a required property of the parent, or if the parent isn't set.Try to remove this node from the parent |
| public boolean | |
| public Node | |
| public void | |
| public void | |
| public boolean | |
| public boolean | |
| public boolean | |
| protected void | |
| protected void | |
| public final Node | |
| public Node | |
| public < The type of data M> void | |
| public final Node | |
| public Node | setParentNode(Node
node to be set as parent newParentNode)Implements com. |
| public Node | |
| public Node | setRange(Range
the range of characters in the source code that this node covers. null can be used to indicate that
no range information is known, or that it is not of interest. range)Implements com. |
| public Node | setTokenRange(TokenRange tokenRange)
Implements com. |
| public static void | setToStringPrettyPrinterConfiguration(PrettyPrinterConfiguration toStringPrettyPrinterConfiguration)
|
| public Stream | |
| public Stream | |
| public final String | Returns: pretty printed source code for this node and its children. Formatting can be configured with Node.setToStringPrettyPrinterConfiguration.Overrides java. |
| public final String | Returns: pretty printed source code for this node and its children. Formatting can be configured with parameter prettyPrinterConfiguration.
|
| private Iterable | |
| private Iterator | |
| public void | |
| public void | unregister(AstObserver observer)
Implements com. |
| public void | walk(Node.
Walks the AST, calling the consumer for every node, with traversal algorithm "traversal". |
| public void | |
| public <T extends Node> void |