TreePathScanner (Java SE 26 & JDK 26)
Description: declaration: module: jdk.compiler, package: com.sun.source.util, class: TreePathScanner
Keywords: com.sun.source.util.TreePathScanner class

# Class TreePathScanner<R,P>

java.lang.Object
com.sun.source.util.TreeScanner<R,P>
com.sun.source.util.TreePathScanner<R,P>

Type Parameters:
  `R` - the return type of this visitor's methods. Use `Void` for visitors that do not need to return results.
  `P` - the type of the additional parameter to this visitor's methods. Use `Void` for visitors that do not need an additional parameter.

All Implemented Interfaces:
  `TreeVisitor<R,P>`

public class TreePathScanner<R,P> extends TreeScanner<R,P>
A TreeVisitor that visits all the child tree nodes, and provides support for maintaining a path for the parent nodes. To visit nodes of a particular type, just override the corresponding visitorXYZ method. Inside your method, call super.visitXYZ to visit descendant nodes.

API Note:
  In order to initialize the "current path", the scan must be started by calling one of the `scan` methods.
Since:
  1.6

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `TreePathScanner()`
  Constructs a `TreePathScanner`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `TreePath`
  `getCurrentPath()`
  Returns the current path for the node, as built up by the currently active set of scan calls.
  `R`
  `scan(Tree tree, P p)`
  Scans a single node.
  `R`
  `scan(TreePath path, P p)`
  Scans a tree from a position identified by a TreePath.

  ### Methods declared in class TreeScanner

  `reduce, scan, visitAnnotatedType, visitAnnotation, visitAnyPattern, visitArrayAccess, visitArrayType, visitAssert, visitAssignment, visitBinary, visitBindingPattern, visitBlock, visitBreak, visitCase, visitCatch, visitClass, visitCompilationUnit, visitCompoundAssignment, visitConditionalExpression, visitConstantCaseLabel, visitContinue, visitDeconstructionPattern, visitDefaultCaseLabel, visitDoWhileLoop, visitEmptyStatement, visitEnhancedForLoop, visitErroneous, visitExports, visitExpressionStatement, visitForLoop, visitIdentifier, visitIf, visitImport, visitInstanceOf, visitIntersectionType, visitLabeledStatement, visitLambdaExpression, visitLiteral, visitMemberReference, visitMemberSelect, visitMethod, visitMethodInvocation, visitModifiers, visitModule, visitNewArray, visitNewClass, visitOpens, visitOther, visitPackage, visitParameterizedType, visitParenthesized, visitPatternCaseLabel, visitPrimitiveType, visitProvides, visitRequires, visitReturn, visitSwitch, visitSwitchExpression, visitSynchronized, visitThrow, visitTry, visitTypeCast, visitTypeParameter, visitUnary, visitUnionType, visitUses, visitVariable, visitWhileLoop, visitWildcard, visitYield`
  Modifier and Type
  Method
  Description
  `R`
  `reduce(R r1, R r2)`
  Reduces two results into a combined result.
  `R`
  `scan(Iterable<? extends Tree> nodes, P p)`
  Scans a sequence of nodes.
  `R`
  `visitAnnotatedType(AnnotatedTypeTree node, P p)`
  Visits an `AnnotatedTypeTree` node.
  `R`
  `visitAnnotation(AnnotationTree node, P p)`
  Visits an `AnnotatedTree` node.
  `R`
  `visitAnyPattern(AnyPatternTree node, P p)`
  Visits a `AnyPatternTree` node.
  `R`
  `visitArrayAccess(ArrayAccessTree node, P p)`
  Visits an `ArrayAccessTree` node.
  `R`
  `visitArrayType(ArrayTypeTree node, P p)`
  Visits an `ArrayTypeTree` node.
  `R`
  `visitAssert(AssertTree node, P p)`
  Visits an `AssertTree` node.
  `R`
  `visitAssignment(AssignmentTree node, P p)`
  Visits an `AssignmentTree` node.
  `R`
  `visitBinary(BinaryTree node, P p)`
  Visits a `BinaryTree` node.
  `R`
  `visitBindingPattern(BindingPatternTree node, P p)`
  Visits a `BindingPatternTree` node.
  `R`
  `visitBlock(BlockTree node, P p)`
  Visits a `BlockTree` node.
  `R`
  `visitBreak(BreakTree node, P p)`
  Visits a `BreakTree` node.
  `R`
  `visitCase(CaseTree node, P p)`
  Visits a `CaseTree` node.
  `R`
  `visitCatch(CatchTree node, P p)`
  Visits a `CatchTree` node.
  `R`
  `visitClass(ClassTree node, P p)`
  Visits a `ClassTree` node.
  `R`
  `visitCompilationUnit(CompilationUnitTree node, P p)`
  Visits a `CompilationUnitTree` node.
  `R`
  `visitCompoundAssignment(CompoundAssignmentTree node, P p)`
  Visits a `CompoundAssignmentTree` node.
  `R`
  `visitConditionalExpression(ConditionalExpressionTree node, P p)`
  Visits a `ConditionalExpressionTree` node.
  `R`
  `visitConstantCaseLabel(ConstantCaseLabelTree node, P p)`
  Visits a `ConstantCaseLabelTree` node.
  `R`
  `visitContinue(ContinueTree node, P p)`
  Visits a `ContinueTree` node.
  `R`
  `visitDeconstructionPattern(DeconstructionPatternTree node, P p)`
  Visits a `DeconstructionPatternTree` node.
  `R`
  `visitDefaultCaseLabel(DefaultCaseLabelTree node, P p)`
  Visits a `DefaultCaseLabelTree` node.
  `R`
  `visitDoWhileLoop(DoWhileLoopTree node, P p)`
  Visits a `DoWhileTree` node.
  `R`
  `visitEmptyStatement(EmptyStatementTree node, P p)`
  Visits an `EmptyStatementTree` node.
  `R`
  `visitEnhancedForLoop(EnhancedForLoopTree node, P p)`
  Visits an `EnhancedForLoopTree` node.
  `R`
  `visitErroneous(ErroneousTree node, P p)`
  Visits an `ErroneousTree` node.
  `R`
  `visitExports(ExportsTree node, P p)`
  Visits an `ExportsTree` node.
  `R`
  `visitExpressionStatement(ExpressionStatementTree node, P p)`
  Visits an `ExpressionStatementTree` node.
  `R`
  `visitForLoop(ForLoopTree node, P p)`
  Visits a `ForLoopTree` node.
  `R`
  `visitIdentifier(IdentifierTree node, P p)`
  Visits an `IdentifierTree` node.
  `R`
  `visitIf(IfTree node, P p)`
  Visits an `IfTree` node.
  `R`
  `visitImport(ImportTree node, P p)`
  Visits an `ImportTree` node.
  `R`
  `visitInstanceOf(InstanceOfTree node, P p)`
  Visits an `InstanceOfTree` node.
  `R`
  `visitIntersectionType(IntersectionTypeTree node, P p)`
  Visits an `IntersectionTypeTree` node.
  `R`
  `visitLabeledStatement(LabeledStatementTree node, P p)`
  Visits a `LabeledStatementTree` node.
  `R`
  `visitLambdaExpression(LambdaExpressionTree node, P p)`
  Visits a `LambdaExpressionTree` node.
  `R`
  `visitLiteral(LiteralTree node, P p)`
  Visits a `LiteralTree` node.
  `R`
  `visitMemberReference(MemberReferenceTree node, P p)`
  Visits a `MemberReferenceTree` node.
  `R`
  `visitMemberSelect(MemberSelectTree node, P p)`
  Visits a `MemberSelectTree` node.
  `R`
  `visitMethod(MethodTree node, P p)`
  Visits a `MethodTree` node.
  `R`
  `visitMethodInvocation(MethodInvocationTree node, P p)`
  Visits a `MethodInvocationTree` node.
  `R`
  `visitModifiers(ModifiersTree node, P p)`
  Visits a `ModifiersTree` node.
  `R`
  `visitModule(ModuleTree node, P p)`
  Visits a `ModuleTree` node.
  `R`
  `visitNewArray(NewArrayTree node, P p)`
  Visits a `NewArrayTree` node.
  `R`
  `visitNewClass(NewClassTree node, P p)`
  Visits a `NewClassTree` node.
  `R`
  `visitOpens(OpensTree node, P p)`
  Visits an `OpensTree` node.
  `R`
  `visitOther(Tree node, P p)`
  Visits an unknown type of `Tree` node.
  `R`
  `visitPackage(PackageTree node, P p)`
  Visits a `PackageTree` node.
  `R`
  `visitParameterizedType(ParameterizedTypeTree node, P p)`
  Visits a `ParameterizedTypeTree` node.
  `R`
  `visitParenthesized(ParenthesizedTree node, P p)`
  Visits a `ParenthesizedTree` node.
  `R`
  `visitPatternCaseLabel(PatternCaseLabelTree node, P p)`
  Visits a `PatternCaseLabelTree` node.
  `R`
  `visitPrimitiveType(PrimitiveTypeTree node, P p)`
  Visits a `PrimitiveTypeTree` node.
  `R`
  `visitProvides(ProvidesTree node, P p)`
  Visits a `ProvidesTree` node.
  `R`
  `visitRequires(RequiresTree node, P p)`
  Visits a `RequiresTree` node.
  `R`
  `visitReturn(ReturnTree node, P p)`
  Visits a `ReturnTree` node.
  `R`
  `visitSwitch(SwitchTree node, P p)`
  Visits a `SwitchTree` node.
  `R`
  `visitSwitchExpression(SwitchExpressionTree node, P p)`
  Visits a `SwitchExpressionTree` node.
  `R`
  `visitSynchronized(SynchronizedTree node, P p)`
  Visits a `SynchronizedTree` node.
  `R`
  `visitThrow(ThrowTree node, P p)`
  Visits a `ThrowTree` node.
  `R`
  `visitTry(TryTree node, P p)`
  Visits a `TryTree` node.
  `R`
  `visitTypeCast(TypeCastTree node, P p)`
  Visits a `TypeCastTree` node.
  `R`
  `visitTypeParameter(TypeParameterTree node, P p)`
  Visits a `TypeParameterTree` node.
  `R`
  `visitUnary(UnaryTree node, P p)`
  Visits a `UnaryTree` node.
  `R`
  `visitUnionType(UnionTypeTree node, P p)`
  Visits a `UnionTypeTree` node.
  `R`
  `visitUses(UsesTree node, P p)`
  Visits a `UsesTree` node.
  `R`
  `visitVariable(VariableTree node, P p)`
  Visits a `VariableTree` node.
  `R`
  `visitWhileLoop(WhileLoopTree node, P p)`
  Visits a `WhileLoopTree` node.
  `R`
  `visitWildcard(WildcardTree node, P p)`
  Visits a `WildcardTypeTree` node.
  `R`
  `visitYield(YieldTree node, P p)`
  Visits a `YieldTree` node.

  ### Methods declared in class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  Creates and returns a copy of this object.
  `boolean`
  `equals(Object obj)`
  Indicates whether some other object is "equal to" this one.
  `protected void`
  `finalize()`
  Deprecated, for removal: This API element is subject to removal in a future version.
  Finalization is deprecated and subject to removal in a future release.
  `final Class<?>`
  `getClass()`
  Returns the runtime class of this `Object`.
  `int`
  `hashCode()`
  Returns a hash code value for this object.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `String`
  `toString()`
  Returns a string representation of the object.
  `final void`
  `wait()`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted.
  `final void`
  `wait(long timeoutMillis)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
  `final void`
  `wait(long timeoutMillis, int nanos)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

- ## Constructor Details

  - ### TreePathScanner

    public TreePathScanner()
    Constructs a `TreePathScanner`.

- ## Method Details

  - ### scan

    public R scan(TreePath path, P p)
    Scans a tree from a position identified by a TreePath.
    Parameters:
      `path` - the path identifying the node to be scanned
      `p` - a parameter value passed to visit methods
    Returns:
      the result value from the visit method

  - ### scan

    public R scan(Tree tree, P p)
    Scans a single node. The current path is updated for the duration of the scan.
    Overrides:
      `scan` in class `TreeScanner<R,P>`
    API Note:
      This method should normally only be called by the scanner's `visit` methods, as part of an ongoing scan initiated by `scan(TreePath, P)`. The one exception is that it may also be called to initiate a full scan of a `CompilationUnitTree`.
    Parameters:
      `tree` - the node to be scanned
      `p` - a parameter value passed to the visit method
    Returns:
      the result value from the visit method

  - ### getCurrentPath

    public TreePath getCurrentPath()
    Returns the current path for the node, as built up by the currently active set of scan calls.
    Returns:
      the current path