java.lang.Object
com.sun.source.util.TreeScanner<R,P>
- Type Parameters:
R
- the return type of this visitor's methods. UseVoid
for visitors that do not need to return results.P
- the type of the additional parameter to this visitor's methods. UseVoid
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 thereduce(R, R)
method. Each child may be either a simple node of a list of nodes. The default behavior of thereduce
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 Description TreeScanner()
-
Method Summary
Modifier and Type Method Description R
reduce(R r1, R r2)
Reduces two results into a combined result.R
scan(Tree tree, P p)
Scans a single node.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
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
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
visitContinue(ContinueTree node, P p)
Visits a ContinueTree 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
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
visitNewArray(NewArrayTree node, P p)
Visits a NewArrayTree node.R
visitNewClass(NewClassTree node, P p)
Visits a NewClassTree 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
visitPrimitiveType(PrimitiveTypeTree node, P p)
Visits a PrimitiveTypeTree 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)
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.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
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)
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.Methods declared in class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods declared in interface com.sun.source.tree.TreeVisitor
visitExports, visitModule, visitOpens, visitProvides, visitRequires, visitUses
-
Constructor Details
-
TreeScanner
public TreeScanner()
-
-
Method Details
-
scan
Scans a single node.- Parameters:
tree
- the node to be scannedp
- a parameter value passed to the visit method- Returns:
- the result value from the visit method
-
scan
Scans a sequence of nodes.- Parameters:
nodes
- the nodes to be scannedp
- 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
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 combinedr2
- the second of the values to be combined- Returns:
- the result of combining the two parameters
-
visitCompilationUnit
Visits a CompilationUnitTree node. This implementation scans the children in left to right order.- Specified by:
visitCompilationUnit
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitPackage
Visits a PackageTree node. This implementation scans the children in left to right order.- Specified by:
visitPackage
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitImport
Visits an ImportTree node. This implementation scans the children in left to right order.- Specified by:
visitImport
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitClass
Visits a ClassTree node. This implementation scans the children in left to right order.- Specified by:
visitClass
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitMethod
Visits a MethodTree node. This implementation scans the children in left to right order.- Specified by:
visitMethod
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitVariable
Visits a VariableTree node. This implementation scans the children in left to right order.- Specified by:
visitVariable
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitEmptyStatement
Visits an EmptyStatementTree node. This implementation returnsnull
.- Specified by:
visitEmptyStatement
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitBlock
Visits a BlockTree node. This implementation scans the children in left to right order.- Specified by:
visitBlock
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitDoWhileLoop
Visits a DoWhileTree node. This implementation scans the children in left to right order.- Specified by:
visitDoWhileLoop
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitWhileLoop
Visits a WhileLoopTree node. This implementation scans the children in left to right order.- Specified by:
visitWhileLoop
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitForLoop
Visits a ForLoopTree node. This implementation scans the children in left to right order.- Specified by:
visitForLoop
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitEnhancedForLoop
Visits an EnhancedForLoopTree node. This implementation scans the children in left to right order.- Specified by:
visitEnhancedForLoop
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitLabeledStatement
Visits a LabeledStatementTree node. This implementation scans the children in left to right order.- Specified by:
visitLabeledStatement
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitSwitch
Visits a SwitchTree node. This implementation scans the children in left to right order.- Specified by:
visitSwitch
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- 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 interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitCase
Visits a CaseTree node. This implementation scans the children in left to right order.- Specified by:
visitCase
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitSynchronized
Visits a SynchronizedTree node. This implementation scans the children in left to right order.- Specified by:
visitSynchronized
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitTry
Visits a TryTree node. This implementation scans the children in left to right order.- Specified by:
visitTry
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitCatch
Visits a CatchTree node. This implementation scans the children in left to right order.- Specified by:
visitCatch
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitConditionalExpression
Visits a ConditionalExpressionTree node. This implementation scans the children in left to right order.- Specified by:
visitConditionalExpression
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitIf
Visits an IfTree node. This implementation scans the children in left to right order.- Specified by:
visitIf
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitExpressionStatement
Visits an ExpressionStatementTree node. This implementation scans the children in left to right order.- Specified by:
visitExpressionStatement
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitBreak
Visits a BreakTree node. This implementation returnsnull
.- Specified by:
visitBreak
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitContinue
Visits a ContinueTree node. This implementation returnsnull
.- Specified by:
visitContinue
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitReturn
Visits a ReturnTree node. This implementation scans the children in left to right order.- Specified by:
visitReturn
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitThrow
Visits a ThrowTree node. This implementation scans the children in left to right order.- Specified by:
visitThrow
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitAssert
Visits an AssertTree node. This implementation scans the children in left to right order.- Specified by:
visitAssert
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitMethodInvocation
Visits a MethodInvocationTree node. This implementation scans the children in left to right order.- Specified by:
visitMethodInvocation
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitNewClass
Visits a NewClassTree node. This implementation scans the children in left to right order.- Specified by:
visitNewClass
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitNewArray
Visits a NewArrayTree node. This implementation scans the children in left to right order.- Specified by:
visitNewArray
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitLambdaExpression
Visits a LambdaExpressionTree node. This implementation scans the children in left to right order.- Specified by:
visitLambdaExpression
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitParenthesized
Visits a ParenthesizedTree node. This implementation scans the children in left to right order.- Specified by:
visitParenthesized
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitAssignment
Visits an AssignmentTree node. This implementation scans the children in left to right order.- Specified by:
visitAssignment
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitCompoundAssignment
Visits a CompoundAssignmentTree node. This implementation scans the children in left to right order.- Specified by:
visitCompoundAssignment
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitUnary
Visits a UnaryTree node. This implementation scans the children in left to right order.- Specified by:
visitUnary
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitBinary
Visits a BinaryTree node. This implementation scans the children in left to right order.- Specified by:
visitBinary
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitTypeCast
Visits a TypeCastTree node. This implementation scans the children in left to right order.- Specified by:
visitTypeCast
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitInstanceOf
Visits an InstanceOfTree node. This implementation scans the children in left to right order.- Specified by:
visitInstanceOf
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitArrayAccess
Visits an ArrayAccessTree node. This implementation scans the children in left to right order.- Specified by:
visitArrayAccess
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitMemberSelect
Visits a MemberSelectTree node. This implementation scans the children in left to right order.- Specified by:
visitMemberSelect
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitMemberReference
Visits a MemberReferenceTree node. This implementation scans the children in left to right order.- Specified by:
visitMemberReference
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitIdentifier
Visits an IdentifierTree node. This implementation returnsnull
.- Specified by:
visitIdentifier
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitLiteral
Visits a LiteralTree node. This implementation returnsnull
.- Specified by:
visitLiteral
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitPrimitiveType
Visits a PrimitiveTypeTree node. This implementation returnsnull
.- Specified by:
visitPrimitiveType
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitArrayType
Visits an ArrayTypeTree node. This implementation scans the children in left to right order.- Specified by:
visitArrayType
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitParameterizedType
Visits a ParameterizedTypeTree node. This implementation scans the children in left to right order.- Specified by:
visitParameterizedType
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitUnionType
Visits a UnionTypeTree node. This implementation scans the children in left to right order.- Specified by:
visitUnionType
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitIntersectionType
Visits an IntersectionTypeTree node. This implementation scans the children in left to right order.- Specified by:
visitIntersectionType
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitTypeParameter
Visits a TypeParameterTree node. This implementation scans the children in left to right order.- Specified by:
visitTypeParameter
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitWildcard
Visits a WildcardTypeTree node. This implementation scans the children in left to right order.- Specified by:
visitWildcard
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitModifiers
Visits a ModifiersTree node. This implementation scans the children in left to right order.- Specified by:
visitModifiers
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitAnnotation
Visits an AnnotatedTree node. This implementation scans the children in left to right order.- Specified by:
visitAnnotation
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitAnnotatedType
Visits an AnnotatedTypeTree node. This implementation scans the children in left to right order.- Specified by:
visitAnnotatedType
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitOther
Visits an unknown type of Tree node. This can occur if the language evolves and new kinds of nodes are added to theTree
hierarchy. This implementation returnsnull
.- Specified by:
visitOther
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitErroneous
Visits an ErroneousTree node. This implementation returnsnull
.- Specified by:
visitErroneous
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-
visitYield
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 returnsnull
.- Specified by:
visitYield
in interfaceTreeVisitor<R,P>
- Parameters:
node
- the node being visitedp
- a parameter value- Returns:
- the result of scanning
-