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.
... and use the various methods on the node to initialize it further, if needed.

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);
where argument is an object of your choice (often simply null.)

Author

Julio Vilmar Gesser

Inheritors

Types

Link copied to clipboard
Performs a breadth-first node traversal starting with a given node.
Link copied to clipboard
Performs a simple traversal over all nodes that have the passed node as their parent.
Link copied to clipboard
Different registration mode for observers on nodes.
Link copied to clipboard
Iterates over the parent of the node, then the parent's parent, then the parent's parent's parent, until running out of parents.
Link copied to clipboard
Link copied to clipboard
Performs a post-order (or leaves-first) node traversal starting with a given node.
Link copied to clipboard
Performs a pre-order (or depth-first) node traversal starting with a given node.
Link copied to clipboard

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
This can be used to sort nodes on position.
Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
abstract fun <R, A> accept(v: GenericVisitor<R, A>, arg: A): R
abstract fun <A> accept(v: VoidVisitor<A>, arg: A)
Accept method for visitor support.
Link copied to clipboard
open fun addOrphanComment(comment: Comment)
Link copied to clipboard
@Nullable
open fun associatedSpecificationComments(): @Nullable NodeList<Comment>
Link copied to clipboard
open fun clone(): Node
Link copied to clipboard
@Nullable
open fun comment(): @Nullable Comment
Link copied to clipboard
open fun containsData(key: DataKey<out Any>): Boolean
Link copied to clipboard
open fun containsWithin(other: Node): Boolean
Link copied to clipboard
open fun containsWithinRange(other: Node): Boolean
Checks whether the range of the given Node is contained within the range of this NodeWithRange.
Link copied to clipboard
open fun equals(obj: Any): Boolean
Link copied to clipboard
open fun <T : Node?> findAll(nodeType: Class<T>): List<T>
Walks the AST with pre-order traversal, returning all nodes of type "nodeType".
open fun <T : Node?> findAll(nodeType: Class<T>, traversal: Node.TreeTraversal): List<T>
Walks the AST with specified traversal order, returning all nodes of type "nodeType".
open fun <T : Node?> findAll(nodeType: Class<T>, predicate: Predicate<T>): List<T>
Walks the AST with pre-order traversal, returning all nodes of type "nodeType" that match the predicate.
Link copied to clipboard
open fun <N> findAncestor(types: Array<Class<N>>): Optional<N>
Walks the parents of this node and returns the first node of type type, or empty() if none is found.
open fun <N> findAncestor(type: Class<N>, predicate: Predicate<N>): Optional<N>
Walks the parents of this node and returns the first node of type type that matches predicate, or empty() if none is found.
open fun <N> findAncestor(predicate: Predicate<N>, types: Array<Class<N>>): Optional<N>
Walks the parents of this node and returns the first node that matches one of types types, or empty() if none is found.
Link copied to clipboard
open fun findByRange(range: Range): Optional<Node>
Link copied to clipboard
Link copied to clipboard
open fun <M> findData(key: DataKey<M>): Optional<M>
Gets data for this node using the given key or returns an Optional.empty().
Link copied to clipboard
open fun <N : Node?> findFirst(nodeType: Class<N>): Optional<N>
Walks the AST with pre-order traversal, returning the first node of type "nodeType" or empty() if none is found.
open fun <T> findFirst(traversal: Node.TreeTraversal, consumer: (Node) -> Optional<T>): Optional<T>
Walks the AST, applying the function for every node, with traversal algorithm "traversal".
open fun <N : Node?> findFirst(nodeType: Class<N>, predicate: Predicate<N>): Optional<N>
Walks the AST with pre-order traversal, returning the first node of type "nodeType" that matches "predicate" or empty() if none is found.
Link copied to clipboard
open fun findRootNode(): Node
Finds the root node of this AST by finding the topmost parent.
Link copied to clipboard
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
Link copied to clipboard
This field is used by key to associated (JML) comments to this node.
Link copied to clipboard
The begin position of this node in the source file.
Link copied to clipboard
open fun getChildNodes(): List<Node>
Contains all nodes that have this node set as their parent.
Link copied to clipboard
open fun <N : Node?> getChildNodesByType(clazz: Class<N>): List<N>
Recursively finds all nodes of a certain type.
Link copied to clipboard
This is a comment associated with this node.
Link copied to clipboard
open fun <M> getData(key: DataKey<M>): M
Gets data for this node using the given key.
Link copied to clipboard
open fun getDataKeys(): Set<DataKey<out Any>>
This method was added to support the clone method.
Link copied to clipboard
open fun getEnd(): Optional<Position>
The end position of this node in the source file.
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open fun <N : Node?> getNodesByType(clazz: Class<N>): List<N>
Link copied to clipboard
This is a list of Comment which are inside the node and are not associated with any meaningful AST Node.
Link copied to clipboard
Returns the parent node, or Optional.empty if no parent is set.
Link copied to clipboard
Returns the parent node from the perspective of the children of this node.
Link copied to clipboard
open fun <T : Node?> getParentNodeOfType(clazz: Class<T>): Optional<T>
Finds the first parent of the given class.
Link copied to clipboard
open fun getRange(): Optional<Range>
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
fun hashCode(): Int
Link copied to clipboard
Returns true if the parent has a parent
Link copied to clipboard
open fun hasRange(): Boolean
Link copied to clipboard
open fun hasScope(): Boolean
Link copied to clipboard
open fun isAncestorOf(descendant: Node): Boolean
Determines whether this node is an ancestor of the given node.
Link copied to clipboard
open fun isDescendantOf(ancestor: Node): Boolean
Determines whether this HasParentNode node is a descendant of the given node.
Link copied to clipboard
open fun isPhantom(): Boolean
Link copied to clipboard
open fun isRegistered(observer: AstObserver): Boolean
Was this observer registered?
Link copied to clipboard
open fun <P> notifyPropertyChange(property: ObservableProperty, oldValue: P, newValue: P)
Link copied to clipboard
open fun register(observer: AstObserver)
Register an observer.
Register a new observer for the given node.
Link copied to clipboard
open fun registerForSubtree(observer: AstObserver)
Register the observer for the current node and all the contained node and nodelists, recursively.
Link copied to clipboard
open fun remove(): Boolean
Try to remove this node from the parent
open fun remove(node: Node): Boolean
Link copied to clipboard
open fun removeComment(): Node
Link copied to clipboard
open fun removeData(key: DataKey<out Any>)
Remove data by key.
Link copied to clipboard
open fun removeForced()
Forcibly removes this node from the AST.
Link copied to clipboard
open fun removeOrphanComment(comment: Comment): Boolean
Link copied to clipboard
open fun replace(node: Node): Boolean
Try to replace this node in the parent with the supplied node.
open fun replace(node: Node, replacementNode: Node): Boolean
Link copied to clipboard
open fun setAssociatedSpecificationComments(@Nullable associatedSpecificationComments: @Nullable NodeList<Comment>): Node
Link copied to clipboard
fun setBlockComment(comment: String): Node
Use this to store additional information to this node.
Link copied to clipboard
open fun setComment(comment: Comment): Node
Use this to store additional information to this node.
Link copied to clipboard
open fun <M> setData(key: DataKey<M>, object: M)
Sets data for this node using the given key.
Link copied to clipboard
fun setLineComment(comment: String): Node
Use this to store additional information to this node.
Link copied to clipboard
open fun setParentNode(newParentNode: Node): Node
Assign a new parent to this node, removing it from the list of children of the previous parent, if any.
Link copied to clipboard
open fun setRange(range: Range): Node
Link copied to clipboard
open fun setTokenRange(tokenRange: TokenRange): Node
Link copied to clipboard
open fun stream(): Stream<Node>
Make a stream of nodes using pre-order traversal.
open fun stream(traversal: Node.TreeTraversal): Stream<Node>
Make a stream of nodes using traversal algorithm "traversal".
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open fun unregister(observer: AstObserver)
Unregister an observer.
Link copied to clipboard
open fun walk(consumer: Consumer<Node>)
Walks the AST, calling the consumer for every node with pre-order traversal.
open fun walk(traversal: Node.TreeTraversal, consumer: Consumer<Node>)
Walks the AST, calling the consumer for every node, with traversal algorithm "traversal".
open fun <T : Node?> walk(nodeType: Class<T>, consumer: Consumer<T>)
Walks the AST with pre-order traversal, calling the consumer for every node of type "nodeType".