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.TreeVisitor<R,P>
TreePathScanner
public class TreeScanner<R,P> extends Object implements TreeVisitor<R,P>
TreeScanner
relies on preview features of the Java platform: TreeScanner
refers to one or more preview APIs: AnyPatternTree
.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); } }
The default implementation of the visitXYZ methods will determine a result as follows:
null
. scan
with that child. The child may be a simple node or itself a list of nodes. scan
with 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 or 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. Constructor | Description |
---|---|
TreeScanner() |
Constructs a TreeScanner . |
Modifier and Type | Method | Description |
---|---|---|
R |
reduce |
Reduces two results into a combined result. |
R |
scan |
Scans a single node. |
R |
scan |
Scans a sequence of nodes. |
R |
visitAnnotatedType |
Visits an AnnotatedTypeTree node. |
R |
visitAnnotation |
Visits an AnnotatedTree node. |
R |
visitAnyPattern |
Visits a AnyPatternTree node. |
R |
visitArrayAccess |
Visits an ArrayAccessTree node. |
R |
visitArrayType |
Visits an ArrayTypeTree node. |
R |
visitAssert |
Visits an AssertTree node. |
R |
visitAssignment |
Visits an AssignmentTree node. |
R |
visitBinary |
Visits a BinaryTree node. |
R |
visitBindingPattern |
Visits a BindingPatternTree node. |
R |
visitBlock |
Visits a BlockTree node. |
R |
visitBreak |
Visits a BreakTree node. |
R |
visitCase |
Visits a CaseTree node. |
R |
visitCatch |
Visits a CatchTree node. |
R |
visitClass |
Visits a ClassTree node. |
R |
visitCompilationUnit |
Visits a CompilationUnitTree node. |
R |
visitCompoundAssignment |
Visits a CompoundAssignmentTree node. |
R |
visitConditionalExpression |
Visits a ConditionalExpressionTree node. |
R |
visitConstantCaseLabel |
Visits a ConstantCaseLabelTree node. |
R |
visitContinue |
Visits a ContinueTree node. |
R |
visitDeconstructionPattern |
Visits a DeconstructionPatternTree node. |
R |
visitDefaultCaseLabel |
Visits a DefaultCaseLabelTree node. |
R |
visitDoWhileLoop |
Visits a DoWhileTree node. |
R |
visitEmptyStatement |
Visits an EmptyStatementTree node. |
R |
visitEnhancedForLoop |
Visits an EnhancedForLoopTree node. |
R |
visitErroneous |
Visits an ErroneousTree node. |
R |
visitExports |
Visits an ExportsTree node. |
R |
visitExpressionStatement |
Visits an ExpressionStatementTree node. |
R |
visitForLoop |
Visits a ForLoopTree node. |
R |
visitIdentifier |
Visits an IdentifierTree node. |
R |
visitIf |
Visits an IfTree node. |
R |
visitImport |
Visits an ImportTree node. |
R |
visitInstanceOf |
Visits an InstanceOfTree node. |
R |
visitIntersectionType |
Visits an IntersectionTypeTree node. |
R |
visitLabeledStatement |
Visits a LabeledStatementTree node. |
R |
visitLambdaExpression |
Visits a LambdaExpressionTree node. |
R |
visitLiteral |
Visits a LiteralTree node. |
R |
visitMemberReference |
Visits a MemberReferenceTree node. |
R |
visitMemberSelect |
Visits a MemberSelectTree node. |
R |
visitMethod |
Visits a MethodTree node. |
R |
visitMethodInvocation |
Visits a MethodInvocationTree node. |
R |
visitModifiers |
Visits a ModifiersTree node. |
R |
visitModule |
Visits a ModuleTree node. |
R |
visitNewArray |
Visits a NewArrayTree node. |
R |
visitNewClass |
Visits a NewClassTree node. |
R |
visitOpens |
Visits an OpensTree node. |
R |
visitOther |
Visits an unknown type of Tree node. |
R |
visitPackage |
Visits a PackageTree node. |
R |
visitParameterizedType |
Visits a ParameterizedTypeTree node. |
R |
visitParenthesized |
Visits a ParenthesizedTree node. |
R |
visitPatternCaseLabel |
Visits a PatternCaseLabelTree node. |
R |
visitPrimitiveType |
Visits a PrimitiveTypeTree node. |
R |
visitProvides |
Visits a ProvidesTree node. |
R |
visitRequires |
Visits a RequiresTree node. |
R |
visitReturn |
Visits a ReturnTree node. |
R |
visitStringTemplate |
Preview. Visits a StringTemplateTree node. |
R |
visitSwitch |
Visits a SwitchTree node. |
R |
visitSwitchExpression |
Visits a SwitchExpressionTree node. |
R |
visitSynchronized |
Visits a SynchronizedTree node. |
R |
visitThrow |
Visits a ThrowTree node. |
R |
visitTry |
Visits a TryTree node. |
R |
visitTypeCast |
Visits a TypeCastTree node. |
R |
visitTypeParameter |
Visits a TypeParameterTree node. |
R |
visitUnary |
Visits a UnaryTree node. |
R |
visitUnionType |
Visits a UnionTypeTree node. |
R |
visitUses |
Visits a UsesTree node. |
R |
visitVariable |
Visits a VariableTree node. |
R |
visitWhileLoop |
Visits a WhileLoopTree node. |
R |
visitWildcard |
Visits a WildcardTypeTree node. |
R |
visitYield |
Visits a YieldTree node. |
public TreeScanner()
TreeScanner
.public R scan(Tree tree, P p)
tree
- the node to be scannedp
- a parameter value passed to the visit methodpublic R scan(Iterable<? extends Tree> nodes, P p)
nodes
- the nodes to be scannedp
- a parameter value to be passed to the visit method for each nodereduce
method.public R reduce(R r1, R r2)
r1
- the first of the values to be combinedr2
- the second of the values to be combinedpublic R visitCompilationUnit(CompilationUnitTree node, P p)
CompilationUnitTree
node.visitCompilationUnit
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitPackage(PackageTree node, P p)
PackageTree
node.visitPackage
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitImport(ImportTree node, P p)
ImportTree
node.visitImport
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitClass(ClassTree node, P p)
ClassTree
node.visitClass
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitMethod(MethodTree node, P p)
MethodTree
node.visitMethod
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitVariable(VariableTree node, P p)
VariableTree
node.visitVariable
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitEmptyStatement(EmptyStatementTree node, P p)
EmptyStatementTree
node.visitEmptyStatement
in interface TreeVisitor<R,P>
null
.node
- the node being visitedp
- a parameter valuepublic R visitBlock(BlockTree node, P p)
BlockTree
node.visitBlock
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitDoWhileLoop(DoWhileLoopTree node, P p)
DoWhileTree
node.visitDoWhileLoop
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitWhileLoop(WhileLoopTree node, P p)
WhileLoopTree
node.visitWhileLoop
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitForLoop(ForLoopTree node, P p)
ForLoopTree
node.visitForLoop
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitEnhancedForLoop(EnhancedForLoopTree node, P p)
EnhancedForLoopTree
node.visitEnhancedForLoop
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitLabeledStatement(LabeledStatementTree node, P p)
LabeledStatementTree
node.visitLabeledStatement
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitSwitch(SwitchTree node, P p)
SwitchTree
node.visitSwitch
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitSwitchExpression(SwitchExpressionTree node, P p)
SwitchExpressionTree
node.visitSwitchExpression
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitCase(CaseTree node, P p)
CaseTree
node.visitCase
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitSynchronized(SynchronizedTree node, P p)
SynchronizedTree
node.visitSynchronized
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitTry(TryTree node, P p)
TryTree
node.visitTry
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitCatch(CatchTree node, P p)
CatchTree
node.visitCatch
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitConditionalExpression(ConditionalExpressionTree node, P p)
ConditionalExpressionTree
node.visitConditionalExpression
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitIf(IfTree node, P p)
IfTree
node.visitIf
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitExpressionStatement(ExpressionStatementTree node, P p)
ExpressionStatementTree
node.visitExpressionStatement
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitBreak(BreakTree node, P p)
BreakTree
node.visitBreak
in interface TreeVisitor<R,P>
null
.node
- the node being visitedp
- a parameter valuepublic R visitContinue(ContinueTree node, P p)
ContinueTree
node.visitContinue
in interface TreeVisitor<R,P>
null
.node
- the node being visitedp
- a parameter valuepublic R visitReturn(ReturnTree node, P p)
ReturnTree
node.visitReturn
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitThrow(ThrowTree node, P p)
ThrowTree
node.visitThrow
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitAssert(AssertTree node, P p)
AssertTree
node.visitAssert
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitMethodInvocation(MethodInvocationTree node, P p)
MethodInvocationTree
node.visitMethodInvocation
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitNewClass(NewClassTree node, P p)
NewClassTree
node.visitNewClass
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitNewArray(NewArrayTree node, P p)
NewArrayTree
node.visitNewArray
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitLambdaExpression(LambdaExpressionTree node, P p)
LambdaExpressionTree
node.visitLambdaExpression
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitParenthesized(ParenthesizedTree node, P p)
ParenthesizedTree
node.visitParenthesized
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitAssignment(AssignmentTree node, P p)
AssignmentTree
node.visitAssignment
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitCompoundAssignment(CompoundAssignmentTree node, P p)
CompoundAssignmentTree
node.visitCompoundAssignment
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitUnary(UnaryTree node, P p)
UnaryTree
node.visitUnary
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitBinary(BinaryTree node, P p)
BinaryTree
node.visitBinary
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitTypeCast(TypeCastTree node, P p)
TypeCastTree
node.visitTypeCast
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitInstanceOf(InstanceOfTree node, P p)
InstanceOfTree
node.visitInstanceOf
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitAnyPattern(AnyPatternTreePREVIEW node, P p)
AnyPatternTree
node.visitAnyPattern
in interface TreeVisitor<R,P>
null
.node
- the node being visitedp
- a parameter valuepublic R visitStringTemplate(StringTemplateTreePREVIEW node, P p)
visitStringTemplate
is a reflective preview API of the Java platform. visitStringTemplate
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitBindingPattern(BindingPatternTree node, P p)
BindingPatternTree
node.visitBindingPattern
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitDefaultCaseLabel(DefaultCaseLabelTree node, P p)
DefaultCaseLabelTree
node.visitDefaultCaseLabel
in interface TreeVisitor<R,P>
null
.node
- the node being visitedp
- a parameter valuepublic R visitConstantCaseLabel(ConstantCaseLabelTree node, P p)
ConstantCaseLabelTree
node.visitConstantCaseLabel
in interface TreeVisitor<R,P>
null
.node
- the node being visitedp
- a parameter valuepublic R visitPatternCaseLabel(PatternCaseLabelTree node, P p)
PatternCaseLabelTree
node.visitPatternCaseLabel
in interface TreeVisitor<R,P>
null
.node
- the node being visitedp
- a parameter valuepublic R visitDeconstructionPattern(DeconstructionPatternTree node, P p)
DeconstructionPatternTree
node.visitDeconstructionPattern
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitArrayAccess(ArrayAccessTree node, P p)
ArrayAccessTree
node. This implementation scans the children in left to right order.visitArrayAccess
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitMemberSelect(MemberSelectTree node, P p)
MemberSelectTree
node.visitMemberSelect
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitMemberReference(MemberReferenceTree node, P p)
MemberReferenceTree
node.visitMemberReference
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitIdentifier(IdentifierTree node, P p)
IdentifierTree
node.visitIdentifier
in interface TreeVisitor<R,P>
null
.node
- the node being visitedp
- a parameter valuepublic R visitLiteral(LiteralTree node, P p)
LiteralTree
node.visitLiteral
in interface TreeVisitor<R,P>
null
.node
- the node being visitedp
- a parameter valuepublic R visitPrimitiveType(PrimitiveTypeTree node, P p)
PrimitiveTypeTree
node.visitPrimitiveType
in interface TreeVisitor<R,P>
null
.node
- the node being visitedp
- a parameter valuepublic R visitArrayType(ArrayTypeTree node, P p)
ArrayTypeTree
node.visitArrayType
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitParameterizedType(ParameterizedTypeTree node, P p)
ParameterizedTypeTree
node.visitParameterizedType
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitUnionType(UnionTypeTree node, P p)
UnionTypeTree
node.visitUnionType
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitIntersectionType(IntersectionTypeTree node, P p)
IntersectionTypeTree
node.visitIntersectionType
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitTypeParameter(TypeParameterTree node, P p)
TypeParameterTree
node.visitTypeParameter
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitWildcard(WildcardTree node, P p)
WildcardTypeTree
node.visitWildcard
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitModifiers(ModifiersTree node, P p)
ModifiersTree
node.visitModifiers
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitAnnotation(AnnotationTree node, P p)
AnnotatedTree
node.visitAnnotation
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitAnnotatedType(AnnotatedTypeTree node, P p)
AnnotatedTypeTree
node.visitAnnotatedType
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitModule(ModuleTree node, P p)
ModuleTree
node.visitModule
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitExports(ExportsTree node, P p)
ExportsTree
node.visitExports
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitOpens(OpensTree node, P p)
OpensTree
node.visitOpens
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitProvides(ProvidesTree node, P p)
ProvidesTree
node.visitProvides
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitRequires(RequiresTree node, P p)
RequiresTree
node.visitRequires
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitUses(UsesTree node, P p)
UsesTree
node.visitUses
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter valuepublic R visitOther(Tree node, P p)
Tree
node. This can occur if the language evolves and new kinds of nodes are added to the Tree
hierarchy.visitOther
in interface TreeVisitor<R,P>
null
.node
- the node being visitedp
- a parameter valuepublic R visitErroneous(ErroneousTree node, P p)
ErroneousTree
node.visitErroneous
in interface TreeVisitor<R,P>
null
.node
- the node being visitedp
- a parameter valuepublic R visitYield(YieldTree node, P p)
YieldTree
node.visitYield
in interface TreeVisitor<R,P>
node
- the node being visitedp
- a parameter value
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/jdk.compiler/com/sun/source/util/TreeScanner.html
TreeScanner
when preview features are enabled.