Package: org.codehaus.groovy.macro.matcher

## [Java] Class ContextualClassCodeVisitor

- org.codehaus.groovy.macro.matcher.ContextualClassCodeVisitor

-     public abstract class ContextualClassCodeVisitor
      extends ClassCodeVisitorSupport

  A class code visitor which is capable of remembering the context of the current visit. This makes it easier for subclasses to perform context-dependent transformations, where for example it is necessary to check the parent nodes of an AST node before performing some operations.

  Since:
    2.5.0

- - ### Constructor Summary

    Constructors
    Constructor and description
    `ContextualClassCodeVisitor()` Creates a visitor with an initial root context.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public TreeContext` | `getLastContext()` Returns the most recently popped traversal context.
    `` | `public TreeContext` | `getTreeContext()` Returns the current traversal context.
    `` | `public List<TreeContext>` | `getTreePath()` Returns the current traversal path, starting with the last completed context.
    `` | `public static List<ASTNodePredicate>` | `matchByClass(Class<ASTNode> classes)` Creates class-based predicates for the supplied AST node classes.
    `` | `public List<TreeContext>` | `pathMatches(List<ASTNodePredicate> predicates)` Returns the current path when all supplied predicates match successive parents.
    `` | `public List<TreeContext>` | `pathUpTo(ASTNodePredicate predicate)` Returns the path up to the first node matching the supplied predicate.
    `` | `public List<TreeContext>` | `pathUpTo(Class<ASTNode> node)` Returns the path up to the first node whose class matches `node`.
    `` | `public List<TreeContext>` | `pathUpTo(Class<ASTNode> node, ASTNodePredicate predicate)` Returns the path up to the first node that matches the supplied class and predicate.
    `` | `protected TreeContext` | `popContext()` Pops the current traversal context.
    `` | `protected void` | `pushContext(TreeContext ctx)` Pushes an explicit traversal context.
    `` | `protected void` | `pushContext(ASTNode node)` Pushes a child context for the supplied AST node.
    `` | `public void` | `visitArrayExpression(ArrayExpression expression)` Visits an array expression while tracking traversal context.
    `` | `public void` | `visitAssertStatement(AssertStatement statement)` Visits an assert statement while tracking traversal context.
    `` | `public void` | `visitAttributeExpression(AttributeExpression expression)` Visits an attribute expression while tracking traversal context.
    `` | `public void` | `visitBinaryExpression(BinaryExpression expression)` Visits a binary expression while tracking traversal context.
    `` | `public void` | `visitBitwiseNegationExpression(BitwiseNegationExpression expression)` Visits a bitwise-negation expression while tracking traversal context.
    `` | `public void` | `visitBlockStatement(BlockStatement block)` Visits a block statement while tracking traversal context.
    `` | `public void` | `visitBooleanExpression(BooleanExpression expression)` Visits a boolean expression while tracking traversal context.
    `` | `public void` | `visitBreakStatement(BreakStatement statement)` Visits a break statement while tracking traversal context.
    `` | `public void` | `visitBytecodeExpression(BytecodeExpression cle)` Visits a bytecode expression while tracking traversal context.
    `` | `public void` | `visitCaseStatement(CaseStatement statement)` Visits a case statement while tracking traversal context.
    `` | `public void` | `visitCastExpression(CastExpression expression)` Visits a cast expression while tracking traversal context.
    `` | `public void` | `visitCatchStatement(CatchStatement statement)` Visits a catch statement while tracking traversal context.
    `` | `public void` | `visitClass(ClassNode node)` Visits a class node while tracking traversal context.
    `` | `public void` | `visitClassExpression(ClassExpression expression)` Visits a class expression while tracking traversal context.
    `` | `public void` | `visitClosureExpression(ClosureExpression expression)` Visits a closure expression while tracking traversal context.
    `` | `public void` | `visitClosureListExpression(ClosureListExpression cle)` Visits a closure-list expression while tracking traversal context.
    `` | `public void` | `visitConstantExpression(ConstantExpression expression)` Visits a constant expression while tracking traversal context.
    `` | `public void` | `visitConstructorCallExpression(ConstructorCallExpression call)` Visits a constructor call expression while tracking traversal context.
    `` | `protected void` | `visitConstructorOrMethod(MethodNode node, boolean isConstructor)` Visits a constructor or method while tracking traversal context.
    `` | `public void` | `visitContinueStatement(ContinueStatement statement)` Visits a continue statement while tracking traversal context.
    `` | `public void` | `visitDoWhileLoop(DoWhileStatement loop)` Visits a do-while loop while tracking traversal context.
    `` | `public void` | `visitEmptyStatement(EmptyStatement statement)` Visits an empty statement while tracking traversal context.
    `` | `public void` | `visitExpressionStatement(ExpressionStatement statement)` Visits an expression statement while tracking traversal context.
    `` | `public void` | `visitField(FieldNode node)` Visits a field node while tracking traversal context.
    `` | `public void` | `visitFieldExpression(FieldExpression expression)` Visits a field expression while tracking traversal context.
    `` | `public void` | `visitForLoop(ForStatement forLoop)` Visits a for loop while tracking traversal context.
    `` | `public void` | `visitGStringExpression(GStringExpression expression)` Visits a GString expression while tracking traversal context.
    `` | `public void` | `visitIfElse(IfStatement ifElse)` Visits an if/else statement while tracking traversal context.
    `` | `public void` | `visitImports(ModuleNode node)` Visits module imports while tracking traversal context.
    `` | `public void` | `visitListExpression(ListExpression expression)` Visits a list expression while tracking traversal context.
    `` | `public void` | `visitMapEntryExpression(MapEntryExpression expression)` Visits a map-entry expression while tracking traversal context.
    `` | `public void` | `visitMapExpression(MapExpression expression)` Visits a map expression while tracking traversal context.
    `` | `public void` | `visitMethodCallExpression(MethodCallExpression call)` Visits a method call expression while tracking traversal context.
    `` | `public void` | `visitMethodPointerExpression(MethodPointerExpression expression)` Visits a method-pointer expression while tracking traversal context.
    `` | `public void` | `visitNotExpression(NotExpression expression)` Visits a not expression while tracking traversal context.
    `` | `public void` | `visitPackage(PackageNode node)` Visits a package node while tracking traversal context.
    `` | `public void` | `visitPostfixExpression(PostfixExpression expression)` Visits a postfix expression while tracking traversal context.
    `` | `public void` | `visitPrefixExpression(PrefixExpression expression)` Visits a prefix expression while tracking traversal context.
    `` | `public void` | `visitProperty(PropertyNode node)` Visits a property node while tracking traversal context.
    `` | `public void` | `visitPropertyExpression(PropertyExpression expression)` Visits a property expression while tracking traversal context.
    `` | `public void` | `visitRangeExpression(RangeExpression expression)` Visits a range expression while tracking traversal context.
    `` | `public void` | `visitReturnStatement(ReturnStatement statement)` Visits a return statement while tracking traversal context.
    `` | `public void` | `visitShortTernaryExpression(ElvisOperatorExpression expression)` Visits an Elvis expression while tracking traversal context.
    `` | `public void` | `visitSpreadExpression(SpreadExpression expression)` Visits a spread expression while tracking traversal context.
    `` | `public void` | `visitSpreadMapExpression(SpreadMapExpression expression)` Visits a spread-map expression while tracking traversal context.
    `` | `public void` | `visitStaticMethodCallExpression(StaticMethodCallExpression call)` Visits a static method call expression while tracking traversal context.
    `` | `public void` | `visitSwitch(SwitchStatement statement)` Visits a switch statement while tracking traversal context.
    `` | `public void` | `visitSwitchExpression(SwitchExpression expression)` Visits a first-class switch expression while tracking traversal context (GROOVY-12255).
    `` | `public void` | `visitSynchronizedStatement(SynchronizedStatement statement)` Visits a synchronized statement while tracking traversal context.
    `` | `public void` | `visitTernaryExpression(TernaryExpression expression)` Visits a ternary expression while tracking traversal context.
    `` | `public void` | `visitThrowStatement(ThrowStatement statement)` Visits a throw statement while tracking traversal context.
    `` | `public void` | `visitTryCatchFinally(TryCatchStatement statement)` Visits a try/catch/finally statement while tracking traversal context.
    `` | `public void` | `visitTupleExpression(TupleExpression expression)` Visits a tuple expression while tracking traversal context.
    `` | `public void` | `visitUnaryMinusExpression(UnaryMinusExpression expression)` Visits a unary-minus expression while tracking traversal context.
    `` | `public void` | `visitUnaryPlusExpression(UnaryPlusExpression expression)` Visits a unary-plus expression while tracking traversal context.
    `` | `public void` | `visitVariableExpression(VariableExpression expression)` Visits a variable expression while tracking traversal context.
    `` | `public void` | `visitWhileLoop(WhileStatement loop)` Visits a while loop while tracking traversal context.
    `` | `public void` | `visitYieldStatement(YieldStatement statement)` Visits a yield statement while tracking traversal context (GROOVY-12255).

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class ClassCodeVisitorSupport` | `addError, getSourceUnit, visitAnnotation, visitAnnotations, visitAnnotations, visitAssertStatement, visitBlockStatement, visitBreakStatement, visitCaseStatement, visitCatchStatement, visitClass, visitClassCodeContainer, visitClosureExpression, visitConstructor, visitConstructorOrMethod, visitContinueStatement, visitDeclarationExpression, visitDoWhileLoop, visitExpressionStatement, visitField, visitForLoop, visitIfElse, visitImports, visitMethod, visitObjectInitializerStatements, visitPackage, visitProperty, visitReturnStatement, visitStatement, visitStatementAnnotations, visitSwitch, visitSynchronizedStatement, visitThrowStatement, visitTryCatchFinally, visitWhileLoop, visitYieldStatement`
    `class CodeVisitorSupport` | `afterSwitchCaseStatementsVisited, afterSwitchCaseStatementsVisited, afterSwitchConditionExpressionVisited, afterSwitchConditionExpressionVisited, visitArgumentlistExpression, visitArrayExpression, visitAssertStatement, visitAttributeExpression, visitBinaryExpression, visitBitwiseNegationExpression, visitBlockStatement, visitBooleanExpression, visitBreakStatement, visitBytecodeExpression, visitCaseStatement, visitCastExpression, visitCatchStatement, visitClassExpression, visitClosureExpression, visitClosureListExpression, visitConstantExpression, visitConstructorCallExpression, visitContinueStatement, visitDeclarationExpression, visitDoWhileLoop, visitEmptyStatement, visitExpressionStatement, visitFieldExpression, visitForLoop, visitGStringExpression, visitIfElse, visitLambdaExpression, visitListExpression, visitMapEntryExpression, visitMapExpression, visitMethodCallExpression, visitMethodPointerExpression, visitMethodReferenceExpression, visitNotExpression, visitPostfixExpression, visitPrefixExpression, visitPropertyExpression, visitRangeExpression, visitReturnStatement, visitShortTernaryExpression, visitSpreadExpression, visitSpreadMapExpression, visitStaticMethodCallExpression, visitSwitch, visitSwitchExpression, visitSynchronizedStatement, visitTernaryExpression, visitThrowStatement, visitTryCatchFinally, visitTupleExpression, visitUnaryMinusExpression, visitUnaryPlusExpression, visitVariableExpression, visitWhileLoop, visitYieldStatement`

