Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/source/util/DocTreePath.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 com.sun.source.doctree.DocCommentTree;28import com.sun.source.doctree.DocTree;29import java.util.Iterator;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 DocCommentTree node.34*35* @since 1.836*/37@jdk.Exported38public class DocTreePath implements Iterable<DocTree> {39/**40* Gets a documentation tree path for a tree node within a compilation unit.41* @return null if the node is not found42*/43public static DocTreePath getPath(TreePath treePath, DocCommentTree doc, DocTree target) {44return getPath(new DocTreePath(treePath, doc), target);45}4647/**48* Gets a documentation tree path for a tree node within a subtree identified by a DocTreePath object.49* @return null if the node is not found50*/51public static DocTreePath getPath(DocTreePath path, DocTree target) {52path.getClass();53target.getClass();5455class Result extends Error {56static final long serialVersionUID = -5942088234594905625L;57DocTreePath path;58Result(DocTreePath path) {59this.path = path;60}61}6263class PathFinder extends DocTreePathScanner<DocTreePath,DocTree> {64public DocTreePath scan(DocTree tree, DocTree target) {65if (tree == target) {66throw new Result(new DocTreePath(getCurrentPath(), target));67}68return super.scan(tree, target);69}70}7172if (path.getLeaf() == target) {73return path;74}7576try {77new PathFinder().scan(path, target);78} catch (Result result) {79return result.path;80}81return null;82}8384/**85* Creates a DocTreePath for a root node.86*87* @param treePath the TreePath from which the root node was created.88* @param t the DocCommentTree to create the path for.89*/90public DocTreePath(TreePath treePath, DocCommentTree t) {91treePath.getClass();92t.getClass();9394this.treePath = treePath;95this.docComment = t;96this.parent = null;97this.leaf = t;98}99100/**101* Creates a DocTreePath for a child node.102*/103public DocTreePath(DocTreePath p, DocTree t) {104if (t.getKind() == DocTree.Kind.DOC_COMMENT) {105throw new IllegalArgumentException("Use DocTreePath(TreePath, DocCommentTree) to construct DocTreePath for a DocCommentTree.");106} else {107treePath = p.treePath;108docComment = p.docComment;109parent = p;110}111leaf = t;112}113114/**115* Get the TreePath associated with this path.116* @return TreePath for this DocTreePath117*/118public TreePath getTreePath() {119return treePath;120}121122/**123* Get the DocCommentTree associated with this path.124* @return DocCommentTree for this DocTreePath125*/126public DocCommentTree getDocComment() {127return docComment;128}129130/**131* Get the leaf node for this path.132* @return DocTree for this DocTreePath133*/134public DocTree getLeaf() {135return leaf;136}137138/**139* Get the path for the enclosing node, or null if there is no enclosing node.140* @return DocTreePath of parent141*/142public DocTreePath getParentPath() {143return parent;144}145146public Iterator<DocTree> iterator() {147return new Iterator<DocTree>() {148public boolean hasNext() {149return next != null;150}151152public DocTree next() {153DocTree t = next.leaf;154next = next.parent;155return t;156}157158public void remove() {159throw new UnsupportedOperationException();160}161162private DocTreePath next = DocTreePath.this;163};164}165166private final TreePath treePath;167private final DocCommentTree docComment;168private final DocTree leaf;169private final DocTreePath parent;170}171172173