Module jdk.compiler

Class TreeScanner<R,​P>

java.lang.Object
com.sun.source.util.TreeScanner<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>
Direct Known Subclasses:
TreePathScanner

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 Details

  • Method Details

    • scan

      public R scan​(Tree tree, P p)
      Scans a single node.
      Parameters:
      tree - the node to be scanned
      p - a parameter value passed to the visit method
      Returns:
      the result value from the visit method
    • scan

      public R scan​(Iterable<? extends Tree> nodes, P p)
      Scans a sequence of nodes.
      Parameters:
      nodes - the nodes to be scanned
      p - a parameter value to be passed to the visit method for each node
      Returns:
      the combined return value from the visit methods. The values are combined using the reduce method.
    • 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.
      Parameters:
      r1 - the first of the values to be combined
      r2 - the second of the values to be combined
      Returns:
      the result of combining the two parameters
    • visitCompilationUnit

      public R visitCompilationUnit​(CompilationUnitTree node, P p)
      Visits a CompilationUnitTree node. This implementation scans the children in left to right order.
      Specified by:
      visitCompilationUnit in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitPackage

      public R visitPackage​(PackageTree node, P p)
      Visits a PackageTree node. This implementation scans the children in left to right order.
      Specified by:
      visitPackage in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitImport

      public R visitImport​(ImportTree node, P p)
      Visits an ImportTree node. This implementation scans the children in left to right order.
      Specified by:
      visitImport in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitClass

      public R visitClass​(ClassTree node, P p)
      Visits a ClassTree node. This implementation scans the children in left to right order.
      Specified by:
      visitClass in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitMethod

      public R visitMethod​(MethodTree node, P p)
      Visits a MethodTree node. This implementation scans the children in left to right order.
      Specified by:
      visitMethod in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitVariable

      public R visitVariable​(VariableTree node, P p)
      Visits a VariableTree node. This implementation scans the children in left to right order.
      Specified by:
      visitVariable in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitEmptyStatement

      public R visitEmptyStatement​(EmptyStatementTree node, P p)
      Visits an EmptyStatementTree node. This implementation returns null.
      Specified by:
      visitEmptyStatement in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitBlock

      public R visitBlock​(BlockTree node, P p)
      Visits a BlockTree node. This implementation scans the children in left to right order.
      Specified by:
      visitBlock in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitDoWhileLoop

      public R visitDoWhileLoop​(DoWhileLoopTree node, P p)
      Visits a DoWhileTree node. This implementation scans the children in left to right order.
      Specified by:
      visitDoWhileLoop in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitWhileLoop

      public R visitWhileLoop​(WhileLoopTree node, P p)
      Visits a WhileLoopTree node. This implementation scans the children in left to right order.
      Specified by:
      visitWhileLoop in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitForLoop

      public R visitForLoop​(ForLoopTree node, P p)
      Visits a ForLoopTree node. This implementation scans the children in left to right order.
      Specified by:
      visitForLoop in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitEnhancedForLoop

      public R visitEnhancedForLoop​(EnhancedForLoopTree node, P p)
      Visits an EnhancedForLoopTree node. This implementation scans the children in left to right order.
      Specified by:
      visitEnhancedForLoop in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitLabeledStatement

      public R visitLabeledStatement​(LabeledStatementTree node, P p)
      Visits a LabeledStatementTree node. This implementation scans the children in left to right order.
      Specified by:
      visitLabeledStatement in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitSwitch

      public R visitSwitch​(SwitchTree node, P p)
      Visits a SwitchTree node. This implementation scans the children in left to right order.
      Specified by:
      visitSwitch in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitSwitchExpression

      @Deprecated(forRemoval=true, since="12") public R visitSwitchExpression​(SwitchExpressionTree node, P p)
      Deprecated, for removal: This API element is subject to removal in a future version.
      This method is modeling switch expressions, which are part of a preview feature and may be removed if the preview feature is removed.
      Visits a SwitchExpressionTree node. This implementation scans the children in left to right order.
      Specified by:
      visitSwitchExpression in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitCase

      public R visitCase​(CaseTree node, P p)
      Visits a CaseTree node. This implementation scans the children in left to right order.
      Specified by:
      visitCase in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitSynchronized

      public R visitSynchronized​(SynchronizedTree node, P p)
      Visits a SynchronizedTree node. This implementation scans the children in left to right order.
      Specified by:
      visitSynchronized in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitTry

      public R visitTry​(TryTree node, P p)
      Visits a TryTree node. This implementation scans the children in left to right order.
      Specified by:
      visitTry in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitCatch

      public R visitCatch​(CatchTree node, P p)
      Visits a CatchTree node. This implementation scans the children in left to right order.
      Specified by:
      visitCatch in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitConditionalExpression

      public R visitConditionalExpression​(ConditionalExpressionTree node, P p)
      Visits a ConditionalExpressionTree node. This implementation scans the children in left to right order.
      Specified by:
      visitConditionalExpression in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitIf

      public R visitIf​(IfTree node, P p)
      Visits an IfTree node. This implementation scans the children in left to right order.
      Specified by:
      visitIf in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitExpressionStatement

      public R visitExpressionStatement​(ExpressionStatementTree node, P p)
      Visits an ExpressionStatementTree node. This implementation scans the children in left to right order.
      Specified by:
      visitExpressionStatement in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitBreak

      public R visitBreak​(BreakTree node, P p)
      Visits a BreakTree node. This implementation returns null.
      Specified by:
      visitBreak in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitContinue

      public R visitContinue​(ContinueTree node, P p)
      Visits a ContinueTree node. This implementation returns null.
      Specified by:
      visitContinue in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitReturn

      public R visitReturn​(ReturnTree node, P p)
      Visits a ReturnTree node. This implementation scans the children in left to right order.
      Specified by:
      visitReturn in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitThrow

      public R visitThrow​(ThrowTree node, P p)
      Visits a ThrowTree node. This implementation scans the children in left to right order.
      Specified by:
      visitThrow in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitAssert

      public R visitAssert​(AssertTree node, P p)
      Visits an AssertTree node. This implementation scans the children in left to right order.
      Specified by:
      visitAssert in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitMethodInvocation

      public R visitMethodInvocation​(MethodInvocationTree node, P p)
      Visits a MethodInvocationTree node. This implementation scans the children in left to right order.
      Specified by:
      visitMethodInvocation in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitNewClass

      public R visitNewClass​(NewClassTree node, P p)
      Visits a NewClassTree node. This implementation scans the children in left to right order.
      Specified by:
      visitNewClass in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitNewArray

      public R visitNewArray​(NewArrayTree node, P p)
      Visits a NewArrayTree node. This implementation scans the children in left to right order.
      Specified by:
      visitNewArray in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitLambdaExpression

      public R visitLambdaExpression​(LambdaExpressionTree node, P p)
      Visits a LambdaExpressionTree node. This implementation scans the children in left to right order.
      Specified by:
      visitLambdaExpression in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitParenthesized

      public R visitParenthesized​(ParenthesizedTree node, P p)
      Visits a ParenthesizedTree node. This implementation scans the children in left to right order.
      Specified by:
      visitParenthesized in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitAssignment

      public R visitAssignment​(AssignmentTree node, P p)
      Visits an AssignmentTree node. This implementation scans the children in left to right order.
      Specified by:
      visitAssignment in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitCompoundAssignment

      public R visitCompoundAssignment​(CompoundAssignmentTree node, P p)
      Visits a CompoundAssignmentTree node. This implementation scans the children in left to right order.
      Specified by:
      visitCompoundAssignment in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitUnary

      public R visitUnary​(UnaryTree node, P p)
      Visits a UnaryTree node. This implementation scans the children in left to right order.
      Specified by:
      visitUnary in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitBinary

      public R visitBinary​(BinaryTree node, P p)
      Visits a BinaryTree node. This implementation scans the children in left to right order.
      Specified by:
      visitBinary in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitTypeCast

      public R visitTypeCast​(TypeCastTree node, P p)
      Visits a TypeCastTree node. This implementation scans the children in left to right order.
      Specified by:
      visitTypeCast in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitInstanceOf

      public R visitInstanceOf​(InstanceOfTree node, P p)
      Visits an InstanceOfTree node. This implementation scans the children in left to right order.
      Specified by:
      visitInstanceOf in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitArrayAccess

      public R visitArrayAccess​(ArrayAccessTree node, P p)
      Visits an ArrayAccessTree node. This implementation scans the children in left to right order.
      Specified by:
      visitArrayAccess in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitMemberSelect

      public R visitMemberSelect​(MemberSelectTree node, P p)
      Visits a MemberSelectTree node. This implementation scans the children in left to right order.
      Specified by:
      visitMemberSelect in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitMemberReference

      public R visitMemberReference​(MemberReferenceTree node, P p)
      Visits a MemberReferenceTree node. This implementation scans the children in left to right order.
      Specified by:
      visitMemberReference in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitIdentifier

      public R visitIdentifier​(IdentifierTree node, P p)
      Visits an IdentifierTree node. This implementation returns null.
      Specified by:
      visitIdentifier in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitLiteral

      public R visitLiteral​(LiteralTree node, P p)
      Visits a LiteralTree node. This implementation returns null.
      Specified by:
      visitLiteral in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitPrimitiveType

      public R visitPrimitiveType​(PrimitiveTypeTree node, P p)
      Visits a PrimitiveTypeTree node. This implementation returns null.
      Specified by:
      visitPrimitiveType in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitArrayType

      public R visitArrayType​(ArrayTypeTree node, P p)
      Visits an ArrayTypeTree node. This implementation scans the children in left to right order.
      Specified by:
      visitArrayType in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitParameterizedType

      public R visitParameterizedType​(ParameterizedTypeTree node, P p)
      Visits a ParameterizedTypeTree node. This implementation scans the children in left to right order.
      Specified by:
      visitParameterizedType in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitUnionType

      public R visitUnionType​(UnionTypeTree node, P p)
      Visits a UnionTypeTree node. This implementation scans the children in left to right order.
      Specified by:
      visitUnionType in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitIntersectionType

      public R visitIntersectionType​(IntersectionTypeTree node, P p)
      Visits an IntersectionTypeTree node. This implementation scans the children in left to right order.
      Specified by:
      visitIntersectionType in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitTypeParameter

      public R visitTypeParameter​(TypeParameterTree node, P p)
      Visits a TypeParameterTree node. This implementation scans the children in left to right order.
      Specified by:
      visitTypeParameter in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitWildcard

      public R visitWildcard​(WildcardTree node, P p)
      Visits a WildcardTypeTree node. This implementation scans the children in left to right order.
      Specified by:
      visitWildcard in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitModifiers

      public R visitModifiers​(ModifiersTree node, P p)
      Visits a ModifiersTree node. This implementation scans the children in left to right order.
      Specified by:
      visitModifiers in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitAnnotation

      public R visitAnnotation​(AnnotationTree node, P p)
      Visits an AnnotatedTree node. This implementation scans the children in left to right order.
      Specified by:
      visitAnnotation in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitAnnotatedType

      public R visitAnnotatedType​(AnnotatedTypeTree node, P p)
      Visits an AnnotatedTypeTree node. This implementation scans the children in left to right order.
      Specified by:
      visitAnnotatedType in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitOther

      public R visitOther​(Tree node, P p)
      Visits an unknown type of Tree node. This can occur if the language evolves and new kinds of nodes are added to the Tree hierarchy. This implementation returns null.
      Specified by:
      visitOther in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitErroneous

      public R visitErroneous​(ErroneousTree node, P p)
      Visits an ErroneousTree node. This implementation returns null.
      Specified by:
      visitErroneous in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning
    • visitYield

      @Deprecated(forRemoval=true, since="13") public R visitYield​(YieldTree node, P p)
      Deprecated, for removal: This API element is subject to removal in a future version.
      This method is modeling switch expressions, which are part of a preview feature and may be removed if the preview feature is removed.
      Visits a YieldTree node. This implementation returns null.
      Specified by:
      visitYield in interface TreeVisitor<R,​P>
      Parameters:
      node - the node being visited
      p - a parameter value
      Returns:
      the result of scanning