TreeScannerpublic class TreeScanner extends Object implements TreeVisitorA 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 {@code 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 {@code 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 {@link #reduce} method. Each child may be either
a simple node of a list of nodes. The default behavior of the {@code 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 {
{@literal @}Override
public Integer visitIdentifier(IdentifierTree node, Void p) {
return 1;
}
{@literal @}Override
public Integer reduce(Integer r1, Integer r2) {
return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);
}
}
|
Methods Summary |
---|
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.
return r1;
| public R | scan(com.sun.source.tree.Tree node, P p)Scan a single node.
return (node == null) ? null : node.accept(this, p);
| public R | scan(java.lang.Iterable nodes, P p)Scan a list of nodes.
R r = null;
if (nodes != null) {
boolean first = true;
for (Tree node : nodes) {
r = (first ? scan(node, p) : scanAndReduce(node, p, r));
first = false;
}
}
return r;
| private R | scanAndReduce(com.sun.source.tree.Tree node, P p, R r)
return reduce(scan(node, p), r);
| private R | scanAndReduce(java.lang.Iterable nodes, P p, R r)
return reduce(scan(nodes, p), r);
| public R | visitAnnotation(com.sun.source.tree.AnnotationTree node, P p)
R r = scan(node.getAnnotationType(), p);
r = scanAndReduce(node.getArguments(), p, r);
return r;
| public R | visitArrayAccess(com.sun.source.tree.ArrayAccessTree node, P p)
R r = scan(node.getExpression(), p);
r = scanAndReduce(node.getIndex(), p, r);
return r;
| public R | visitArrayType(com.sun.source.tree.ArrayTypeTree node, P p)
return scan(node.getType(), p);
| public R | visitAssert(com.sun.source.tree.AssertTree node, P p)
R r = scan(node.getCondition(), p);
r = scanAndReduce(node.getDetail(), p, r);
return r;
| public R | visitAssignment(com.sun.source.tree.AssignmentTree node, P p)
R r = scan(node.getVariable(), p);
r = scanAndReduce(node.getExpression(), p, r);
return r;
| public R | visitBinary(com.sun.source.tree.BinaryTree node, P p)
R r = scan(node.getLeftOperand(), p);
r = scanAndReduce(node.getRightOperand(), p, r);
return r;
| public R | visitBlock(com.sun.source.tree.BlockTree node, P p)
return scan(node.getStatements(), p);
| public R | visitBreak(com.sun.source.tree.BreakTree node, P p)
return null;
| public R | visitCase(com.sun.source.tree.CaseTree node, P p)
R r = scan(node.getExpression(), p);
r = scanAndReduce(node.getStatements(), p, r);
return r;
| public R | visitCatch(com.sun.source.tree.CatchTree node, P p)
R r = scan(node.getParameter(), p);
r = scanAndReduce(node.getBlock(), p, r);
return r;
| public R | visitClass(com.sun.source.tree.ClassTree node, P p)
R r = scan(node.getModifiers(), p);
r = scanAndReduce(node.getTypeParameters(), p, r);
r = scanAndReduce(node.getExtendsClause(), p, r);
r = scanAndReduce(node.getImplementsClause(), p, r);
r = scanAndReduce(node.getMembers(), p, r);
return r;
| public R | visitCompilationUnit(com.sun.source.tree.CompilationUnitTree node, P p)
R r = scan(node.getPackageAnnotations(), p);
r = scanAndReduce(node.getPackageName(), p, r);
r = scanAndReduce(node.getImports(), p, r);
r = scanAndReduce(node.getTypeDecls(), p, r);
return r;
| public R | visitCompoundAssignment(com.sun.source.tree.CompoundAssignmentTree node, P p)
R r = scan(node.getVariable(), p);
r = scanAndReduce(node.getExpression(), p, r);
return r;
| public R | visitConditionalExpression(com.sun.source.tree.ConditionalExpressionTree node, P p)
R r = scan(node.getCondition(), p);
r = scanAndReduce(node.getTrueExpression(), p, r);
r = scanAndReduce(node.getFalseExpression(), p, r);
return r;
| public R | visitContinue(com.sun.source.tree.ContinueTree node, P p)
return null;
| public R | visitDoWhileLoop(com.sun.source.tree.DoWhileLoopTree node, P p)
R r = scan(node.getStatement(), p);
r = scanAndReduce(node.getCondition(), p, r);
return r;
| public R | visitEmptyStatement(com.sun.source.tree.EmptyStatementTree node, P p)
return null;
| public R | visitEnhancedForLoop(com.sun.source.tree.EnhancedForLoopTree node, P p)
R r = scan(node.getVariable(), p);
r = scanAndReduce(node.getExpression(), p, r);
r = scanAndReduce(node.getStatement(), p, r);
return r;
| public R | visitErroneous(com.sun.source.tree.ErroneousTree node, P p)
return null;
| public R | visitExpressionStatement(com.sun.source.tree.ExpressionStatementTree node, P p)
return scan(node.getExpression(), p);
| public R | visitForLoop(com.sun.source.tree.ForLoopTree node, P p)
R r = scan(node.getInitializer(), p);
r = scanAndReduce(node.getCondition(), p, r);
r = scanAndReduce(node.getUpdate(), p, r);
r = scanAndReduce(node.getStatement(), p, r);
return r;
| public R | visitIdentifier(com.sun.source.tree.IdentifierTree node, P p)
return null;
| public R | visitIf(com.sun.source.tree.IfTree node, P p)
R r = scan(node.getCondition(), p);
r = scanAndReduce(node.getThenStatement(), p, r);
r = scanAndReduce(node.getElseStatement(), p, r);
return r;
| public R | visitImport(com.sun.source.tree.ImportTree node, P p)
return scan(node.getQualifiedIdentifier(), p);
| public R | visitInstanceOf(com.sun.source.tree.InstanceOfTree node, P p)
R r = scan(node.getExpression(), p);
r = scanAndReduce(node.getType(), p, r);
return r;
| public R | visitLabeledStatement(com.sun.source.tree.LabeledStatementTree node, P p)
return scan(node.getStatement(), p);
| public R | visitLiteral(com.sun.source.tree.LiteralTree node, P p)
return null;
| public R | visitMemberSelect(com.sun.source.tree.MemberSelectTree node, P p)
return scan(node.getExpression(), p);
| public R | visitMethod(com.sun.source.tree.MethodTree node, P p)
R r = scan(node.getModifiers(), p);
r = scanAndReduce(node.getReturnType(), p, r);
r = scanAndReduce(node.getTypeParameters(), p, r);
r = scanAndReduce(node.getParameters(), p, r);
r = scanAndReduce(node.getThrows(), p, r);
r = scanAndReduce(node.getBody(), p, r);
return r;
| public R | visitMethodInvocation(com.sun.source.tree.MethodInvocationTree node, P p)
R r = scan(node.getTypeArguments(), p);
r = scanAndReduce(node.getMethodSelect(), p, r);
r = scanAndReduce(node.getArguments(), p, r);
return r;
| public R | visitModifiers(com.sun.source.tree.ModifiersTree node, P p)
return scan(node.getAnnotations(), p);
| public R | visitNewArray(com.sun.source.tree.NewArrayTree node, P p)
R r = scan(node.getType(), p);
r = scanAndReduce(node.getDimensions(), p, r);
r = scanAndReduce(node.getInitializers(), p, r);
return r;
| public R | visitNewClass(com.sun.source.tree.NewClassTree node, P p)
R r = scan(node.getEnclosingExpression(), p);
r = scanAndReduce(node.getIdentifier(), p, r);
r = scanAndReduce(node.getTypeArguments(), p, r);
r = scanAndReduce(node.getArguments(), p, r);
r = scanAndReduce(node.getClassBody(), p, r);
return r;
| public R | visitOther(com.sun.source.tree.Tree node, P p)
return null;
| public R | visitParameterizedType(com.sun.source.tree.ParameterizedTypeTree node, P p)
R r = scan(node.getType(), p);
r = scanAndReduce(node.getTypeArguments(), p, r);
return r;
| public R | visitParenthesized(com.sun.source.tree.ParenthesizedTree node, P p)
return scan(node.getExpression(), p);
| public R | visitPrimitiveType(com.sun.source.tree.PrimitiveTypeTree node, P p)
return null;
| public R | visitReturn(com.sun.source.tree.ReturnTree node, P p)
return scan(node.getExpression(), p);
| public R | visitSwitch(com.sun.source.tree.SwitchTree node, P p)
R r = scan(node.getExpression(), p);
r = scanAndReduce(node.getCases(), p, r);
return r;
| public R | visitSynchronized(com.sun.source.tree.SynchronizedTree node, P p)
R r = scan(node.getExpression(), p);
r = scanAndReduce(node.getBlock(), p, r);
return r;
| public R | visitThrow(com.sun.source.tree.ThrowTree node, P p)
return scan(node.getExpression(), p);
| public R | visitTry(com.sun.source.tree.TryTree node, P p)
R r = scan(node.getBlock(), p);
r = scanAndReduce(node.getCatches(), p, r);
r = scanAndReduce(node.getFinallyBlock(), p, r);
return r;
| public R | visitTypeCast(com.sun.source.tree.TypeCastTree node, P p)
R r = scan(node.getType(), p);
r = scanAndReduce(node.getExpression(), p, r);
return r;
| public R | visitTypeParameter(com.sun.source.tree.TypeParameterTree node, P p)
return scan(node.getBounds(), p);
| public R | visitUnary(com.sun.source.tree.UnaryTree node, P p)
return scan(node.getExpression(), p);
| public R | visitVariable(com.sun.source.tree.VariableTree node, P p)
R r = scan(node.getModifiers(), p);
r = scanAndReduce(node.getType(), p, r);
r = scanAndReduce(node.getInitializer(), p, r);
return r;
| public R | visitWhileLoop(com.sun.source.tree.WhileLoopTree node, P p)
R r = scan(node.getCondition(), p);
r = scanAndReduce(node.getStatement(), p, r);
return r;
| public R | visitWildcard(com.sun.source.tree.WildcardTree node, P p)
return scan(node.getBound(), p);
|
|