src/share/classes/com/sun/source/util/DocTreePath.java

Fri, 04 Oct 2013 10:00:28 -0700

author
darcy
date
Fri, 04 Oct 2013 10:00:28 -0700
changeset 2083
379c04c090cf
parent 1726
a7ff36d06fa2
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8025913: Rename jdk.Supported to jdk.Exported
Reviewed-by: psandoz, forax, lancea, alanb, mchung, jjg

jlahoda@1726 1 /*
jlahoda@1726 2 * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
jlahoda@1726 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jlahoda@1726 4 *
jlahoda@1726 5 * This code is free software; you can redistribute it and/or modify it
jlahoda@1726 6 * under the terms of the GNU General Public License version 2 only, as
jlahoda@1726 7 * published by the Free Software Foundation. Oracle designates this
jlahoda@1726 8 * particular file as subject to the "Classpath" exception as provided
jlahoda@1726 9 * by Oracle in the LICENSE file that accompanied this code.
jlahoda@1726 10 *
jlahoda@1726 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jlahoda@1726 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jlahoda@1726 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jlahoda@1726 14 * version 2 for more details (a copy is included in the LICENSE file that
jlahoda@1726 15 * accompanied this code).
jlahoda@1726 16 *
jlahoda@1726 17 * You should have received a copy of the GNU General Public License version
jlahoda@1726 18 * 2 along with this work; if not, write to the Free Software Foundation,
jlahoda@1726 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jlahoda@1726 20 *
jlahoda@1726 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jlahoda@1726 22 * or visit www.oracle.com if you need additional information or have any
jlahoda@1726 23 * questions.
jlahoda@1726 24 */
jlahoda@1726 25
jlahoda@1726 26 package com.sun.source.util;
jlahoda@1726 27
jlahoda@1726 28 import com.sun.source.doctree.DocCommentTree;
jlahoda@1726 29 import com.sun.source.doctree.DocTree;
jlahoda@1726 30 import java.util.Iterator;
jlahoda@1726 31
jlahoda@1726 32 /**
jlahoda@1726 33 * A path of tree nodes, typically used to represent the sequence of ancestor
jlahoda@1726 34 * nodes of a tree node up to the top level DocCommentTree node.
jlahoda@1726 35 *
jlahoda@1726 36 * @since 1.8
jlahoda@1726 37 */
darcy@2083 38 @jdk.Exported
jlahoda@1726 39 public class DocTreePath implements Iterable<DocTree> {
jlahoda@1726 40 /**
jlahoda@1726 41 * Gets a documentation tree path for a tree node within a compilation unit.
jlahoda@1726 42 * @return null if the node is not found
jlahoda@1726 43 */
jlahoda@1726 44 public static DocTreePath getPath(TreePath treePath, DocCommentTree doc, DocTree target) {
jlahoda@1726 45 return getPath(new DocTreePath(treePath, doc), target);
jlahoda@1726 46 }
jlahoda@1726 47
jlahoda@1726 48 /**
jlahoda@1726 49 * Gets a documentation tree path for a tree node within a subtree identified by a DocTreePath object.
jlahoda@1726 50 * @return null if the node is not found
jlahoda@1726 51 */
jlahoda@1726 52 public static DocTreePath getPath(DocTreePath path, DocTree target) {
jlahoda@1726 53 path.getClass();
jlahoda@1726 54 target.getClass();
jlahoda@1726 55
jlahoda@1726 56 class Result extends Error {
jlahoda@1726 57 static final long serialVersionUID = -5942088234594905625L;
jlahoda@1726 58 DocTreePath path;
jlahoda@1726 59 Result(DocTreePath path) {
jlahoda@1726 60 this.path = path;
jlahoda@1726 61 }
jlahoda@1726 62 }
jlahoda@1726 63
jlahoda@1726 64 class PathFinder extends DocTreePathScanner<DocTreePath,DocTree> {
jlahoda@1726 65 public DocTreePath scan(DocTree tree, DocTree target) {
jlahoda@1726 66 if (tree == target) {
jlahoda@1726 67 throw new Result(new DocTreePath(getCurrentPath(), target));
jlahoda@1726 68 }
jlahoda@1726 69 return super.scan(tree, target);
jlahoda@1726 70 }
jlahoda@1726 71 }
jlahoda@1726 72
jlahoda@1726 73 if (path.getLeaf() == target) {
jlahoda@1726 74 return path;
jlahoda@1726 75 }
jlahoda@1726 76
jlahoda@1726 77 try {
jlahoda@1726 78 new PathFinder().scan(path, target);
jlahoda@1726 79 } catch (Result result) {
jlahoda@1726 80 return result.path;
jlahoda@1726 81 }
jlahoda@1726 82 return null;
jlahoda@1726 83 }
jlahoda@1726 84
jlahoda@1726 85 /**
jlahoda@1726 86 * Creates a DocTreePath for a root node.
jlahoda@1726 87 *
jlahoda@1726 88 * @param treePath the TreePath from which the root node was created.
jlahoda@1726 89 * @param t the DocCommentTree to create the path for.
jlahoda@1726 90 */
jlahoda@1726 91 public DocTreePath(TreePath treePath, DocCommentTree t) {
jlahoda@1726 92 treePath.getClass();
jlahoda@1726 93 t.getClass();
jlahoda@1726 94
jlahoda@1726 95 this.treePath = treePath;
jlahoda@1726 96 this.docComment = t;
jlahoda@1726 97 this.parent = null;
jlahoda@1726 98 this.leaf = t;
jlahoda@1726 99 }
jlahoda@1726 100
jlahoda@1726 101 /**
jlahoda@1726 102 * Creates a DocTreePath for a child node.
jlahoda@1726 103 */
jlahoda@1726 104 public DocTreePath(DocTreePath p, DocTree t) {
jlahoda@1726 105 if (t.getKind() == DocTree.Kind.DOC_COMMENT) {
jlahoda@1726 106 throw new IllegalArgumentException("Use DocTreePath(TreePath, DocCommentTree) to construct DocTreePath for a DocCommentTree.");
jlahoda@1726 107 } else {
jlahoda@1726 108 treePath = p.treePath;
jlahoda@1726 109 docComment = p.docComment;
jlahoda@1726 110 parent = p;
jlahoda@1726 111 }
jlahoda@1726 112 leaf = t;
jlahoda@1726 113 }
jlahoda@1726 114
jlahoda@1726 115 /**
jlahoda@1726 116 * Get the TreePath associated with this path.
jlahoda@1726 117 * @return TreePath for this DocTreePath
jlahoda@1726 118 */
jlahoda@1726 119 public TreePath getTreePath() {
jlahoda@1726 120 return treePath;
jlahoda@1726 121 }
jlahoda@1726 122
jlahoda@1726 123 /**
jlahoda@1726 124 * Get the DocCommentTree associated with this path.
jlahoda@1726 125 * @return DocCommentTree for this DocTreePath
jlahoda@1726 126 */
jlahoda@1726 127 public DocCommentTree getDocComment() {
jlahoda@1726 128 return docComment;
jlahoda@1726 129 }
jlahoda@1726 130
jlahoda@1726 131 /**
jlahoda@1726 132 * Get the leaf node for this path.
jlahoda@1726 133 * @return DocTree for this DocTreePath
jlahoda@1726 134 */
jlahoda@1726 135 public DocTree getLeaf() {
jlahoda@1726 136 return leaf;
jlahoda@1726 137 }
jlahoda@1726 138
jlahoda@1726 139 /**
jlahoda@1726 140 * Get the path for the enclosing node, or null if there is no enclosing node.
jlahoda@1726 141 * @return DocTreePath of parent
jlahoda@1726 142 */
jlahoda@1726 143 public DocTreePath getParentPath() {
jlahoda@1726 144 return parent;
jlahoda@1726 145 }
jlahoda@1726 146
jlahoda@1726 147 public Iterator<DocTree> iterator() {
jlahoda@1726 148 return new Iterator<DocTree>() {
jlahoda@1726 149 public boolean hasNext() {
jlahoda@1726 150 return next != null;
jlahoda@1726 151 }
jlahoda@1726 152
jlahoda@1726 153 public DocTree next() {
jlahoda@1726 154 DocTree t = next.leaf;
jlahoda@1726 155 next = next.parent;
jlahoda@1726 156 return t;
jlahoda@1726 157 }
jlahoda@1726 158
jlahoda@1726 159 public void remove() {
jlahoda@1726 160 throw new UnsupportedOperationException();
jlahoda@1726 161 }
jlahoda@1726 162
jlahoda@1726 163 private DocTreePath next = DocTreePath.this;
jlahoda@1726 164 };
jlahoda@1726 165 }
jlahoda@1726 166
jlahoda@1726 167 private final TreePath treePath;
jlahoda@1726 168 private final DocCommentTree docComment;
jlahoda@1726 169 private final DocTree leaf;
jlahoda@1726 170 private final DocTreePath parent;
jlahoda@1726 171 }

mercurial