java.lang.Object
com.sun.source.util.TreeScanner<R,P>
- Type Parameters:
R- the return type of this visitor's methods. UseVoidfor visitors that do not need to return results.P- the type of the additional parameter to this visitor's methods. UseVoidfor 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
scanon 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
scaneach 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 thereducemethod 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 Rreduce(R r1, R r2)Reduces two results into a combined result.Rscan(Tree tree, P p)Scans a single node.Rscan(Iterable<? extends Tree> nodes, P p)Scans a sequence of nodes.RvisitAnnotatedType(AnnotatedTypeTree node, P p)Visits an AnnotatedTypeTree node.RvisitAnnotation(AnnotationTree node, P p)Visits an AnnotatedTree node.RvisitArrayAccess(ArrayAccessTree node, P p)Visits an ArrayAccessTree node.RvisitArrayType(ArrayTypeTree node, P p)Visits an ArrayTypeTree node.RvisitAssert(AssertTree node, P p)Visits an AssertTree node.RvisitAssignment(AssignmentTree node, P p)Visits an AssignmentTree node.RvisitBinary(BinaryTree node, P p)Visits a BinaryTree node.RvisitBindingPattern(BindingPatternTree node, P p)Associated with pattern matching for instanceof, a preview feature of the Java language.
Visits an BindingPattern node.RvisitBlock(BlockTree node, P p)Visits a BlockTree node.RvisitBreak(BreakTree node, P p)Visits a BreakTree node.RvisitCase(CaseTree node, P p)Visits a CaseTree node.RvisitCatch(CatchTree node, P p)Visits a CatchTree node.RvisitClass(ClassTree node, P p)Visits a ClassTree node.RvisitCompilationUnit(CompilationUnitTree node, P p)Visits a CompilationUnitTree node.RvisitCompoundAssignment(CompoundAssignmentTree node, P p)Visits a CompoundAssignmentTree node.RvisitConditionalExpression(ConditionalExpressionTree node, P p)Visits a ConditionalExpressionTree node.RvisitContinue(ContinueTree node, P p)Visits a ContinueTree node.RvisitDoWhileLoop(DoWhileLoopTree node, P p)Visits a DoWhileTree node.RvisitEmptyStatement(EmptyStatementTree node, P p)Visits an EmptyStatementTree node.RvisitEnhancedForLoop(EnhancedForLoopTree node, P p)Visits an EnhancedForLoopTree node.RvisitErroneous(ErroneousTree node, P p)Visits an ErroneousTree node.RvisitExpressionStatement(ExpressionStatementTree node, P p)Visits an ExpressionStatementTree node.RvisitForLoop(ForLoopTree node, P p)Visits a ForLoopTree node.RvisitIdentifier(IdentifierTree node, P p)Visits an IdentifierTree node.RvisitIf(IfTree node, P p)Visits an IfTree node.RvisitImport(ImportTree node, P p)Visits an ImportTree node.RvisitInstanceOf(InstanceOfTree node, P p)Visits an InstanceOfTree node.RvisitIntersectionType(IntersectionTypeTree node, P p)Visits an IntersectionTypeTree node.RvisitLabeledStatement(LabeledStatementTree node, P p)Visits a LabeledStatementTree node.RvisitLambdaExpression(LambdaExpressionTree node, P p)Visits a LambdaExpressionTree node.RvisitLiteral(LiteralTree node, P p)Visits a LiteralTree node.RvisitMemberReference(MemberReferenceTree node, P p)Visits a MemberReferenceTree node.RvisitMemberSelect(MemberSelectTree node, P p)Visits a MemberSelectTree node.RvisitMethod(MethodTree node, P p)Visits a MethodTree node.RvisitMethodInvocation(MethodInvocationTree node, P p)Visits a MethodInvocationTree node.RvisitModifiers(ModifiersTree node, P p)Visits a ModifiersTree node.RvisitNewArray(NewArrayTree node, P p)Visits a NewArrayTree node.RvisitNewClass(NewClassTree node, P p)Visits a NewClassTree node.RvisitOther(Tree node, P p)Visits an unknown type of Tree node.RvisitPackage(PackageTree node, P p)Visits a PackageTree node.RvisitParameterizedType(ParameterizedTypeTree node, P p)Visits a ParameterizedTypeTree node.RvisitParenthesized(ParenthesizedTree node, P p)Visits a ParenthesizedTree node.RvisitPrimitiveType(PrimitiveTypeTree node, P p)Visits a PrimitiveTypeTree node.RvisitReturn(ReturnTree node, P p)Visits a ReturnTree node.RvisitSwitch(SwitchTree node, P p)Visits a SwitchTree node.RvisitSwitchExpression(SwitchExpressionTree node, P p)Visits a SwitchExpressionTree node.RvisitSynchronized(SynchronizedTree node, P p)Visits a SynchronizedTree node.RvisitThrow(ThrowTree node, P p)Visits a ThrowTree node.RvisitTry(TryTree node, P p)Visits a TryTree node.RvisitTypeCast(TypeCastTree node, P p)Visits a TypeCastTree node.RvisitTypeParameter(TypeParameterTree node, P p)Visits a TypeParameterTree node.RvisitUnary(UnaryTree node, P p)Visits a UnaryTree node.RvisitUnionType(UnionTypeTree node, P p)Visits a UnionTypeTree node.RvisitVariable(VariableTree node, P p)Visits a VariableTree node.RvisitWhileLoop(WhileLoopTree node, P p)Visits a WhileLoopTree node.RvisitWildcard(WildcardTree node, P p)Visits a WildcardTypeTree node.RvisitYield(YieldTree node, P p)Visits a YieldTree node.Methods declared in class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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
reducemethod.
-
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:
visitCompilationUnitin 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:
visitPackagein 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:
visitImportin 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:
visitClassin 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:
visitMethodin 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:
visitVariablein 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:
visitEmptyStatementin 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:
visitBlockin 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:
visitDoWhileLoopin 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:
visitWhileLoopin 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:
visitForLoopin 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:
visitEnhancedForLoopin 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:
visitLabeledStatementin 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:
visitSwitchin interfaceTreeVisitor<R,P>- Parameters:
node- the node being visitedp- a parameter value- Returns:
- the result of scanning
-
visitSwitchExpression
Visits a SwitchExpressionTree node. This implementation scans the children in left to right order.- Specified by:
visitSwitchExpressionin 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:
visitCasein 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:
visitSynchronizedin 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:
visitTryin 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:
visitCatchin 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:
visitConditionalExpressionin 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:
visitIfin 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:
visitExpressionStatementin 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:
visitBreakin 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:
visitContinuein 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:
visitReturnin 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:
visitThrowin 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:
visitAssertin 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:
visitMethodInvocationin 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:
visitNewClassin 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:
visitNewArrayin 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:
visitLambdaExpressionin 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:
visitParenthesizedin 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:
visitAssignmentin 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:
visitCompoundAssignmentin 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:
visitUnaryin 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:
visitBinaryin 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:
visitTypeCastin 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:
visitInstanceOfin interfaceTreeVisitor<R,P>- Parameters:
node- the node being visitedp- a parameter value- Returns:
- the result of scanning
-
visitBindingPattern
This method is associated with pattern matching for instanceof, a preview feature of the Java language. Preview features may be removed in a future release, or upgraded to permanent features of the Java language.
Visits an BindingPattern node. This implementation scans the children in left to right order.- Specified by:
visitBindingPatternin interfaceTreeVisitor<R,P>- Parameters:
node- the node being visitedp- a parameter value- Returns:
- the result of scanning
- Since:
- 14
-
visitArrayAccess
Visits an ArrayAccessTree node. This implementation scans the children in left to right order.- Specified by:
visitArrayAccessin 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:
visitMemberSelectin 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:
visitMemberReferencein 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:
visitIdentifierin 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:
visitLiteralin 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:
visitPrimitiveTypein 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:
visitArrayTypein 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:
visitParameterizedTypein 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:
visitUnionTypein 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:
visitIntersectionTypein 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:
visitTypeParameterin 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:
visitWildcardin 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:
visitModifiersin 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:
visitAnnotationin 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:
visitAnnotatedTypein 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 theTreehierarchy. This implementation returnsnull.- Specified by:
visitOtherin 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:
visitErroneousin interfaceTreeVisitor<R,P>- Parameters:
node- the node being visitedp- a parameter value- Returns:
- the result of scanning
-
visitYield
Visits a YieldTree node. This implementation returnsnull.- Specified by:
visitYieldin interfaceTreeVisitor<R,P>- Parameters:
node- the node being visitedp- a parameter value- Returns:
- the result of scanning
-