Path: blob/master/src/jdk.compiler/share/classes/com/sun/source/util/TreePathScanner.java
41175 views
/*1* Copyright (c) 2006, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.source.util;2627import com.sun.source.tree.*;2829/**30* A TreeVisitor that visits all the child tree nodes, and provides31* support for maintaining a path for the parent nodes.32* To visit nodes of a particular type, just override the33* corresponding visitorXYZ method.34* Inside your method, call super.visitXYZ to visit descendant35* nodes.36*37* @apiNote38* In order to initialize the "current path", the scan must be39* started by calling one of the {@code scan} methods.40*41* @author Jonathan Gibbons42* @since 1.643*/44public class TreePathScanner<R, P> extends TreeScanner<R, P> {45/**46* Constructs a {@code TreePathScanner}.47*/48public TreePathScanner() {}4950/**51* Scans a tree from a position identified by a TreePath.52* @param path the path identifying the node to be scanned53* @param p a parameter value passed to visit methods54* @return the result value from the visit method55*/56public R scan(TreePath path, P p) {57this.path = path;58try {59return path.getLeaf().accept(this, p);60} finally {61this.path = null;62}63}6465/**66* Scans a single node.67* The current path is updated for the duration of the scan.68*69* @apiNote This method should normally only be called by the70* scanner's {@code visit} methods, as part of an ongoing scan71* initiated by {@link #scan(TreePath,Object) scan(TreePath, P)}.72* The one exception is that it may also be called to initiate73* a full scan of a {@link CompilationUnitTree}.74*75* @return the result value from the visit method76*/77@Override78public R scan(Tree tree, P p) {79if (tree == null)80return null;8182TreePath prev = path;83path = new TreePath(path, tree);84try {85return tree.accept(this, p);86} finally {87path = prev;88}89}9091/**92* Returns the current path for the node, as built up by the currently93* active set of scan calls.94* @return the current path95*/96public TreePath getCurrentPath() {97return path;98}99100private TreePath path;101}102103104