Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/source/util/TreePath.java
38899 views
/*1* Copyright (c) 2006, 2013, 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 java.util.Iterator;2829import com.sun.source.tree.*;3031/**32* A path of tree nodes, typically used to represent the sequence of ancestor33* nodes of a tree node up to the top level CompilationUnitTree node.34*35* @author Jonathan Gibbons36* @since 1.637*/38@jdk.Exported39public class TreePath implements Iterable<Tree> {40/**41* Gets a tree path for a tree node within a compilation unit.42* @return null if the node is not found43*/44public static TreePath getPath(CompilationUnitTree unit, Tree target) {45return getPath(new TreePath(unit), target);46}4748/**49* Gets a tree path for a tree node within a subtree identified by a TreePath object.50* @return null if the node is not found51*/52public static TreePath getPath(TreePath path, Tree target) {53path.getClass();54target.getClass();5556class Result extends Error {57static final long serialVersionUID = -5942088234594905625L;58TreePath path;59Result(TreePath path) {60this.path = path;61}62}6364class PathFinder extends TreePathScanner<TreePath,Tree> {65public TreePath scan(Tree tree, Tree target) {66if (tree == target) {67throw new Result(new TreePath(getCurrentPath(), target));68}69return super.scan(tree, target);70}71}7273if (path.getLeaf() == target) {74return path;75}7677try {78new PathFinder().scan(path, target);79} catch (Result result) {80return result.path;81}82return null;83}8485/**86* Creates a TreePath for a root node.87*/88public TreePath(CompilationUnitTree t) {89this(null, t);90}9192/**93* Creates a TreePath for a child node.94*/95public TreePath(TreePath p, Tree t) {96if (t.getKind() == Tree.Kind.COMPILATION_UNIT) {97compilationUnit = (CompilationUnitTree) t;98parent = null;99}100else {101compilationUnit = p.compilationUnit;102parent = p;103}104leaf = t;105}106/**107* Get the compilation unit associated with this path.108*/109public CompilationUnitTree getCompilationUnit() {110return compilationUnit;111}112113/**114* Get the leaf node for this path.115*/116public Tree getLeaf() {117return leaf;118}119120/**121* Get the path for the enclosing node, or null if there is no enclosing node.122*/123public TreePath getParentPath() {124return parent;125}126127/**128* Iterates from leaves to root.129*/130@Override131public Iterator<Tree> iterator() {132return new Iterator<Tree>() {133@Override134public boolean hasNext() {135return next != null;136}137138@Override139public Tree next() {140Tree t = next.leaf;141next = next.parent;142return t;143}144145@Override146public void remove() {147throw new UnsupportedOperationException();148}149150private TreePath next = TreePath.this;151};152}153154private CompilationUnitTree compilationUnit;155private Tree leaf;156private TreePath parent;157}158159160