FileDocCategorySizeDatePackage
TreeScanner.javaAPI DocJava SE 6 API10713Tue Jun 10 00:23:26 BST 2008com.sun.source.util

TreeScanner

public class TreeScanner extends Object implements TreeVisitor
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 {@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);
}
}
author
Peter von der Ahé
author
Jonathan Gibbons
since
1.6

Fields Summary
Constructors Summary
Methods Summary
public Rreduce(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 Rscan(com.sun.source.tree.Tree node, P p)
Scan a single node.

	return (node == null) ? null : node.accept(this, p);
    
public Rscan(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 RscanAndReduce(com.sun.source.tree.Tree node, P p, R r)

        return reduce(scan(node, p), r);
    
private RscanAndReduce(java.lang.Iterable nodes, P p, R r)

        return reduce(scan(nodes, p), r);
    
public RvisitAnnotation(com.sun.source.tree.AnnotationTree node, P p)

	R r = scan(node.getAnnotationType(), p);
	r = scanAndReduce(node.getArguments(), p, r);
        return r;
    
public RvisitArrayAccess(com.sun.source.tree.ArrayAccessTree node, P p)

	R r = scan(node.getExpression(), p);
	r = scanAndReduce(node.getIndex(), p, r);
        return r;
    
public RvisitArrayType(com.sun.source.tree.ArrayTypeTree node, P p)

    	return scan(node.getType(), p);
    
public RvisitAssert(com.sun.source.tree.AssertTree node, P p)

	R r = scan(node.getCondition(), p);
	r = scanAndReduce(node.getDetail(), p, r);
        return r;
    
public RvisitAssignment(com.sun.source.tree.AssignmentTree node, P p)

	R r = scan(node.getVariable(), p);
	r = scanAndReduce(node.getExpression(), p, r);
        return r;
    
public RvisitBinary(com.sun.source.tree.BinaryTree node, P p)

	R r = scan(node.getLeftOperand(), p);
	r = scanAndReduce(node.getRightOperand(), p, r);
        return r;
    
public RvisitBlock(com.sun.source.tree.BlockTree node, P p)

	return scan(node.getStatements(), p);
    
public RvisitBreak(com.sun.source.tree.BreakTree node, P p)

        return null;
    
public RvisitCase(com.sun.source.tree.CaseTree node, P p)

	R r = scan(node.getExpression(), p);
	r = scanAndReduce(node.getStatements(), p, r);
        return r;
    
public RvisitCatch(com.sun.source.tree.CatchTree node, P p)

	R r = scan(node.getParameter(), p);
	r = scanAndReduce(node.getBlock(), p, r);
        return r;
    
public RvisitClass(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 RvisitCompilationUnit(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 RvisitCompoundAssignment(com.sun.source.tree.CompoundAssignmentTree node, P p)

	R r = scan(node.getVariable(), p);
	r = scanAndReduce(node.getExpression(), p, r);
        return r;
    
public RvisitConditionalExpression(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 RvisitContinue(com.sun.source.tree.ContinueTree node, P p)

        return null;
    
public RvisitDoWhileLoop(com.sun.source.tree.DoWhileLoopTree node, P p)

	R r = scan(node.getStatement(), p);
	r = scanAndReduce(node.getCondition(), p, r);
        return r;
    
public RvisitEmptyStatement(com.sun.source.tree.EmptyStatementTree node, P p)

        return null;
    
public RvisitEnhancedForLoop(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 RvisitErroneous(com.sun.source.tree.ErroneousTree node, P p)

        return null;
    
public RvisitExpressionStatement(com.sun.source.tree.ExpressionStatementTree node, P p)

	return scan(node.getExpression(), p);
    
public RvisitForLoop(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 RvisitIdentifier(com.sun.source.tree.IdentifierTree node, P p)

        return null;
    
public RvisitIf(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 RvisitImport(com.sun.source.tree.ImportTree node, P p)

	return scan(node.getQualifiedIdentifier(), p);
    
public RvisitInstanceOf(com.sun.source.tree.InstanceOfTree node, P p)

	R r = scan(node.getExpression(), p);
	r = scanAndReduce(node.getType(), p, r);
        return r;
    
public RvisitLabeledStatement(com.sun.source.tree.LabeledStatementTree node, P p)

	return scan(node.getStatement(), p);
    
public RvisitLiteral(com.sun.source.tree.LiteralTree node, P p)

        return null;
    
public RvisitMemberSelect(com.sun.source.tree.MemberSelectTree node, P p)

	return scan(node.getExpression(), p);
    
public RvisitMethod(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 RvisitMethodInvocation(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 RvisitModifiers(com.sun.source.tree.ModifiersTree node, P p)

	return scan(node.getAnnotations(), p);
    
public RvisitNewArray(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 RvisitNewClass(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 RvisitOther(com.sun.source.tree.Tree node, P p)

	return null;
    
public RvisitParameterizedType(com.sun.source.tree.ParameterizedTypeTree node, P p)

	R r = scan(node.getType(), p);
	r = scanAndReduce(node.getTypeArguments(), p, r);
        return r;
    
public RvisitParenthesized(com.sun.source.tree.ParenthesizedTree node, P p)

	return scan(node.getExpression(), p);
    
public RvisitPrimitiveType(com.sun.source.tree.PrimitiveTypeTree node, P p)

        return null;
    
public RvisitReturn(com.sun.source.tree.ReturnTree node, P p)

	return scan(node.getExpression(), p);
    
public RvisitSwitch(com.sun.source.tree.SwitchTree node, P p)

	R r = scan(node.getExpression(), p);
	r = scanAndReduce(node.getCases(), p, r);
        return r;
    
public RvisitSynchronized(com.sun.source.tree.SynchronizedTree node, P p)

	R r = scan(node.getExpression(), p);
	r = scanAndReduce(node.getBlock(), p, r);
        return r;
    
public RvisitThrow(com.sun.source.tree.ThrowTree node, P p)

	return scan(node.getExpression(), p);
    
public RvisitTry(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 RvisitTypeCast(com.sun.source.tree.TypeCastTree node, P p)

	R r = scan(node.getType(), p);
	r = scanAndReduce(node.getExpression(), p, r);
        return r;
    
public RvisitTypeParameter(com.sun.source.tree.TypeParameterTree node, P p)

	return scan(node.getBounds(), p);
    
public RvisitUnary(com.sun.source.tree.UnaryTree node, P p)

	return scan(node.getExpression(), p);
    
public RvisitVariable(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 RvisitWhileLoop(com.sun.source.tree.WhileLoopTree node, P p)

	R r = scan(node.getCondition(), p);
	r = scanAndReduce(node.getStatement(), p, r);
        return r;
    
public RvisitWildcard(com.sun.source.tree.WildcardTree node, P p)

        return scan(node.getBound(), p);