1 /*
   2  * Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.source.util;
  27 
  28 import com.sun.source.tree.*;
  29 
  30 /**
  31  * A TreeVisitor that visits all the child tree nodes, and provides
  32  * support for maintaining a path for the parent nodes.
  33  * To visit nodes of a particular type, just override the
  34  * corresponding visitorXYZ method.
  35  * Inside your method, call super.visitXYZ to visit descendant
  36  * nodes.
  37  *
  38  * @apiNote
  39  * In order to initialize the "current path", the scan must be
  40  * started by calling one of the {@code scan} methods.
  41  *
  42  * @author Jonathan Gibbons
  43  * @since 1.6
  44  */
  45 public class TreePathScanner<R, P> extends TreeScanner<R, P> {
  46     /**
  47      * Constructs a {@code TreePathScanner}.
  48      */
  49     public TreePathScanner() {}
  50 
  51     /**
  52      * Scans a tree from a position identified by a TreePath.
  53      * @param path the path identifying the node to be scanned
  54      * @param p a parameter value passed to visit methods
  55      * @return the result value from the visit method
  56      */
  57     public R scan(TreePath path, P p) {
  58         this.path = path;
  59         try {
  60             return path.getLeaf().accept(this, p);
  61         } finally {
  62             this.path = null;
  63         }
  64     }
  65 
  66     /**
  67      * Scans a single node.
  68      * The current path is updated for the duration of the scan.
  69      *
  70      * @apiNote This method should normally only be called by the
  71      * scanner's {@code visit} methods, as part of an ongoing scan
  72      * initiated by {@link #scan(TreePath,Object) scan(TreePath, P)}.
  73      * The one exception is that it may also be called to initiate
  74      * a full scan of a {@link CompilationUnitTree}.
  75      *
  76      * @return the result value from the visit method
  77      */
  78     @Override
  79     public R scan(Tree tree, P p) {
  80         if (tree == null)
  81             return null;
  82 
  83         TreePath prev = path;
  84         path = new TreePath(path, tree);
  85         try {
  86             return tree.accept(this, p);
  87         } finally {
  88             path = prev;
  89         }
  90     }
  91 
  92     /**
  93      * Returns the current path for the node, as built up by the currently
  94      * active set of scan calls.
  95      * @return the current path
  96      */
  97     public TreePath getCurrentPath() {
  98         return path;
  99     }
 100 
 101     private TreePath path;
 102 }