Package com.tonic.analysis.source.ast
Interface ASTNode
-
- All Known Subinterfaces:
Expression,SourceType,Statement,TypeDecl
- All Known Implementing Classes:
AnnotationExpr,AnnotationValue,ArrayAccessExpr,ArrayInitExpr,ArraySourceType,BinaryExpr,BlockStmt,BreakStmt,CastExpr,ClassDecl,ClassExpr,CompilationUnit,ConstructorDecl,ContinueStmt,DoWhileStmt,DynamicConstantExpr,EnumConstantDecl,EnumDecl,ExprStmt,FieldAccessExpr,FieldDecl,ForEachStmt,ForStmt,GenericSourceType,IfStmt,ImportDecl,InstanceOfExpr,InterfaceDecl,IntersectionSourceType,InvokeDynamicExpr,IRRegionStmt,LabeledStmt,LambdaExpr,LiteralExpr,MethodCallExpr,MethodDecl,MethodRefExpr,NewArrayExpr,NewExpr,ParameterDecl,PrimitiveSourceType,ReferenceSourceType,ReturnStmt,SuperExpr,SwitchExpr,SwitchStmt,SynchronizedStmt,TernaryExpr,ThisExpr,ThrowStmt,TryCatchStmt,UnaryExpr,UnionSourceType,VarDeclStmt,VarRefExpr,VoidSourceType,WhileStmt,WildcardSourceType
public interface ASTNodeBase interface for all AST nodes in the source representation.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description <T> Taccept(SourceVisitor<T> visitor)Accepts a visitor for this node.default ASTNodedeepClone()Creates a deep clone of this node and its entire subtree.default <T extends ASTNode>
List<T>findAll(Class<T> type)Finds all descendants of the specified type.default <T extends ASTNode>
List<T>findAll(Class<T> type, Predicate<T> predicate)Finds all descendants of the specified type matching the predicate.default <T extends ASTNode>
Optional<T>findAncestor(Class<T> type)Finds the first ancestor of the specified type.default <T extends ASTNode>
Optional<T>findFirst(Class<T> type)Finds the first descendant of the specified type.default <T extends ASTNode>
Optional<T>findFirst(Class<T> type, Predicate<T> predicate)Finds the first descendant of the specified type matching the predicate.default List<ASTNode>getChildren()Lists the direct children a node holds, in source order.SourceLocationgetLocation()Gets the source location of this node.ASTNodegetParent()Gets the parent node in the AST tree.default ASTNodegetRoot()Gets the root of the AST tree containing this node.default booleanisDescendantOf(ASTNode ancestor)Checks if this node is a descendant of the specified ancestor.static voidreleaseFormerChild(ASTNode formerChild, ASTNode parent)Clears the parent of a node thatparentno longer holds.default booleanremove()Removes this node from its parent.default booleanreplaceWith(ASTNode replacement)Replaces this node in its parent with a new node.voidsetParent(ASTNode parent)Sets the parent node in the AST tree.default Stream<ASTNode>stream()Returns a stream of all nodes in this subtree (including this node).default <T extends ASTNode>
Stream<T>stream(Class<T> type)Returns a stream of all nodes of the specified type in this subtree.default voidwalk(Consumer<ASTNode> visitor)Walks the entire subtree rooted at this node in pre-order (parent first), calling the visitor on each node.
-
-
-
Method Detail
-
getParent
ASTNode getParent()
Gets the parent node in the AST tree.- Returns:
- the parent node, or null if this is a root node
-
setParent
void setParent(ASTNode parent)
Sets the parent node in the AST tree.- Parameters:
parent- the parent node
-
getLocation
SourceLocation getLocation()
Gets the source location of this node.- Returns:
- the source location
-
accept
<T> T accept(SourceVisitor<T> visitor)
Accepts a visitor for this node.- Type Parameters:
T- the return type of the visitor- Parameters:
visitor- the visitor to accept- Returns:
- the result from the visitor
-
releaseFormerChild
static void releaseFormerChild(ASTNode formerChild, ASTNode parent)
Clears the parent of a node thatparentno longer holds.- Parameters:
formerChild- the node the parent used to hold, may be nullparent- the node that replaced it
-
getChildren
default List<ASTNode> getChildren()
Lists the direct children a node holds, in source order.- Returns:
- the direct children, empty for a leaf
-
walk
default void walk(Consumer<ASTNode> visitor)
Walks the entire subtree rooted at this node in pre-order (parent first), calling the visitor on each node.- Parameters:
visitor- the consumer to call on each node
-
findFirst
default <T extends ASTNode> Optional<T> findFirst(Class<T> type)
Finds the first descendant of the specified type.- Type Parameters:
T- the type of node to find- Parameters:
type- the class of the node to find- Returns:
- an Optional containing the first matching node, or empty if not found
-
findFirst
default <T extends ASTNode> Optional<T> findFirst(Class<T> type, Predicate<T> predicate)
Finds the first descendant of the specified type matching the predicate.- Type Parameters:
T- the type of node to find- Parameters:
type- the class of the node to findpredicate- the condition the node must satisfy- Returns:
- an Optional containing the first matching node, or empty if not found
-
findAll
default <T extends ASTNode> List<T> findAll(Class<T> type)
Finds all descendants of the specified type.- Type Parameters:
T- the type of nodes to find- Parameters:
type- the class of the nodes to find- Returns:
- a list of all matching nodes
-
findAll
default <T extends ASTNode> List<T> findAll(Class<T> type, Predicate<T> predicate)
Finds all descendants of the specified type matching the predicate.- Type Parameters:
T- the type of nodes to find- Parameters:
type- the class of the nodes to findpredicate- the condition the nodes must satisfy- Returns:
- a list of all matching nodes
-
stream
default Stream<ASTNode> stream()
Returns a stream of all nodes in this subtree (including this node).- Returns:
- a stream of all descendant nodes
-
stream
default <T extends ASTNode> Stream<T> stream(Class<T> type)
Returns a stream of all nodes of the specified type in this subtree.- Type Parameters:
T- the type of nodes to stream- Parameters:
type- the class of nodes to include- Returns:
- a stream of matching nodes
-
findAncestor
default <T extends ASTNode> Optional<T> findAncestor(Class<T> type)
Finds the first ancestor of the specified type.- Type Parameters:
T- the type of ancestor to find- Parameters:
type- the class of the ancestor to find- Returns:
- an Optional containing the first matching ancestor, or empty if not found
-
isDescendantOf
default boolean isDescendantOf(ASTNode ancestor)
Checks if this node is a descendant of the specified ancestor.- Parameters:
ancestor- the potential ancestor node- Returns:
- true if this node is a descendant of the ancestor
-
getRoot
default ASTNode getRoot()
Gets the root of the AST tree containing this node.- Returns:
- the root node (the topmost parent)
-
deepClone
default ASTNode deepClone()
Creates a deep clone of this node and its entire subtree.- Returns:
- a deep copy of this node
-
replaceWith
default boolean replaceWith(ASTNode replacement)
Replaces this node in its parent with a new node.- Parameters:
replacement- the node to replace this with- Returns:
- true if replacement was successful
-
remove
default boolean remove()
Removes this node from its parent.- Returns:
- true if removal was successful
-
-