public final class AstQuery<T extends ASTNode>
extends Object
A small, read-only, fluent query API over a Groovy AST subtree.
An AstQuery selects descendant nodes of a root, optionally filtered by type and by an
arbitrary predicate, and exposes terminal operations that collect or test the matches. It is a
declarative alternative to hand-writing a ClassCodeVisitorSupport subclass for the common
"find / collect / detect" cases.
import org.codehaus.groovy.ast.query.AstQuery
import org.codehaus.groovy.ast.expr.MethodCallExpression
import org.codehaus.groovy.ast.builder.AstBuilder
def code = new AstBuilder().buildFromString('foo(); bar()')[0]
assert AstQuery.from(code).descendants(MethodCallExpression).count() == 2
ClosureExpression) and into(Class[])
to opt back in past a default boundary. Nested classes are currently the only default boundary,
so into(ClassNode.class) is the only case in which into(Class[]) has an effect.
T - the node type produced by this query| Type Params | Return Type | Name and description |
|---|---|---|
|
public AstQuery<T> |
andSelf()Includes the root node itself as a candidate (it is excluded by default). |
|
public boolean |
any()
|
|
public long |
count()
|
|
public AstQuery<ASTNode> |
descendants()Selects all descendant nodes regardless of type. |
<U extends ASTNode> |
public AstQuery<U> |
descendants(Class<U> type)Selects descendant nodes that are instances of the given type. |
|
public final AstQuery<ASTNode> |
descendants(Class<? extends ASTNode> types)Selects descendant nodes that are instances of any of the given types. |
|
public Optional<T> |
findFirst()
|
|
public T |
first()
|
|
public void |
forEach(Consumer<? super T> action)Performs the given action for each match in document order. |
|
public void |
forEach(BiConsumer<? super T, AstContext> action)Performs the given action, with enclosing context, for each match in document order. |
|
public static AstQuery<ASTNode> |
from(ASTNode root)Starts a query rooted at the given node. |
|
public final AstQuery<T> |
into(Class<? extends ASTNode> types)Opts the traversal in to descending past a default boundary. |
|
public List<T> |
list()Collects all matches in document order. |
|
public boolean |
none()
|
|
public final AstQuery<T> |
notInto(Class<? extends ASTNode> boundary)Stops the traversal from descending into the subtrees rooted at nodes of the given types. |
|
public Stream<T> |
stream()
|
|
public AstQuery<T> |
where(Predicate<? super T> p)Restricts the selection to nodes satisfying the predicate. |
|
public AstQuery<T> |
where(BiPredicate<? super T, AstContext> p)Restricts the selection to nodes satisfying a predicate that also inspects the enclosing AstContext. |
Includes the root node itself as a candidate (it is excluded by default).
true if at least one node matches; stops at the first match
Selects all descendant nodes regardless of type.
Selects descendant nodes that are instances of the given type.
type - the node type to matchU - the node typeSelects descendant nodes that are instances of any of the given types.
types - the node types to match (logical OR)
null; stops at the first matchPerforms the given action for each match in document order.
action - the actionPerforms the given action, with enclosing context, for each match in document order.
action - the action receiving the node and its AstContextStarts a query rooted at the given node.
root - the subtree root to query; must not be nullroot Opts the traversal in to descending past a default boundary. Currently the only default
boundary is nested classes, so into(ClassNode.class) makes the traversal recurse into
inner classes.
types - the boundary types to descend intoCollects all matches in document order.
true if no node matches; stops at the first matchStops the traversal from descending into the subtrees rooted at nodes of the given types. The boundary nodes themselves are still candidates; only their contents are skipped.
boundary - the boundary types (for example ClosureExpression.class) Restricts the selection to nodes satisfying the predicate. Multiple where calls are
combined with logical AND.
p - the predicate (a Groovy Closure coerces to this functional type)Restricts the selection to nodes satisfying a predicate that also inspects the enclosing AstContext. Combined with logical AND with any other refinements.
p - the contextual predicate (a two-argument Groovy Closure coerces to it)