- - ### Constructor Detail

    - #### public ContextualClassCodeVisitor()

      Creates a visitor with an initial root context.

  - ### Method Detail

    - #### public TreeContext getLastContext()

      Returns the most recently popped traversal context.

      Returns:
        the last completed context

    - #### public TreeContext getTreeContext()

      Returns the current traversal context.

      Returns:
        the current context, or `null`

    - #### public List<TreeContext> getTreePath()

      Returns the current traversal path, starting with the last completed context.

      Returns:
        the current tree path

    - #### public static List<ASTNodePredicate> matchByClass(Class<ASTNode> classes)

      Creates class-based predicates for the supplied AST node classes.

      Parameters:
        `classes` - the classes to match
      Returns:
        the corresponding predicates

    - #### public List<TreeContext> pathMatches(List<ASTNodePredicate> predicates)

      Returns the current path when all supplied predicates match successive parents.

      Parameters:
        `predicates` - the predicates to match against the path
      Returns:
        the matching path, or an empty list

    - #### public List<TreeContext> pathUpTo(ASTNodePredicate predicate)

      Returns the path up to the first node matching the supplied predicate.

      Parameters:
        `predicate` - the predicate that terminates the path
      Returns:
        the matching path, or an empty list

    - #### public List<TreeContext> pathUpTo(Class<ASTNode> node)

      Returns the path up to the first node whose class matches `node`.

      Parameters:
        `node` - the node class that terminates the path
      Returns:
        the matching path, or an empty list

    - #### public List<TreeContext> pathUpTo(Class<ASTNode> node, ASTNodePredicate predicate)

      Returns the path up to the first node that matches the supplied class and predicate.

      Parameters:
        `node` - the node class that terminates the path, or `null`
        `predicate` - the predicate that terminates the path, or `null`
      Returns:
        the matching path, or an empty list

    - #### protected TreeContext popContext()

      Pops the current traversal context.

      Returns:
        the popped context

    - #### protected void pushContext(TreeContext ctx)

      Pushes an explicit traversal context.

      Parameters:
        `ctx` - the context to push

    - #### protected void pushContext(ASTNode node)

      Pushes a child context for the supplied AST node.

      Parameters:
        `node` - the node to enter

    - #### public void visitArrayExpression(ArrayExpression expression)

      Visits an array expression while tracking traversal context.

    - #### public void visitAssertStatement(AssertStatement statement)

      Visits an assert statement while tracking traversal context.

    - #### public void visitAttributeExpression(AttributeExpression expression)

      Visits an attribute expression while tracking traversal context.

    - #### public void visitBinaryExpression(BinaryExpression expression)

      Visits a binary expression while tracking traversal context.

    - #### public void visitBitwiseNegationExpression(BitwiseNegationExpression expression)

      Visits a bitwise-negation expression while tracking traversal context.

    - #### public void visitBlockStatement(BlockStatement block)

      Visits a block statement while tracking traversal context.

    - #### public void visitBooleanExpression(BooleanExpression expression)

      Visits a boolean expression while tracking traversal context.

    - #### public void visitBreakStatement(BreakStatement statement)

      Visits a break statement while tracking traversal context.

    - #### public void visitBytecodeExpression(BytecodeExpression cle)

      Visits a bytecode expression while tracking traversal context.

    - #### public void visitCaseStatement(CaseStatement statement)

      Visits a case statement while tracking traversal context.

    - #### public void visitCastExpression(CastExpression expression)

      Visits a cast expression while tracking traversal context.

    - #### public void visitCatchStatement(CatchStatement statement)

      Visits a catch statement while tracking traversal context.

    - #### public void visitClass(ClassNode node)

      Visits a class node while tracking traversal context.

    - #### public void visitClassExpression(ClassExpression expression)

      Visits a class expression while tracking traversal context.

    - #### public void visitClosureExpression(ClosureExpression expression)

      Visits a closure expression while tracking traversal context.

    - #### public void visitClosureListExpression(ClosureListExpression cle)

      Visits a closure-list expression while tracking traversal context.

    - #### public void visitConstantExpression(ConstantExpression expression)

      Visits a constant expression while tracking traversal context.

    - #### public void visitConstructorCallExpression(ConstructorCallExpression call)

      Visits a constructor call expression while tracking traversal context.

    - #### protected void visitConstructorOrMethod(MethodNode node, boolean isConstructor)

      Visits a constructor or method while tracking traversal context.

    - #### public void visitContinueStatement(ContinueStatement statement)

      Visits a continue statement while tracking traversal context.

    - #### public void visitDoWhileLoop(DoWhileStatement loop)

      Visits a do-while loop while tracking traversal context.

    - #### public void visitEmptyStatement(EmptyStatement statement)

      Visits an empty statement while tracking traversal context.

    - #### public void visitExpressionStatement(ExpressionStatement statement)

      Visits an expression statement while tracking traversal context.

    - #### public void visitField(FieldNode node)

      Visits a field node while tracking traversal context.

    - #### public void visitFieldExpression(FieldExpression expression)

      Visits a field expression while tracking traversal context.

    - #### public void visitForLoop(ForStatement forLoop)

      Visits a for loop while tracking traversal context.

    - #### public void visitGStringExpression(GStringExpression expression)

      Visits a GString expression while tracking traversal context.

    - #### public void visitIfElse(IfStatement ifElse)

      Visits an if/else statement while tracking traversal context.

    - #### public void visitImports(ModuleNode node)

      Visits module imports while tracking traversal context.

    - #### public void visitListExpression(ListExpression expression)

      Visits a list expression while tracking traversal context.

    - #### public void visitMapEntryExpression(MapEntryExpression expression)

      Visits a map-entry expression while tracking traversal context.

    - #### public void visitMapExpression(MapExpression expression)

      Visits a map expression while tracking traversal context.

    - #### public void visitMethodCallExpression(MethodCallExpression call)

      Visits a method call expression while tracking traversal context.

    - #### public void visitMethodPointerExpression(MethodPointerExpression expression)

      Visits a method-pointer expression while tracking traversal context.

    - #### public void visitNotExpression(NotExpression expression)

      Visits a not expression while tracking traversal context.

    - #### public void visitPackage(PackageNode node)

      Visits a package node while tracking traversal context.

    - #### public void visitPostfixExpression(PostfixExpression expression)

      Visits a postfix expression while tracking traversal context.

    - #### public void visitPrefixExpression(PrefixExpression expression)

      Visits a prefix expression while tracking traversal context.

    - #### public void visitProperty(PropertyNode node)

      Visits a property node while tracking traversal context.

    - #### public void visitPropertyExpression(PropertyExpression expression)

      Visits a property expression while tracking traversal context.

    - #### public void visitRangeExpression(RangeExpression expression)

      Visits a range expression while tracking traversal context.

    - #### public void visitReturnStatement(ReturnStatement statement)

      Visits a return statement while tracking traversal context.

    - #### public void visitShortTernaryExpression(ElvisOperatorExpression expression)

      Visits an Elvis expression while tracking traversal context.

    - #### public void visitSpreadExpression(SpreadExpression expression)

      Visits a spread expression while tracking traversal context.

    - #### public void visitSpreadMapExpression(SpreadMapExpression expression)

      Visits a spread-map expression while tracking traversal context.

    - #### public void visitStaticMethodCallExpression(StaticMethodCallExpression call)

      Visits a static method call expression while tracking traversal context.

    - #### public void visitSwitch(SwitchStatement statement)

      Visits a switch statement while tracking traversal context.

    - #### public void visitSwitchExpression(SwitchExpression expression)

      Visits a first-class switch expression while tracking traversal context (GROOVY-12255).

    - #### public void visitSynchronizedStatement(SynchronizedStatement statement)

      Visits a synchronized statement while tracking traversal context.

    - #### public void visitTernaryExpression(TernaryExpression expression)

      Visits a ternary expression while tracking traversal context.

    - #### public void visitThrowStatement(ThrowStatement statement)

      Visits a throw statement while tracking traversal context.

    - #### public void visitTryCatchFinally(TryCatchStatement statement)

      Visits a try/catch/finally statement while tracking traversal context.

    - #### public void visitTupleExpression(TupleExpression expression)

      Visits a tuple expression while tracking traversal context.

    - #### public void visitUnaryMinusExpression(UnaryMinusExpression expression)

      Visits a unary-minus expression while tracking traversal context.

    - #### public void visitUnaryPlusExpression(UnaryPlusExpression expression)

      Visits a unary-plus expression while tracking traversal context.

    - #### public void visitVariableExpression(VariableExpression expression)

      Visits a variable expression while tracking traversal context.

    - #### public void visitWhileLoop(WhileStatement loop)

      Visits a while loop while tracking traversal context.

    - #### public void visitYieldStatement(YieldStatement statement)

      Visits a yield statement while tracking traversal context (GROOVY-12255).