Top Description Inners Fields Constructors Methods
com.github.javaparser.ast

public abstract Class Node

extends Object
implements Cloneable, HasParentNode<Node>, Visitable, NodeWithRange<Node>, NodeWithTokenRange<Node>
Class Inheritance
All Implemented Interfaces
com.github.javaparser.ast.nodeTypes.NodeWithTokenRange, com.github.javaparser.ast.nodeTypes.NodeWithRange, com.github.javaparser.ast.visitor.Visitable, com.github.javaparser.HasParentNode, com.github.javaparser.ast.observer.Observable, java.lang.Cloneable
Known Direct Subclasses
com.github.javaparser.ast.PackageDeclaration, com.github.javaparser.ast.body.BodyDeclaration, com.github.javaparser.ast.body.Parameter, com.github.javaparser.ast.body.ReceiverParameter, com.github.javaparser.ast.body.VariableDeclarator, com.github.javaparser.ast.comments.Comment, com.github.javaparser.ast.expr.Expression, com.github.javaparser.ast.expr.MemberValuePair, com.github.javaparser.ast.expr.Name, com.github.javaparser.ast.expr.SimpleName, com.github.javaparser.ast.modules.ModuleDeclaration, com.github.javaparser.ast.modules.ModuleDirective, com.github.javaparser.ast.stmt.CatchClause, com.github.javaparser.ast.stmt.Statement, com.github.javaparser.ast.stmt.SwitchEntry, com.github.javaparser.ast.type.Type, com.github.javaparser.ast.ArrayCreationLevel, com.github.javaparser.ast.CompilationUnit, com.github.javaparser.ast.ImportDeclaration, com.github.javaparser.ast.Modifier
Imports
com.github.javaparser.HasParentNode, .Range, .TokenRange, com.github.javaparser.ast.comments.BlockComment, .Comment, .LineComment, com.github.javaparser.ast.nodeTypes.NodeWithRange, .NodeWithTokenRange, com.github.javaparser.ast.observer.AstObserver, .ObservableProperty, .PropagatingAstObserver, com.github.javaparser.ast.visitor.CloneVisitor, .EqualsVisitor, .HashCodeVisitor, .Visitable, com.github.javaparser.metamodel.*, .NodeMetaModel, .JavaParserMetaModel, com.github.javaparser.printer.PrettyPrinter, .PrettyPrinterConfiguration, com.github.javaparser.resolution.SymbolResolver, com.github.javaparser.resolution.types.ResolvedType, java.util.*, java.util.function.Consumer, .Function, .Predicate, java.util.stream.Stream, .StreamSupport, com.github.javaparser.ast.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...

... 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 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

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
Node.BreadthFirstIterator

Performs a breadth-first node traversal starting with a given node.

public static class
Node.DirectChildrenIterator

Performs a simple traversal over all nodes that have the passed node as their parent.

public static enum
Node.ObserverRegistrationMode

Different registration mode for observers on nodes.

public 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.

public static enum
public static class
Node.PostOrderIterator

Performs a post-order (or leaves-first) node traversal starting with a given node.

public static class
Node.PreOrderIterator

Performs a pre-order (or depth-first) node traversal starting with a given node.

public static enum

Field Summary

Modifier and TypeField and Description
public static final int
public static final int
private List<Node>
private Comment
private IdentityHashMap<DataKey<?>, Object>
public static Comparator<NodeWithRange<?>>
NODE_BY_BEGIN_POSITION

This can be used to sort nodes on position.

private List<AstObserver>
private List<Comment>
private Node
private Node.Parsedness
protected static final PrettyPrinterConfiguration
private Range
public static final DataKey<SymbolResolver>
private TokenRange
private static PrettyPrinterConfiguration

Constructor Summary

AccessConstructor and Description
protected
Node(TokenRange tokenRange)

Method Summary

Modifier and TypeMethod and Description
public void
public Node
clone()

Overrides java.lang.Object.clone.

Creates and returns a copy of this object.
public boolean

Returns:

does this node have data for this key?
containsData
(DataKey<?> key)

protected void
customInitialization()

Called in every constructor for node specific code.

