public abstract class Node extends Object implements Cloneable, HasParentNode<Node>, Visitable, NodeWithRange<Node>, NodeWithTokenRange<Node>
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 |
---|---|
static class |
Node.BreadthFirstIterator
Performs a breadth-first node traversal starting with a given node.
|
static class |
Node.DirectChildrenIterator
Performs a simple traversal over all nodes that have the passed node as their parent.
|
static class |
Node.ObserverRegistrationMode
Different registration mode for observers on nodes.
|
static class |
Node.ParentsVisitor
Iterates over the parent of the node, then the parent's parent, then the parent's parent's parent, until running
out of parents.
|
static class |
Node.Parsedness |
static class |
Node.PostOrderIterator
Performs a post-order (or leaves-first) node traversal starting with a given node.
|
static class |
Node.PreOrderIterator
Performs a pre-order (or depth-first) node traversal starting with a given node.
|
static class |
Node.TreeTraversal |
Modifier and Type | Field and Description |
---|---|
static int |
ABSOLUTE_BEGIN_LINE
Deprecated.
|
static int |
ABSOLUTE_END_LINE
Deprecated.
|
static DataKey<LineSeparator> |
LINE_SEPARATOR_KEY |
static Comparator<NodeWithRange<?>> |
NODE_BY_BEGIN_POSITION
This can be used to sort nodes on position.
|
protected static DataKey<Boolean> |
PHANTOM_KEY |
protected static PrinterConfiguration |
prettyPrinterNoCommentsConfiguration |
protected static DataKey<Printer> |
PRINTER_KEY |
static DataKey<SymbolResolver> |
SYMBOL_RESOLVER_KEY |
Modifier | Constructor and Description |
---|---|
protected |
Node(TokenRange tokenRange) |
Modifier and Type | Method and Description |
---|---|
void |
addOrphanComment(Comment comment) |
Node |
clone() |
boolean |
containsData(DataKey<?> key) |
protected Printer |
createDefaultPrinter() |
protected Printer |
createDefaultPrinter(PrinterConfiguration configuration) |
protected void |
customInitialization()
Called in every constructor for node specific code.
|
boolean |
equals(Object obj) |
<T extends Node> |
findAll(Class<T> nodeType)
Walks the AST with pre-order traversal, returning all nodes of type "nodeType".
|
<T extends Node> |
findAll(Class<T> nodeType,
Node.TreeTraversal traversal)
Walks the AST with specified traversal order, returning all nodes of type "nodeType".
|
<T extends Node> |
findAll(Class<T> nodeType,
Predicate<T> predicate)
Walks the AST with pre-order traversal, returning all nodes of type "nodeType" that match the predicate.
|
Optional<CompilationUnit> |
findCompilationUnit() |
<N extends Node> |
findFirst(Class<N> nodeType)
Walks the AST with pre-order traversal, returning the first node of type "nodeType" or empty() if none is found.
|
<N extends Node> |
findFirst(Class<N> nodeType,
Predicate<N> predicate)
Walks the AST with pre-order traversal, returning the first node of type "nodeType" that matches "predicate" or empty() if none is
found.
|
<T> Optional<T> |
findFirst(Node.TreeTraversal traversal,
Function<Node,Optional<T>> consumer)
Walks the AST, applying the function for every node, with traversal algorithm "traversal".
|
Node |
findRootNode()
Finds the root node of this AST by finding the topmost parent.
|
List<Comment> |
getAllContainedComments()
This 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
|
List<Node> |
getChildNodes()
Contains all nodes that have this node set as their parent.
|
<N extends Node> |
getChildNodesByType(Class<N> clazz)
Deprecated.
use
findAll(Class) but be aware that findAll also considers the initial node. |
Optional<Comment> |
getComment()
This is a comment associated with this node.
|
<M> M |
getData(DataKey<M> key)
Gets data for this node using the given key.
|
Set<DataKey<?>> |
getDataKeys()
This method was added to support the clone method.
|
protected PrinterConfiguration |
getDefaultPrinterConfiguration() |
LineSeparator |
getLineEndingStyle() |
LineSeparator |
getLineEndingStyleOrDefault(LineSeparator defaultLineSeparator) |
NodeMetaModel |
getMetaModel() |
<N extends Node> |
getNodesByType(Class<N> clazz)
Deprecated.
use
findAll(Class) but be aware that findAll also considers the initial node. |
List<Comment> |
getOrphanComments()
This is a list of Comment which are inside the node and are not associated
with any meaningful AST Node.
|
Optional<Node> |
getParentNode()
Returns the parent node, or
Optional.empty if no parent is set. |
Node |
getParentNodeForChildren()
Returns the parent node from the perspective of the children of this node.
|
Node.Parsedness |
getParsed() |
protected Printer |
getPrinter() |
protected Printer |
getPrinter(PrinterConfiguration configuration) |
Optional<Range> |
getRange() |
protected SymbolResolver |
getSymbolResolver() |
Optional<TokenRange> |
getTokenRange() |
int |
hashCode() |
boolean |
hasScope() |
boolean |
isAncestorOf(Node descendant)
Determines whether this node is an ancestor of the given node.
|
boolean |
isPhantom() |
boolean |
isRegistered(AstObserver observer)
Was this observer registered?
Note that equals is used to determine if the given observer was registered.
|
<P> void |
notifyPropertyChange(ObservableProperty property,
P oldValue,
P newValue) |
void |
register(AstObserver observer)
Register an observer.
|
void |
register(AstObserver observer,
Node.ObserverRegistrationMode mode)
Register a new observer for the given node.
|
void |
registerForSubtree(AstObserver observer)
Register the observer for the current node and all the contained node and nodelists, recursively.
|
boolean |
remove()
Try to remove this node from the parent
|
boolean |
remove(Node node) |
Node |
removeComment() |
void |
removeData(DataKey<?> key)
Remove data by key.
|
void |
removeForced()
Forcibly removes this node from the AST.
|
boolean |
removeOrphanComment(Comment comment) |
boolean |
replace(Node node)
Try to replace this node in the parent with the supplied node.
|
boolean |
replace(Node node,
Node replacementNode) |
protected void |
setAsParentNodeOf(Node childNode) |
protected void |
setAsParentNodeOf(NodeList<? extends Node> list) |
Node |
setBlockComment(String comment)
Use this to store additional information to this node.
|
Node |
setComment(Comment comment)
Use this to store additional information to this node.
|
<M> void |
setData(DataKey<M> key,
M object)
Sets data for this node using the given key.
|
Node |
setLineComment(String comment)
Use this to store additional information to this node.
|
Node |
setParentNode(Node newParentNode)
Assign a new parent to this node, removing it
from the list of children of the previous parent, if any.
|
Node |
setParsed(Node.Parsedness parsed)
Used by the parser to flag unparsable nodes.
|
Node |
setRange(Range range) |
Node |
setTokenRange(TokenRange tokenRange) |
Stream<Node> |
stream()
Make a stream of nodes using pre-order traversal.
|
Stream<Node> |
stream(Node.TreeTraversal traversal)
Make a stream of nodes using traversal algorithm "traversal".
|
String |
toString() |
String |
toString(PrinterConfiguration configuration) |
void |
tryAddImportToParentCompilationUnit(Class<?> clazz) |
void |
unregister(AstObserver observer)
Unregister an observer.
|
<T extends Node> |
walk(Class<T> nodeType,
Consumer<T> consumer)
Walks the AST with pre-order traversal, calling the consumer for every node of type "nodeType".
|
void |
walk(Consumer<Node> consumer)
Walks the AST, calling the consumer for every node with pre-order traversal.
|
void |
walk(Node.TreeTraversal traversal,
Consumer<Node> consumer)
Walks the AST, calling the consumer for every node, with traversal algorithm "traversal".
|
finalize, getClass, notify, notifyAll, wait, wait, wait
findAncestor, findAncestor, hasParentNode, isDescendantOf
containsWithin, containsWithinRange, getBegin, getEnd, hasRange
public static Comparator<NodeWithRange<?>> NODE_BY_BEGIN_POSITION
protected static final PrinterConfiguration prettyPrinterNoCommentsConfiguration
@Deprecated public static final int ABSOLUTE_BEGIN_LINE
Position.ABSOLUTE_BEGIN_LINE
@Deprecated public static final int ABSOLUTE_END_LINE
Position.ABSOLUTE_END_LINE
public static final DataKey<SymbolResolver> SYMBOL_RESOLVER_KEY
public static final DataKey<LineSeparator> LINE_SEPARATOR_KEY
protected Node(TokenRange tokenRange)
protected void customInitialization()
protected Printer getPrinter()
protected Printer getPrinter(PrinterConfiguration configuration)
protected Printer createDefaultPrinter()
protected Printer createDefaultPrinter(PrinterConfiguration configuration)
protected PrinterConfiguration getDefaultPrinterConfiguration()
public Optional<Comment> getComment()
public Optional<Range> getRange()
getRange
in interface NodeWithRange<Node>
public Optional<TokenRange> getTokenRange()
getTokenRange
in interface NodeWithTokenRange<Node>
public Node setTokenRange(TokenRange tokenRange)
setTokenRange
in interface NodeWithTokenRange<Node>
public Node setRange(Range range)
setRange
in interface NodeWithRange<Node>
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.public Node setComment(Comment comment)
comment
- to be setpublic final Node setLineComment(String comment)
comment
- to be setpublic final Node setBlockComment(String comment)
comment
- to be setpublic final String toString()
public final String toString(PrinterConfiguration configuration)
public Optional<Node> getParentNode()
HasParentNode
Optional.empty
if no parent is set.getParentNode
in interface HasParentNode<Node>
public List<Node> getChildNodes()
public void addOrphanComment(Comment comment)
public boolean removeOrphanComment(Comment comment)
public List<Comment> getOrphanComments()
For example, comments at the end of methods (immediately before the parenthesis) or at the end of CompilationUnit are orphan comments.
When more than one comment preceeds a statement, the one immediately preceding it it is associated with the statements, while the others are orphans.
Changes to this list are not persisted.
public List<Comment> getAllContainedComments()
public Node setParentNode(Node newParentNode)
setParentNode
in interface HasParentNode<Node>
newParentNode
- node to be set as parentthis
protected void setAsParentNodeOf(Node childNode)
public void tryAddImportToParentCompilationUnit(Class<?> clazz)
@Deprecated public <N extends Node> List<N> getChildNodesByType(Class<N> clazz)
findAll(Class)
but be aware that findAll also considers the initial node.clazz
- the type of node to find.@Deprecated public <N extends Node> List<N> getNodesByType(Class<N> clazz)
findAll(Class)
but be aware that findAll also considers the initial node.public <M> M getData(DataKey<M> key)
M
- The type of the data.key
- The key for the dataIllegalStateException
- if the key was not set in this node.containsData(DataKey)
,
DataKey
public Set<DataKey<?>> getDataKeys()
public <M> void setData(DataKey<M> key, M object)
DataKey
.M
- The type of datakey
- The singleton key for the dataobject
- The data objectDataKey
public boolean containsData(DataKey<?> key)
DataKey
public boolean remove()
RuntimeException
- if it fails in an unexpected waypublic boolean replace(Node node)
RuntimeException
- if it fails in an unexpected waypublic void removeForced()
Since everything at CompilationUnit level is removable, this method will only (silently) fail when the node is in a detached AST fragment.
public Node getParentNodeForChildren()
HasParentNode
That is, this method returns this
for everything except NodeList
. A NodeList
returns its
parent node instead. This is because a NodeList
sets the parent of all its children to its own parent
node (see NodeList
for details).
getParentNodeForChildren
in interface HasParentNode<Node>
public <P> void notifyPropertyChange(ObservableProperty property, P oldValue, P newValue)
public void unregister(AstObserver observer)
Observable
unregister
in interface Observable
public void register(AstObserver observer)
Observable
register
in interface Observable
public void register(AstObserver observer, Node.ObserverRegistrationMode mode)
public void registerForSubtree(AstObserver observer)
public boolean isRegistered(AstObserver observer)
Observable
isRegistered
in interface Observable
public boolean remove(Node node)
public Node removeComment()
public NodeMetaModel getMetaModel()
public Node.Parsedness getParsed()
public Node setParsed(Node.Parsedness parsed)
public Node findRootNode()
public Optional<CompilationUnit> findCompilationUnit()
public LineSeparator getLineEndingStyleOrDefault(LineSeparator defaultLineSeparator)
public LineSeparator getLineEndingStyle()
protected SymbolResolver getSymbolResolver()
public Stream<Node> stream(Node.TreeTraversal traversal)
public void walk(Node.TreeTraversal traversal, Consumer<Node> consumer)
public void walk(Consumer<Node> consumer)
public <T extends Node> void walk(Class<T> nodeType, Consumer<T> consumer)
public <T extends Node> List<T> findAll(Class<T> nodeType)
public <T extends Node> List<T> findAll(Class<T> nodeType, Node.TreeTraversal traversal)
public <T extends Node> List<T> findAll(Class<T> nodeType, Predicate<T> predicate)
public <T> Optional<T> findFirst(Node.TreeTraversal traversal, Function<Node,Optional<T>> consumer)
public <N extends Node> Optional<N> findFirst(Class<N> nodeType)
public <N extends Node> Optional<N> findFirst(Class<N> nodeType, Predicate<N> predicate)
public boolean isAncestorOf(Node descendant)
descendant
- the node for which to determine whether it has this node as an ancestor.true
if this node is an ancestor of the given node, and false
otherwise.HasParentNode.isDescendantOf(Node)
public boolean hasScope()
public boolean isPhantom()
Copyright © 2007–2021. All rights reserved.