public abstract class Node extends Object implements Cloneable, HasParentNode<Node>, Visitable, NodeWithRange<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.ObserverRegistrationMode
Different registration mode for observers on nodes.
|
Modifier and Type | Field and Description |
---|---|
static int |
ABSOLUTE_BEGIN_LINE |
static int |
ABSOLUTE_END_LINE |
static Comparator<NodeWithRange<?>> |
NODE_BY_BEGIN_POSITION
This can be used to sort nodes on position.
|
protected static PrettyPrinterConfiguration |
prettyPrinterNoCommentsConfiguration |
Modifier and Type | Method and Description |
---|---|
void |
addOrphanComment(Comment comment) |
Node |
clone() |
protected void |
customInitialization()
Called in every constructor for node specific code.
|
boolean |
equals(Object obj) |
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)
Recursively finds all nodes of a certain type.
|
Optional<Comment> |
getComment()
This is a comment associated with this node.
|
<M> M |
getData(DataKey<M> key)
Gets data for this component using the given key.
|
NodeMetaModel |
getMetaModel() |
List<NodeList<?>> |
getNodeLists()
The list of NodeLists owned by this node.
|
<N extends Node> |
getNodesByType(Class<N> clazz)
Deprecated.
use getChildNodesByType
|
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()
Return the parent node or null, if no parent is set.
|
Node |
getParentNodeForChildren()
this for everything except NodeLists.
|
Optional<Range> |
getRange() |
boolean |
hasComment()
Deprecated.
use getComment().isPresent()
|
int |
hashCode() |
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 |
removeForced()
Forcibly removes this node from the AST.
|
boolean |
removeOrphanComment(Comment comment) |
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 component using the given key.
|
Node |
setLineComment(String comment)
Use this to store additional information to this node.
|
Node |
setParentNode(Node parentNode)
Assign a new parent to this node, removing it
from the list of children of the previous parent, if any.
|
Node |
setRange(Range range) |
String |
toString()
Return the String representation of this node.
|
String |
toString(PrettyPrinterConfiguration prettyPrinterConfiguration) |
void |
tryAddImportToParentCompilationUnit(Class<?> clazz) |
void |
unregister(AstObserver observer)
Unregister an observer.
|
finalize, getClass, notify, notifyAll, wait, wait, wait
getAncestorOfType, setAsParentNodeOf, setAsParentNodeOf
containsWithin, getBegin, getEnd, isPositionedAfter, isPositionedBefore
public static Comparator<NodeWithRange<?>> NODE_BY_BEGIN_POSITION
protected static final PrettyPrinterConfiguration prettyPrinterNoCommentsConfiguration
public static final int ABSOLUTE_BEGIN_LINE
public static final int ABSOLUTE_END_LINE
public Node(Range range)
protected void customInitialization()
@Generated(value="com.github.javaparser.generator.core.node.PropertyGenerator") public Optional<Comment> getComment()
public Optional<Range> getRange()
getRange
in interface NodeWithRange<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 final 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(PrettyPrinterConfiguration prettyPrinterConfiguration)
public Optional<Node> getParentNode()
HasParentNode
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 parentNode)
setParentNode
in interface HasParentNode<Node>
parentNode
- node to be set as parent@Deprecated public boolean hasComment()
public void tryAddImportToParentCompilationUnit(Class<?> clazz)
public <N extends Node> List<N> getChildNodesByType(Class<N> clazz)
clazz
- the type of node to find.@Deprecated public <N extends Node> List<N> getNodesByType(Class<N> clazz)
public <M> M getData(DataKey<M> key)
M
- The type of the data.key
- The key for the dataDataKey
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 remove()
RuntimeException
- if it fails in an unexpected waypublic void removeForced()
public Node getParentNodeForChildren()
HasParentNode
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
@Generated(value="com.github.javaparser.generator.core.node.RemoveMethodGenerator") public boolean remove(Node node)
@Generated(value="com.github.javaparser.generator.core.node.RemoveMethodGenerator") public Node removeComment()
@Generated(value="com.github.javaparser.generator.core.node.CloneGenerator") public Node clone()
@Generated(value="com.github.javaparser.generator.core.node.GetMetaModelGenerator") public NodeMetaModel getMetaModel()
Copyright © 2007–2017. All rights reserved.