Module jdk.compiler

Class TreeScanner<R,P>

java.lang.Object
com.sun.source.util.TreeScanner<R,P>
Type Parameters:
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.
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.

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);
      }
   }
 
Implementation Requirements:

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 with 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 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.
Since:
1.6