17549
|
1 |
/*
|
25287
|
2 |
* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
|
17549
|
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.doctree.DocCommentTree;
|
|
29 |
import com.sun.source.doctree.DocTree;
|
|
30 |
import java.util.Iterator;
|
|
31 |
|
|
32 |
/**
|
|
33 |
* A path of tree nodes, typically used to represent the sequence of ancestor
|
|
34 |
* nodes of a tree node up to the top level DocCommentTree node.
|
|
35 |
*
|
|
36 |
* @since 1.8
|
|
37 |
*/
|
20612
|
38 |
@jdk.Exported
|
17549
|
39 |
public class DocTreePath implements Iterable<DocTree> {
|
|
40 |
/**
|
25287
|
41 |
* Returns a documentation tree path for a tree node within a compilation unit,
|
|
42 |
* or {@code null} if the node is not found.
|
|
43 |
* @param treePath the path for the node with which the doc comment is associated
|
|
44 |
* @param doc the doc comment associated with the node
|
|
45 |
* @param target a node within the doc comment
|
|
46 |
* @return a path identifying the target within the tree
|
17549
|
47 |
*/
|
|
48 |
public static DocTreePath getPath(TreePath treePath, DocCommentTree doc, DocTree target) {
|
|
49 |
return getPath(new DocTreePath(treePath, doc), target);
|
|
50 |
}
|
|
51 |
|
|
52 |
/**
|
25287
|
53 |
* Returns a documentation tree path for a tree node within a subtree
|
|
54 |
* identified by a DocTreePath object, or {@code null} if the node is not found.
|
|
55 |
* @param path a path identifying a node within a doc comment tree
|
|
56 |
* @param target a node to be located within the given node
|
|
57 |
* @return a path identifying the target node
|
17549
|
58 |
*/
|
|
59 |
public static DocTreePath getPath(DocTreePath path, DocTree target) {
|
|
60 |
path.getClass();
|
|
61 |
target.getClass();
|
|
62 |
|
|
63 |
class Result extends Error {
|
|
64 |
static final long serialVersionUID = -5942088234594905625L;
|
|
65 |
DocTreePath path;
|
|
66 |
Result(DocTreePath path) {
|
|
67 |
this.path = path;
|
|
68 |
}
|
|
69 |
}
|
|
70 |
|
|
71 |
class PathFinder extends DocTreePathScanner<DocTreePath,DocTree> {
|
|
72 |
public DocTreePath scan(DocTree tree, DocTree target) {
|
|
73 |
if (tree == target) {
|
|
74 |
throw new Result(new DocTreePath(getCurrentPath(), target));
|
|
75 |
}
|
|
76 |
return super.scan(tree, target);
|
|
77 |
}
|
|
78 |
}
|
|
79 |
|
|
80 |
if (path.getLeaf() == target) {
|
|
81 |
return path;
|
|
82 |
}
|
|
83 |
|
|
84 |
try {
|
|
85 |
new PathFinder().scan(path, target);
|
|
86 |
} catch (Result result) {
|
|
87 |
return result.path;
|
|
88 |
}
|
|
89 |
return null;
|
|
90 |
}
|
|
91 |
|
|
92 |
/**
|
|
93 |
* Creates a DocTreePath for a root node.
|
|
94 |
*
|
|
95 |
* @param treePath the TreePath from which the root node was created.
|
|
96 |
* @param t the DocCommentTree to create the path for.
|
|
97 |
*/
|
|
98 |
public DocTreePath(TreePath treePath, DocCommentTree t) {
|
|
99 |
treePath.getClass();
|
|
100 |
t.getClass();
|
|
101 |
|
|
102 |
this.treePath = treePath;
|
|
103 |
this.docComment = t;
|
|
104 |
this.parent = null;
|
|
105 |
this.leaf = t;
|
|
106 |
}
|
|
107 |
|
|
108 |
/**
|
|
109 |
* Creates a DocTreePath for a child node.
|
25287
|
110 |
* @param p the parent node
|
|
111 |
* @param t the child node
|
17549
|
112 |
*/
|
|
113 |
public DocTreePath(DocTreePath p, DocTree t) {
|
|
114 |
if (t.getKind() == DocTree.Kind.DOC_COMMENT) {
|
|
115 |
throw new IllegalArgumentException("Use DocTreePath(TreePath, DocCommentTree) to construct DocTreePath for a DocCommentTree.");
|
|
116 |
} else {
|
|
117 |
treePath = p.treePath;
|
|
118 |
docComment = p.docComment;
|
|
119 |
parent = p;
|
|
120 |
}
|
|
121 |
leaf = t;
|
|
122 |
}
|
|
123 |
|
|
124 |
/**
|
25287
|
125 |
* Returns the TreePath associated with this path.
|
|
126 |
* @return the TreePath for this DocTreePath
|
17549
|
127 |
*/
|
|
128 |
public TreePath getTreePath() {
|
|
129 |
return treePath;
|
|
130 |
}
|
|
131 |
|
|
132 |
/**
|
25287
|
133 |
* Returns the DocCommentTree associated with this path.
|
|
134 |
* @return the DocCommentTree for this DocTreePath
|
17549
|
135 |
*/
|
|
136 |
public DocCommentTree getDocComment() {
|
|
137 |
return docComment;
|
|
138 |
}
|
|
139 |
|
|
140 |
/**
|
25287
|
141 |
* Returns the leaf node for this path.
|
|
142 |
* @return the DocTree for this DocTreePath
|
17549
|
143 |
*/
|
|
144 |
public DocTree getLeaf() {
|
|
145 |
return leaf;
|
|
146 |
}
|
|
147 |
|
|
148 |
/**
|
25287
|
149 |
* Returns the path for the enclosing node, or {@code null} if there is no enclosing node.
|
17549
|
150 |
* @return DocTreePath of parent
|
|
151 |
*/
|
|
152 |
public DocTreePath getParentPath() {
|
|
153 |
return parent;
|
|
154 |
}
|
|
155 |
|
|
156 |
public Iterator<DocTree> iterator() {
|
|
157 |
return new Iterator<DocTree>() {
|
|
158 |
public boolean hasNext() {
|
|
159 |
return next != null;
|
|
160 |
}
|
|
161 |
|
|
162 |
public DocTree next() {
|
|
163 |
DocTree t = next.leaf;
|
|
164 |
next = next.parent;
|
|
165 |
return t;
|
|
166 |
}
|
|
167 |
|
|
168 |
public void remove() {
|
|
169 |
throw new UnsupportedOperationException();
|
|
170 |
}
|
|
171 |
|
|
172 |
private DocTreePath next = DocTreePath.this;
|
|
173 |
};
|
|
174 |
}
|
|
175 |
|
|
176 |
private final TreePath treePath;
|
|
177 |
private final DocCommentTree docComment;
|
|
178 |
private final DocTree leaf;
|
|
179 |
private final DocTreePath parent;
|
|
180 |
}
|