Package org.codehaus.groovy.ast.query

# Interface AstContext

public interface AstContext
The enclosing context of a candidate node during an `AstQuery` traversal.

A `AstContext` is supplied to the contextual `where`/`forEach` overloads so a predicate or consumer can take the surrounding structure of a node into account (for example, "a `VariableExpression` that occurs inside a static method"). The information is valid only for the duration of the callback; do not retain it.

Since:
  6.0.0

- ## Method Summary

  Modifier and Type
  Method
  Description
  `List<ASTNode>`
  `ancestors()`
  Returns the ancestors of the candidate node, nearest first (the immediate parent is the first element).
  `ClassNode`
  `enclosingClass()`
  Returns the nearest enclosing class, or `null` if the candidate is not inside one.
  `MethodNode`
  `enclosingMethod()`
  Returns the nearest enclosing method or constructor, or `null` if the candidate is not inside one.
  `ASTNode`
  `parent()`
  Returns the immediate parent of the candidate node, or `null` if the candidate is the query root.

- ## Method Details

  - ### parent

    ASTNode parent()
    Returns the immediate parent of the candidate node, or `null` if the candidate is the query root.
    Returns:
      the parent node or `null`

  - ### ancestors

    List<ASTNode> ancestors()
    Returns the ancestors of the candidate node, nearest first (the immediate parent is the first element). The candidate node itself is not included.
    Returns:
      an immutable snapshot of the ancestor chain

  - ### enclosingMethod

    MethodNode enclosingMethod()
    Returns the nearest enclosing method or constructor, or `null` if the candidate is not inside one.
    Returns:
      the enclosing `MethodNode` or `null`

  - ### enclosingClass

    ClassNode enclosingClass()
    Returns the nearest enclosing class, or `null` if the candidate is not inside one.
    Returns:
      the enclosing `ClassNode` or `null`