public boolean
equals(final Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public <T extends Node> List<T>
findAll(Class<T> nodeType)

Walks the AST with pre-order traversal, returning all nodes of type "nodeType".

public <T extends Node> List<T>
findAll(Class<T> nodeType, Predicate<T> predicate)

Walks the AST with pre-order traversal, returning all nodes of type "nodeType" that match the predicate.

public Optional<CompilationUnit>

Returns:

the containing CompilationUnit, or empty if this node is not inside a compilation unit.
findCompilationUnit
()

public <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".

public <N extends Node> Optional<N>
findFirst(Class<N> nodeType)

Walks the AST with pre-order traversal, returning the first node of type "nodeType" or empty() if none is found.

public <N extends Node> Optional<N>
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.

public Node
findRootNode()

Finds the root node of this AST by finding the topmost parent.

public List<Comment>

Returns:

all Comments within the node as a list
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

public List<Node>

Returns:

all nodes that have this node as their parent.
getChildNodes
()

Contains all nodes that have this node set as their parent.

public <N extends Node> List<N>
getChildNodesByType(Class<N>
the type of node to find.
clazz
)

Deprecated use Node#findAll(Class) but be aware that findAll also considers the initial node.
Recursively finds all nodes of a certain type.
public Optional<Comment>

Returns:

comment property
getComment
()

This is a comment associated with this node.

public <
The type of the data.
M
>
M

Returns:

The data.
getData
(final DataKey<M>
The key for the data
key
)

Gets data for this node using the given key.

public Set<DataKey<?>>

Returns:

all known data keys.
getDataKeys
()

This method was added to support the clone method.

public NodeMetaModel

Returns:

get JavaParser specific node introspection information.
getMetaModel
()

public <N extends Node> List<N>
getNodesByType(Class<N> clazz)

Deprecated use Node#findAll(Class) but be aware that findAll also considers the initial node.
public List<Comment>

Returns:

all comments that cannot be attributed to a concept
getOrphanComments
()

This is a list of Comment which are inside the node and are not associated with any meaningful AST Node.

public Optional<Node>
getParentNode()

Implements com.github.javaparser.HasParentNode.getParentNode.

Returns the parent node, or Optional.empty if no parent is set.
public Node
getParentNodeForChildren()

Implements com.github.javaparser.HasParentNode.getParentNodeForChildren.

Returns the parent node from the perspective of the children of this node.
public Node.Parsedness

Returns:

whether this node was successfully parsed or not. If it was not, only the range and tokenRange fields will be valid.
getParsed
()

public Optional<Range>

Returns:

the range of characters in the source code that this node covers.
getRange
()

Implements com.github.javaparser.ast.nodeTypes.NodeWithRange.getRange.

protected SymbolResolver
public Optional<TokenRange>

Returns:

the range of tokens that this node covers.
getTokenRange
()

Implements com.github.javaparser.ast.nodeTypes.NodeWithTokenRange.getTokenRange.

public static PrettyPrinterConfiguration
public final int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.
public boolean

Returns:

true if this node is an ancestor of the given node, and false otherwise.
isAncestorOf
(Node
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.github.javaparser.ast.observer.Observable.isRegistered.

Was this observer registered?
public <P> void
notifyPropertyChange(ObservableProperty property, P oldValue, P newValue)

public void
public void
register(AstObserver observer, Node.ObserverRegistrationMode mode)

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.
remove
()

Try to remove this node from the parent

public boolean
remove(Node node)

public Node
public void
removeData(DataKey<ResolvedType> key)

Remove data by key.

public void
removeForced()

Forcibly removes this node from the AST.

public boolean
public boolean

Returns:

true if removed, or if the parent isn't set.
replace
(Node node)

Try to replace this node in the parent with the supplied node.

public boolean
replace(Node node, Node replacementNode)

protected void
protected void
setAsParentNodeOf(NodeList<? extends Node> list)

public final Node
setBlockComment(String
to be set
comment
)

Use this to store additional information to this node.

public Node
setComment(final Comment
to be set
comment
)

Use this to store additional information to this node.

public <
The type of data
M
>
void
setData(DataKey<M>
The singleton key for the data
key
,
M
The data object
object
)

Sets data for this node using the given key.

public final Node
setLineComment(String
to be set
comment
)

Use this to store additional information to this node.

public Node
setParentNode(Node
node to be set as parent
newParentNode
)

Implements com.github.javaparser.HasParentNode.setParentNode.

Assign a new parent to this node, removing it from the list of children of the previous parent, if any.
public Node
setParsed(Node.Parsedness parsed)

Used by the parser to flag unparsable nodes.

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.github.javaparser.ast.nodeTypes.NodeWithRange.setRange.

public Node
public static void
public Stream<Node>
stream(Node.TreeTraversal traversal)

Make a stream of nodes using traversal algorithm "traversal".

public Stream<Node>
stream()

Make a stream of nodes using pre-order traversal.

public final String

Returns:

pretty printed source code for this node and its children. Formatting can be configured with Node.setToStringPrettyPrinterConfiguration.
toString
()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public final String

Returns:

pretty printed source code for this node and its children. Formatting can be configured with parameter prettyPrinterConfiguration.
toString
(PrettyPrinterConfiguration prettyPrinterConfiguration)

private Iterable<Node>
private Iterator<Node>
public void
public void
public void
walk(Node.TreeTraversal traversal, Consumer<Node> consumer)

Walks the AST, calling the consumer for every node, with traversal algorithm "traversal".

public void
walk(Consumer<Node> consumer)

Walks the AST, calling the consumer for every node with pre-order traversal.

public <T extends Node> void
walk(Class<T> nodeType, Consumer<T> consumer)

Walks the AST with pre-order traversal, calling the consumer for every node of type "nodeType".

Inherited from java.lang.Object:
finalizegetClassnotifynotifyAllwaitwaitwait