TreeScanner (Compiler Tree API )
Keywords: com.sun.source.util.TreeScanner class
Date: 2020-03-26

com.sun.source.util

## Class TreeScanner<R,P>

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

- All Implemented Interfaces:
    TreeVisitor<R,P>
  Direct Known Subclasses:
    TreePathScanner

      @Exported
      public class TreeScanner<R,P>
      extends Object
      implements TreeVisitor<R,P>

  A TreeVisitor that visits all the child tree nodes. To visit nodes of a particular type, just override the corresponding visitXYZ method. Inside your method, call super.visitXYZ to visit descendant nodes.

  The default implementation of the visitXYZ methods will determine a result as follows:

  - If the node being visited has no children, the result will be null.
  - If the node being visited has one child, the result will be the result of calling `scan` on that child. The child may be a simple node or itself a list of nodes.
  - If the node being visited has more than one child, the result will be determined by calling `scan` each child in turn, and then combining the result of each scan after the first with the cumulative result so far, as determined by the `reduce(R, R)` method. Each child may be either a simple node of a list of nodes. The default behavior of the `reduce` method is such that the result of the visitXYZ method will be the result of the last child scanned.

  Here is an example to count the number of identifier nodes in a tree:

         class CountIdentifiers extends TreeScanner<Integer,Void> {
             @Override
            public Integer visitIdentifier(IdentifierTree node, Void p) {
                return 1;
            }
             @Override
            public Integer reduce(Integer r1, Integer r2) {
                return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);
            }
         }

  Since:
    1.6

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `TreeScanner()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `R` | `reduce(R r1, R r2)` Reduces two results into a combined result.
    `R` | `scan(Iterable<? extends Tree> nodes, P p)` Scan a list of nodes.
    `R` | `scan(Tree node, P p)` Scan a single node.
    `R` | `visitAnnotatedType(AnnotatedTypeTree node, P p)`
    `R` | `visitAnnotation(AnnotationTree node, P p)`
    `R` | `visitArrayAccess(ArrayAccessTree node, P p)`
    `R` | `visitArrayType(ArrayTypeTree node, P p)`
    `R` | `visitAssert(AssertTree node, P p)`
    `R` | `visitAssignment(AssignmentTree node, P p)`
    `R` | `visitBinary(BinaryTree node, P p)`
    `R` | `visitBlock(BlockTree node, P p)`
    `R` | `visitBreak(BreakTree node, P p)`
    `R` | `visitCase(CaseTree node, P p)`
    `R` | `visitCatch(CatchTree node, P p)`
    `R` | `visitClass(ClassTree node, P p)`
    `R` | `visitCompilationUnit(CompilationUnitTree node, P p)`
    `R` | `visitCompoundAssignment(CompoundAssignmentTree node, P p)`
    `R` | `visitConditionalExpression(ConditionalExpressionTree node, P p)`
    `R` | `visitContinue(ContinueTree node, P p)`
    `R` | `visitDoWhileLoop(DoWhileLoopTree node, P p)`
    `R` | `visitEmptyStatement(EmptyStatementTree node, P p)`
    `R` | `visitEnhancedForLoop(EnhancedForLoopTree node, P p)`
    `R` | `visitErroneous(ErroneousTree node, P p)`
    `R` | `visitExpressionStatement(ExpressionStatementTree node, P p)`
    `R` | `visitForLoop(ForLoopTree node, P p)`
    `R` | `visitIdentifier(IdentifierTree node, P p)`
    `R` | `visitIf(IfTree node, P p)`
    `R` | `visitImport(ImportTree node, P p)`
    `R` | `visitInstanceOf(InstanceOfTree node, P p)`
    `R` | `visitIntersectionType(IntersectionTypeTree node, P p)`
    `R` | `visitLabeledStatement(LabeledStatementTree node, P p)`
    `R` | `visitLambdaExpression(LambdaExpressionTree node, P p)`
    `R` | `visitLiteral(LiteralTree node, P p)`
    `R` | `visitMemberReference(MemberReferenceTree node, P p)`
    `R` | `visitMemberSelect(MemberSelectTree node, P p)`
    `R` | `visitMethod(MethodTree node, P p)`
    `R` | `visitMethodInvocation(MethodInvocationTree node, P p)`
    `R` | `visitModifiers(ModifiersTree node, P p)`
    `R` | `visitNewArray(NewArrayTree node, P p)`
    `R` | `visitNewClass(NewClassTree node, P p)`
    `R` | `visitOther(Tree node, P p)`
    `R` | `visitParameterizedType(ParameterizedTypeTree node, P p)`
    `R` | `visitParenthesized(ParenthesizedTree node, P p)`
    `R` | `visitPrimitiveType(PrimitiveTypeTree node, P p)`
    `R` | `visitReturn(ReturnTree node, P p)`
    `R` | `visitSwitch(SwitchTree node, P p)`
    `R` | `visitSynchronized(SynchronizedTree node, P p)`
    `R` | `visitThrow(ThrowTree node, P p)`
    `R` | `visitTry(TryTree node, P p)`
    `R` | `visitTypeCast(TypeCastTree node, P p)`
    `R` | `visitTypeParameter(TypeParameterTree node, P p)`
    `R` | `visitUnary(UnaryTree node, P p)`
    `R` | `visitUnionType(UnionTypeTree node, P p)`
    `R` | `visitVariable(VariableTree node, P p)`
    `R` | `visitWhileLoop(WhileLoopTree node, P p)`
    `R` | `visitWildcard(WildcardTree node, P p)`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### TreeScanner

          public TreeScanner()

  - ### Method Detail

    - #### scan

          public R scan(Tree node,
                        P p)

      Scan a single node.

    - #### scan

          public R scan(Iterable<? extends Tree> nodes,
                        P p)

      Scan a list of nodes.

    - #### reduce

          public R reduce(R r1,
                          R r2)

      Reduces two results into a combined result. The default implementation is to return the first parameter. The general contract of the method is that it may take any action whatsoever.

    - #### visitCompilationUnit

          public R visitCompilationUnit(CompilationUnitTree node,
                                        P p)

      Specified by:
        `visitCompilationUnit` in interface `TreeVisitor<R,P>`

    - #### visitImport

          public R visitImport(ImportTree node,
                               P p)

      Specified by:
        `visitImport` in interface `TreeVisitor<R,P>`

    - #### visitClass

          public R visitClass(ClassTree node,
                              P p)

      Specified by:
        `visitClass` in interface `TreeVisitor<R,P>`

    - #### visitMethod

          public R visitMethod(MethodTree node,
                               P p)

      Specified by:
        `visitMethod` in interface `TreeVisitor<R,P>`

    - #### visitVariable

          public R visitVariable(VariableTree node,
                                 P p)

      Specified by:
        `visitVariable` in interface `TreeVisitor<R,P>`

    - #### visitEmptyStatement

          public R visitEmptyStatement(EmptyStatementTree node,
                                       P p)

      Specified by:
        `visitEmptyStatement` in interface `TreeVisitor<R,P>`

    - #### visitBlock

          public R visitBlock(BlockTree node,
                              P p)

      Specified by:
        `visitBlock` in interface `TreeVisitor<R,P>`

    - #### visitDoWhileLoop

          public R visitDoWhileLoop(DoWhileLoopTree node,
                                    P p)

      Specified by:
        `visitDoWhileLoop` in interface `TreeVisitor<R,P>`

    - #### visitWhileLoop

          public R visitWhileLoop(WhileLoopTree node,
                                  P p)

      Specified by:
        `visitWhileLoop` in interface `TreeVisitor<R,P>`

    - #### visitForLoop

          public R visitForLoop(ForLoopTree node,
                                P p)

      Specified by:
        `visitForLoop` in interface `TreeVisitor<R,P>`

    - #### visitEnhancedForLoop

          public R visitEnhancedForLoop(EnhancedForLoopTree node,
                                        P p)

      Specified by:
        `visitEnhancedForLoop` in interface `TreeVisitor<R,P>`

    - #### visitLabeledStatement

          public R visitLabeledStatement(LabeledStatementTree node,
                                         P p)

      Specified by:
        `visitLabeledStatement` in interface `TreeVisitor<R,P>`

    - #### visitSwitch

          public R visitSwitch(SwitchTree node,
                               P p)

      Specified by:
        `visitSwitch` in interface `TreeVisitor<R,P>`

    - #### visitCase

          public R visitCase(CaseTree node,
                             P p)

      Specified by:
        `visitCase` in interface `TreeVisitor<R,P>`

    - #### visitSynchronized

          public R visitSynchronized(SynchronizedTree node,
                                     P p)

      Specified by:
        `visitSynchronized` in interface `TreeVisitor<R,P>`

    - #### visitTry

          public R visitTry(TryTree node,
                            P p)

      Specified by:
        `visitTry` in interface `TreeVisitor<R,P>`

    - #### visitCatch

          public R visitCatch(CatchTree node,
                              P p)

      Specified by:
        `visitCatch` in interface `TreeVisitor<R,P>`

    - #### visitConditionalExpression

          public R visitConditionalExpression(ConditionalExpressionTree node,
                                              P p)

      Specified by:
        `visitConditionalExpression` in interface `TreeVisitor<R,P>`

    - #### visitIf

          public R visitIf(IfTree node,
                           P p)

      Specified by:
        `visitIf` in interface `TreeVisitor<R,P>`

    - #### visitExpressionStatement

          public R visitExpressionStatement(ExpressionStatementTree node,
                                            P p)

      Specified by:
        `visitExpressionStatement` in interface `TreeVisitor<R,P>`

    - #### visitBreak

          public R visitBreak(BreakTree node,
                              P p)

      Specified by:
        `visitBreak` in interface `TreeVisitor<R,P>`

    - #### visitContinue

          public R visitContinue(ContinueTree node,
                                 P p)

      Specified by:
        `visitContinue` in interface `TreeVisitor<R,P>`

    - #### visitReturn

          public R visitReturn(ReturnTree node,
                               P p)

      Specified by:
        `visitReturn` in interface `TreeVisitor<R,P>`

    - #### visitThrow

          public R visitThrow(ThrowTree node,
                              P p)

      Specified by:
        `visitThrow` in interface `TreeVisitor<R,P>`

    - #### visitAssert

          public R visitAssert(AssertTree node,
                               P p)

      Specified by:
        `visitAssert` in interface `TreeVisitor<R,P>`

    - #### visitMethodInvocation

          public R visitMethodInvocation(MethodInvocationTree node,
                                         P p)

      Specified by:
        `visitMethodInvocation` in interface `TreeVisitor<R,P>`

    - #### visitNewClass

          public R visitNewClass(NewClassTree node,
                                 P p)

      Specified by:
        `visitNewClass` in interface `TreeVisitor<R,P>`

    - #### visitNewArray

          public R visitNewArray(NewArrayTree node,
                                 P p)

      Specified by:
        `visitNewArray` in interface `TreeVisitor<R,P>`

    - #### visitLambdaExpression

          public R visitLambdaExpression(LambdaExpressionTree node,
                                         P p)

      Specified by:
        `visitLambdaExpression` in interface `TreeVisitor<R,P>`

    - #### visitParenthesized

          public R visitParenthesized(ParenthesizedTree node,
                                      P p)

      Specified by:
        `visitParenthesized` in interface `TreeVisitor<R,P>`

    - #### visitAssignment

          public R visitAssignment(AssignmentTree node,
                                   P p)

      Specified by:
        `visitAssignment` in interface `TreeVisitor<R,P>`

    - #### visitCompoundAssignment

          public R visitCompoundAssignment(CompoundAssignmentTree node,
                                           P p)

      Specified by:
        `visitCompoundAssignment` in interface `TreeVisitor<R,P>`

    - #### visitUnary

          public R visitUnary(UnaryTree node,
                              P p)

      Specified by:
        `visitUnary` in interface `TreeVisitor<R,P>`

    - #### visitBinary

          public R visitBinary(BinaryTree node,
                               P p)

      Specified by:
        `visitBinary` in interface `TreeVisitor<R,P>`

    - #### visitTypeCast

          public R visitTypeCast(TypeCastTree node,
                                 P p)

      Specified by:
        `visitTypeCast` in interface `TreeVisitor<R,P>`

    - #### visitInstanceOf

          public R visitInstanceOf(InstanceOfTree node,
                                   P p)

      Specified by:
        `visitInstanceOf` in interface `TreeVisitor<R,P>`

    - #### visitArrayAccess

          public R visitArrayAccess(ArrayAccessTree node,
                                    P p)

      Specified by:
        `visitArrayAccess` in interface `TreeVisitor<R,P>`

    - #### visitMemberSelect

          public R visitMemberSelect(MemberSelectTree node,
                                     P p)

      Specified by:
        `visitMemberSelect` in interface `TreeVisitor<R,P>`

    - #### visitMemberReference

          public R visitMemberReference(MemberReferenceTree node,
                                        P p)

      Specified by:
        `visitMemberReference` in interface `TreeVisitor<R,P>`

    - #### visitIdentifier

          public R visitIdentifier(IdentifierTree node,
                                   P p)

      Specified by:
        `visitIdentifier` in interface `TreeVisitor<R,P>`

    - #### visitLiteral

          public R visitLiteral(LiteralTree node,
                                P p)

      Specified by:
        `visitLiteral` in interface `TreeVisitor<R,P>`

    - #### visitPrimitiveType

          public R visitPrimitiveType(PrimitiveTypeTree node,
                                      P p)

      Specified by:
        `visitPrimitiveType` in interface `TreeVisitor<R,P>`

    - #### visitArrayType

          public R visitArrayType(ArrayTypeTree node,
                                  P p)

      Specified by:
        `visitArrayType` in interface `TreeVisitor<R,P>`

    - #### visitParameterizedType

          public R visitParameterizedType(ParameterizedTypeTree node,
                                          P p)

      Specified by:
        `visitParameterizedType` in interface `TreeVisitor<R,P>`

    - #### visitUnionType

          public R visitUnionType(UnionTypeTree node,
                                  P p)

      Specified by:
        `visitUnionType` in interface `TreeVisitor<R,P>`

    - #### visitIntersectionType

          public R visitIntersectionType(IntersectionTypeTree node,
                                         P p)

      Specified by:
        `visitIntersectionType` in interface `TreeVisitor<R,P>`

    - #### visitTypeParameter

          public R visitTypeParameter(TypeParameterTree node,
                                      P p)

      Specified by:
        `visitTypeParameter` in interface `TreeVisitor<R,P>`

    - #### visitWildcard

          public R visitWildcard(WildcardTree node,
                                 P p)

      Specified by:
        `visitWildcard` in interface `TreeVisitor<R,P>`

    - #### visitModifiers

          public R visitModifiers(ModifiersTree node,
                                  P p)

      Specified by:
        `visitModifiers` in interface `TreeVisitor<R,P>`

    - #### visitAnnotation

          public R visitAnnotation(AnnotationTree node,
                                   P p)

      Specified by:
        `visitAnnotation` in interface `TreeVisitor<R,P>`

    - #### visitAnnotatedType

          public R visitAnnotatedType(AnnotatedTypeTree node,
                                      P p)

      Specified by:
        `visitAnnotatedType` in interface `TreeVisitor<R,P>`

    - #### visitOther

          public R visitOther(Tree node,
                              P p)

      Specified by:
        `visitOther` in interface `TreeVisitor<R,P>`

    - #### visitErroneous

          public R visitErroneous(ErroneousTree node,
                                  P p)

      Specified by:
        `visitErroneous` in interface `TreeVisitor<R,P>`

Copyright © 2005, 2020, Oracle and/or its affiliates. All rights reserved.