author | fyang |
Thu, 24 May 2018 07:20:10 +0800 | |
changeset 50242 | 9a87afc49148 |
parent 48237 | ee130cca69e6 |
permissions | -rw-r--r-- |
41865 | 1 |
/* |
44063
5f0cf4126949
8175560: Drop String pkgName from javax.tools.JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName)
jlahoda
parents:
43773
diff
changeset
|
2 |
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. |
41865 | 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 |
package jdk.internal.shellsupport.doc; |
|
26 |
||
27 |
import java.io.IOException; |
|
28 |
import java.net.URI; |
|
29 |
import java.net.URISyntaxException; |
|
30 |
import java.nio.file.Path; |
|
31 |
import java.util.ArrayList; |
|
32 |
import java.util.Arrays; |
|
33 |
import java.util.Collection; |
|
34 |
import java.util.Comparator; |
|
35 |
import java.util.HashMap; |
|
36 |
import java.util.HashSet; |
|
37 |
import java.util.IdentityHashMap; |
|
38 |
import java.util.Iterator; |
|
39 |
import java.util.List; |
|
40 |
import java.util.Map; |
|
41 |
import java.util.Map.Entry; |
|
42 |
import java.util.Objects; |
|
43 |
import java.util.Set; |
|
44 |
import java.util.Stack; |
|
45 |
import java.util.TreeMap; |
|
46 |
import java.util.stream.Collectors; |
|
47 |
import java.util.stream.Stream; |
|
48 |
||
49 |
import javax.lang.model.element.Element; |
|
50 |
import javax.lang.model.element.ElementKind; |
|
51 |
import javax.lang.model.element.ExecutableElement; |
|
43773 | 52 |
import javax.lang.model.element.ModuleElement; |
41865 | 53 |
import javax.lang.model.element.TypeElement; |
54 |
import javax.lang.model.element.VariableElement; |
|
55 |
import javax.lang.model.type.DeclaredType; |
|
56 |
import javax.lang.model.type.TypeKind; |
|
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41865
diff
changeset
|
57 |
import javax.lang.model.type.TypeMirror; |
41865 | 58 |
import javax.lang.model.util.ElementFilter; |
43773 | 59 |
import javax.lang.model.util.Elements; |
60 |
import javax.tools.ForwardingJavaFileManager; |
|
41865 | 61 |
import javax.tools.JavaCompiler; |
62 |
import javax.tools.JavaFileManager; |
|
63 |
import javax.tools.JavaFileObject; |
|
48028
9e022f580a9d
8190552: Augment the Compiler API tree with APIs to represent HTML content
ksrini
parents:
47216
diff
changeset
|
64 |
import javax.tools.JavaFileObject.Kind; |
41865 | 65 |
import javax.tools.SimpleJavaFileObject; |
66 |
import javax.tools.StandardJavaFileManager; |
|
67 |
import javax.tools.StandardLocation; |
|
68 |
import javax.tools.ToolProvider; |
|
69 |
||
70 |
import com.sun.source.doctree.DocCommentTree; |
|
71 |
import com.sun.source.doctree.DocTree; |
|
72 |
import com.sun.source.doctree.InheritDocTree; |
|
73 |
import com.sun.source.doctree.ParamTree; |
|
74 |
import com.sun.source.doctree.ReturnTree; |
|
75 |
import com.sun.source.doctree.ThrowsTree; |
|
76 |
import com.sun.source.tree.ClassTree; |
|
77 |
import com.sun.source.tree.CompilationUnitTree; |
|
78 |
import com.sun.source.tree.MethodTree; |
|
79 |
import com.sun.source.tree.VariableTree; |
|
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
80 |
import com.sun.source.util.DocSourcePositions; |
41865 | 81 |
import com.sun.source.util.DocTreePath; |
82 |
import com.sun.source.util.DocTreeScanner; |
|
83 |
import com.sun.source.util.DocTrees; |
|
84 |
import com.sun.source.util.JavacTask; |
|
85 |
import com.sun.source.util.TreePath; |
|
86 |
import com.sun.source.util.TreePathScanner; |
|
87 |
import com.sun.source.util.Trees; |
|
88 |
import com.sun.tools.javac.api.JavacTaskImpl; |
|
89 |
import com.sun.tools.javac.util.DefinedBy; |
|
90 |
import com.sun.tools.javac.util.DefinedBy.Api; |
|
91 |
import com.sun.tools.javac.util.Pair; |
|
92 |
||
93 |
/**Helper to find javadoc and resolve @inheritDoc. |
|
94 |
*/ |
|
95 |
public abstract class JavadocHelper implements AutoCloseable { |
|
96 |
private static final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
|
97 |
||
98 |
/**Create the helper. |
|
99 |
* |
|
100 |
* @param mainTask JavacTask from which the further Elements originate |
|
101 |
* @param sourceLocations paths where source files should be searched |
|
102 |
* @return a JavadocHelper |
|
103 |
*/ |
|
104 |
public static JavadocHelper create(JavacTask mainTask, Collection<? extends Path> sourceLocations) { |
|
105 |
StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null); |
|
106 |
try { |
|
107 |
fm.setLocationFromPaths(StandardLocation.SOURCE_PATH, sourceLocations); |
|
108 |
return new OnDemandJavadocHelper(mainTask, fm); |
|
109 |
} catch (IOException ex) { |
|
110 |
try { |
|
111 |
fm.close(); |
|
112 |
} catch (IOException closeEx) { |
|
113 |
} |
|
114 |
return new JavadocHelper() { |
|
115 |
@Override |
|
116 |
public String getResolvedDocComment(Element forElement) throws IOException { |
|
117 |
return null; |
|
118 |
} |
|
119 |
@Override |
|
120 |
public Element getSourceElement(Element forElement) throws IOException { |
|
121 |
return forElement; |
|
122 |
} |
|
123 |
@Override |
|
124 |
public void close() throws IOException {} |
|
125 |
}; |
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
/**Returns javadoc for the given element, if it can be found, or null otherwise. The javadoc |
|
130 |
* will have @inheritDoc resolved. |
|
131 |
* |
|
132 |
* @param forElement element for which the javadoc should be searched |
|
133 |
* @return javadoc if found, null otherwise |
|
134 |
* @throws IOException if something goes wrong in the search |
|
135 |
*/ |
|
136 |
public abstract String getResolvedDocComment(Element forElement) throws IOException; |
|
137 |
||
138 |
/**Returns an element representing the same given program element, but the returned element will |
|
139 |
* be resolved from source, if it can be found. Returns the original element if the source for |
|
140 |
* the given element cannot be found. |
|
141 |
* |
|
142 |
* @param forElement element for which the source element should be searched |
|
143 |
* @return source element if found, the original element otherwise |
|
144 |
* @throws IOException if something goes wrong in the search |
|
145 |
*/ |
|
146 |
public abstract Element getSourceElement(Element forElement) throws IOException; |
|
147 |
||
148 |
/**Closes the helper. |
|
149 |
* |
|
150 |
* @throws IOException if something foes wrong during the close |
|
151 |
*/ |
|
152 |
@Override |
|
153 |
public abstract void close() throws IOException; |
|
154 |
||
155 |
private static final class OnDemandJavadocHelper extends JavadocHelper { |
|
156 |
private final JavacTask mainTask; |
|
157 |
private final JavaFileManager baseFileManager; |
|
158 |
private final StandardJavaFileManager fm; |
|
159 |
private final Map<String, Pair<JavacTask, TreePath>> signature2Source = new HashMap<>(); |
|
160 |
||
161 |
private OnDemandJavadocHelper(JavacTask mainTask, StandardJavaFileManager fm) { |
|
162 |
this.mainTask = mainTask; |
|
163 |
this.baseFileManager = ((JavacTaskImpl) mainTask).getContext().get(JavaFileManager.class); |
|
164 |
this.fm = fm; |
|
165 |
} |
|
166 |
||
167 |
@Override |
|
168 |
public String getResolvedDocComment(Element forElement) throws IOException { |
|
169 |
Pair<JavacTask, TreePath> sourceElement = getSourceElement(mainTask, forElement); |
|
170 |
||
171 |
if (sourceElement == null) |
|
172 |
return null; |
|
173 |
||
174 |
return getResolvedDocComment(sourceElement.fst, sourceElement.snd); |
|
175 |
} |
|
176 |
||
177 |
@Override |
|
178 |
public Element getSourceElement(Element forElement) throws IOException { |
|
179 |
Pair<JavacTask, TreePath> sourceElement = getSourceElement(mainTask, forElement); |
|
180 |
||
181 |
if (sourceElement == null) |
|
182 |
return forElement; |
|
183 |
||
184 |
Element result = Trees.instance(sourceElement.fst).getElement(sourceElement.snd); |
|
185 |
||
186 |
if (result == null) |
|
187 |
return forElement; |
|
188 |
||
189 |
return result; |
|
190 |
} |
|
191 |
||
192 |
private String getResolvedDocComment(JavacTask task, TreePath el) throws IOException { |
|
193 |
DocTrees trees = DocTrees.instance(task); |
|
194 |
Element element = trees.getElement(el); |
|
195 |
String docComment = trees.getDocComment(el); |
|
196 |
||
197 |
if (docComment == null && element.getKind() == ElementKind.METHOD) { |
|
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
198 |
//if a method does not have a javadoc, |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
199 |
//try to use javadoc from the methods overridden by this method: |
41865 | 200 |
ExecutableElement executableElement = (ExecutableElement) element; |
201 |
Iterable<Element> superTypes = |
|
202 |
() -> superTypeForInheritDoc(task, element.getEnclosingElement()).iterator(); |
|
203 |
for (Element sup : superTypes) { |
|
204 |
for (ExecutableElement supMethod : ElementFilter.methodsIn(sup.getEnclosedElements())) { |
|
205 |
TypeElement clazz = (TypeElement) executableElement.getEnclosingElement(); |
|
206 |
if (task.getElements().overrides(executableElement, supMethod, clazz)) { |
|
207 |
Pair<JavacTask, TreePath> source = getSourceElement(task, supMethod); |
|
208 |
||
209 |
if (source != null) { |
|
210 |
String overriddenComment = getResolvedDocComment(source.fst, source.snd); |
|
211 |
||
212 |
if (overriddenComment != null) { |
|
213 |
return overriddenComment; |
|
214 |
} |
|
215 |
} |
|
216 |
} |
|
217 |
} |
|
218 |
} |
|
219 |
} |
|
220 |
||
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
221 |
if (docComment == null) |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
222 |
return null; |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
223 |
|
48028
9e022f580a9d
8190552: Augment the Compiler API tree with APIs to represent HTML content
ksrini
parents:
47216
diff
changeset
|
224 |
Pair<DocCommentTree, Integer> parsed = parseDocComment(task, docComment); |
9e022f580a9d
8190552: Augment the Compiler API tree with APIs to represent HTML content
ksrini
parents:
47216
diff
changeset
|
225 |
DocCommentTree docCommentTree = parsed.fst; |
9e022f580a9d
8190552: Augment the Compiler API tree with APIs to represent HTML content
ksrini
parents:
47216
diff
changeset
|
226 |
int offset = parsed.snd; |
41865 | 227 |
IOException[] exception = new IOException[1]; |
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
228 |
Comparator<int[]> spanComp = |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
229 |
(span1, span2) -> span1[0] != span2[0] ? span2[0] - span1[0] |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
230 |
: span2[1] - span1[0]; |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
231 |
//spans in the docComment that should be replaced with the given Strings: |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
232 |
Map<int[], List<String>> replace = new TreeMap<>(spanComp); |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
233 |
DocSourcePositions sp = trees.getSourcePositions(); |
41865 | 234 |
|
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
235 |
//fill in missing elements and resolve {@inheritDoc} |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
236 |
//if an element is (silently) missing in the javadoc, a synthetic {@inheritDoc} |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
237 |
//is created for it. |
41865 | 238 |
new DocTreeScanner<Void, Void>() { |
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
239 |
/* enclosing doctree that may contain {@inheritDoc} (explicit or synthetic)*/ |
41865 | 240 |
private Stack<DocTree> interestingParent = new Stack<>(); |
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
241 |
/* current top-level DocCommentTree*/ |
41865 | 242 |
private DocCommentTree dcTree; |
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
243 |
/* javadoc from a super method from which we may copy elements.*/ |
41865 | 244 |
private String inherited; |
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
245 |
/* JavacTask from which inherited originates.*/ |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
246 |
private JavacTask inheritedJavacTask; |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
247 |
/* TreePath to the super method from which inherited originates.*/ |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
248 |
private TreePath inheritedTreePath; |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
249 |
/* Synthetic trees that contain {@inheritDoc} and |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
250 |
* texts which which they should be replaced.*/ |
41865 | 251 |
private Map<DocTree, String> syntheticTrees = new IdentityHashMap<>(); |
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
252 |
/* Position on which the synthetic trees should be inserted.*/ |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
253 |
private long insertPos = offset; |
41865 | 254 |
@Override @DefinedBy(Api.COMPILER_TREE) |
255 |
public Void visitDocComment(DocCommentTree node, Void p) { |
|
256 |
dcTree = node; |
|
257 |
interestingParent.push(node); |
|
258 |
try { |
|
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
259 |
if (node.getFullBody().isEmpty()) { |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
260 |
//there is no body in the javadoc, add synthetic {@inheritDoc}, which |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
261 |
//will be automatically filled in visitInheritDoc: |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
262 |
DocCommentTree dc = parseDocComment(task, "{@inheritDoc}").fst; |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
263 |
syntheticTrees.put(dc, "*\n"); |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
264 |
interestingParent.push(dc); |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
265 |
boolean prevInSynthetic = inSynthetic; |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
266 |
try { |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
267 |
inSynthetic = true; |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
268 |
scan(dc.getFirstSentence(), p); |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
269 |
scan(dc.getBody(), p); |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
270 |
} finally { |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
271 |
inSynthetic = prevInSynthetic; |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
272 |
interestingParent.pop(); |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
273 |
} |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
274 |
} else { |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
275 |
scan(node.getFirstSentence(), p); |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
276 |
scan(node.getBody(), p); |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
277 |
} |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
278 |
//add missing @param, @throws and @return, augmented with {@inheritDoc} |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
279 |
//which will be resolved in visitInheritDoc: |
41865 | 280 |
List<DocTree> augmentedBlockTags = new ArrayList<>(node.getBlockTags()); |
281 |
if (element.getKind() == ElementKind.METHOD) { |
|
282 |
ExecutableElement executableElement = (ExecutableElement) element; |
|
283 |
List<String> parameters = |
|
284 |
executableElement.getParameters() |
|
285 |
.stream() |
|
286 |
.map(param -> param.getSimpleName().toString()) |
|
287 |
.collect(Collectors.toList()); |
|
288 |
List<String> throwsList = |
|
289 |
executableElement.getThrownTypes() |
|
290 |
.stream() |
|
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41865
diff
changeset
|
291 |
.map(TypeMirror::toString) |
41865 | 292 |
.collect(Collectors.toList()); |
293 |
Set<String> missingParams = new HashSet<>(parameters); |
|
294 |
Set<String> missingThrows = new HashSet<>(throwsList); |
|
295 |
boolean hasReturn = false; |
|
296 |
||
297 |
for (DocTree dt : augmentedBlockTags) { |
|
298 |
switch (dt.getKind()) { |
|
299 |
case PARAM: |
|
300 |
missingParams.remove(((ParamTree) dt).getName().getName().toString()); |
|
301 |
break; |
|
302 |
case THROWS: |
|
303 |
missingThrows.remove(getThrownException(task, el, docCommentTree, (ThrowsTree) dt)); |
|
304 |
break; |
|
305 |
case RETURN: |
|
306 |
hasReturn = true; |
|
307 |
break; |
|
308 |
} |
|
309 |
} |
|
310 |
||
311 |
for (String missingParam : missingParams) { |
|
312 |
DocTree syntheticTag = parseBlockTag(task, "@param " + missingParam + " {@inheritDoc}"); |
|
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
313 |
syntheticTrees.put(syntheticTag, "@param " + missingParam + " *\n"); |
41865 | 314 |
insertTag(augmentedBlockTags, syntheticTag, parameters, throwsList); |
315 |
} |
|
316 |
||
317 |
for (String missingThrow : missingThrows) { |
|
318 |
DocTree syntheticTag = parseBlockTag(task, "@throws " + missingThrow + " {@inheritDoc}"); |
|
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
319 |
syntheticTrees.put(syntheticTag, "@throws " + missingThrow + " *\n"); |
41865 | 320 |
insertTag(augmentedBlockTags, syntheticTag, parameters, throwsList); |
321 |
} |
|
322 |
||
323 |
if (!hasReturn) { |
|
324 |
DocTree syntheticTag = parseBlockTag(task, "@return {@inheritDoc}"); |
|
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
325 |
syntheticTrees.put(syntheticTag, "@return *\n"); |
41865 | 326 |
insertTag(augmentedBlockTags, syntheticTag, parameters, throwsList); |
327 |
} |
|
328 |
} |
|
329 |
scan(augmentedBlockTags, p); |
|
330 |
return null; |
|
331 |
} finally { |
|
332 |
interestingParent.pop(); |
|
333 |
} |
|
334 |
} |
|
335 |
@Override @DefinedBy(Api.COMPILER_TREE) |
|
336 |
public Void visitParam(ParamTree node, Void p) { |
|
337 |
interestingParent.push(node); |
|
338 |
try { |
|
339 |
return super.visitParam(node, p); |
|
340 |
} finally { |
|
341 |
interestingParent.pop(); |
|
342 |
} |
|
343 |
} |
|
344 |
@Override @DefinedBy(Api.COMPILER_TREE) |
|
345 |
public Void visitThrows(ThrowsTree node, Void p) { |
|
346 |
interestingParent.push(node); |
|
347 |
try { |
|
348 |
return super.visitThrows(node, p); |
|
349 |
} finally { |
|
350 |
interestingParent.pop(); |
|
351 |
} |
|
352 |
} |
|
353 |
@Override @DefinedBy(Api.COMPILER_TREE) |
|
354 |
public Void visitReturn(ReturnTree node, Void p) { |
|
355 |
interestingParent.push(node); |
|
356 |
try { |
|
357 |
return super.visitReturn(node, p); |
|
358 |
} finally { |
|
359 |
interestingParent.pop(); |
|
360 |
} |
|
361 |
} |
|
362 |
@Override @DefinedBy(Api.COMPILER_TREE) |
|
363 |
public Void visitInheritDoc(InheritDocTree node, Void p) { |
|
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
364 |
//replace (schedule replacement into the replace map) |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
365 |
//{@inheritDoc} with the corresponding text from an overridden method |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
366 |
|
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
367 |
//first, fill in inherited, inheritedJavacTask and inheritedTreePath if not |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
368 |
//done yet: |
41865 | 369 |
if (inherited == null) { |
370 |
try { |
|
371 |
if (element.getKind() == ElementKind.METHOD) { |
|
372 |
ExecutableElement executableElement = (ExecutableElement) element; |
|
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
373 |
Iterable<ExecutableElement> superMethods = |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
374 |
() -> superMethodsForInheritDoc(task, executableElement). |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
375 |
iterator(); |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
376 |
for (Element supMethod : superMethods) { |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
377 |
Pair<JavacTask, TreePath> source = |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
378 |
getSourceElement(task, supMethod); |
41865 | 379 |
|
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
380 |
if (source != null) { |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
381 |
String overriddenComment = |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
382 |
getResolvedDocComment(source.fst, |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
383 |
source.snd); |
41865 | 384 |
|
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
385 |
if (overriddenComment != null) { |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
386 |
inheritedJavacTask = source.fst; |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
387 |
inheritedTreePath = source.snd; |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
388 |
inherited = overriddenComment; |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
389 |
break; |
41865 | 390 |
} |
391 |
} |
|
392 |
} |
|
393 |
} |
|
394 |
} catch (IOException ex) { |
|
395 |
exception[0] = ex; |
|
396 |
return null; |
|
397 |
} |
|
398 |
} |
|
399 |
if (inherited == null) { |
|
400 |
return null; |
|
401 |
} |
|
48028
9e022f580a9d
8190552: Augment the Compiler API tree with APIs to represent HTML content
ksrini
parents:
47216
diff
changeset
|
402 |
Pair<DocCommentTree, Integer> parsed = |
9e022f580a9d
8190552: Augment the Compiler API tree with APIs to represent HTML content
ksrini
parents:
47216
diff
changeset
|
403 |
parseDocComment(inheritedJavacTask, inherited); |
9e022f580a9d
8190552: Augment the Compiler API tree with APIs to represent HTML content
ksrini
parents:
47216
diff
changeset
|
404 |
DocCommentTree inheritedDocTree = parsed.fst; |
9e022f580a9d
8190552: Augment the Compiler API tree with APIs to represent HTML content
ksrini
parents:
47216
diff
changeset
|
405 |
int offset = parsed.snd; |
41865 | 406 |
List<List<? extends DocTree>> inheritedText = new ArrayList<>(); |
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
407 |
//find the corresponding piece in the inherited javadoc |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
408 |
//(interesting parent keeps the enclosing tree): |
41865 | 409 |
DocTree parent = interestingParent.peek(); |
410 |
switch (parent.getKind()) { |
|
411 |
case DOC_COMMENT: |
|
412 |
inheritedText.add(inheritedDocTree.getFullBody()); |
|
413 |
break; |
|
414 |
case PARAM: |
|
415 |
String paramName = ((ParamTree) parent).getName().getName().toString(); |
|
416 |
new DocTreeScanner<Void, Void>() { |
|
417 |
@Override @DefinedBy(Api.COMPILER_TREE) |
|
418 |
public Void visitParam(ParamTree node, Void p) { |
|
419 |
if (node.getName().getName().contentEquals(paramName)) { |
|
420 |
inheritedText.add(node.getDescription()); |
|
421 |
} |
|
422 |
return super.visitParam(node, p); |
|
423 |
} |
|
424 |
}.scan(inheritedDocTree, null); |
|
425 |
break; |
|
426 |
case THROWS: |
|
427 |
String thrownName = getThrownException(task, el, docCommentTree, (ThrowsTree) parent); |
|
428 |
new DocTreeScanner<Void, Void>() { |
|
429 |
@Override @DefinedBy(Api.COMPILER_TREE) |
|
430 |
public Void visitThrows(ThrowsTree node, Void p) { |
|
431 |
if (Objects.equals(getThrownException(inheritedJavacTask, inheritedTreePath, inheritedDocTree, node), thrownName)) { |
|
432 |
inheritedText.add(node.getDescription()); |
|
433 |
} |
|
434 |
return super.visitThrows(node, p); |
|
435 |
} |
|
436 |
}.scan(inheritedDocTree, null); |
|
437 |
break; |
|
438 |
case RETURN: |
|
439 |
new DocTreeScanner<Void, Void>() { |
|
440 |
@Override @DefinedBy(Api.COMPILER_TREE) |
|
441 |
public Void visitReturn(ReturnTree node, Void p) { |
|
442 |
inheritedText.add(node.getDescription()); |
|
443 |
return super.visitReturn(node, p); |
|
444 |
} |
|
445 |
}.scan(inheritedDocTree, null); |
|
446 |
break; |
|
447 |
} |
|
448 |
if (!inheritedText.isEmpty()) { |
|
449 |
long start = Long.MAX_VALUE; |
|
450 |
long end = Long.MIN_VALUE; |
|
451 |
||
452 |
for (DocTree t : inheritedText.get(0)) { |
|
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
453 |
start = Math.min(start, |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
454 |
sp.getStartPosition(null, inheritedDocTree, t) - offset); |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
455 |
end = Math.max(end, |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
456 |
sp.getEndPosition(null, inheritedDocTree, t) - offset); |
41865 | 457 |
} |
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
458 |
String text = end >= 0 ? inherited.substring((int) start, (int) end) : ""; |
41865 | 459 |
|
460 |
if (syntheticTrees.containsKey(parent)) { |
|
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
461 |
//if the {@inheritDoc} is inside a synthetic tree, don't delete anything, |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
462 |
//but insert the required text |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
463 |
//(insertPos is the position at which new stuff should be added): |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
464 |
int[] span = new int[] {(int) insertPos, (int) insertPos}; |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
465 |
replace.computeIfAbsent(span, s -> new ArrayList<>()) |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
466 |
.add(syntheticTrees.get(parent).replace("*", text)); |
41865 | 467 |
} else { |
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
468 |
//replace the {@inheritDoc} with the full text from |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
469 |
//the overridden method: |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
470 |
long inheritedStart = sp.getStartPosition(null, dcTree, node); |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
471 |
long inheritedEnd = sp.getEndPosition(null, dcTree, node); |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
472 |
int[] span = new int[] {(int) inheritedStart, (int) inheritedEnd}; |
41865 | 473 |
|
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
474 |
replace.computeIfAbsent(span, s -> new ArrayList<>()) |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
475 |
.add(text); |
41865 | 476 |
} |
477 |
} |
|
478 |
return super.visitInheritDoc(node, p); |
|
479 |
} |
|
480 |
private boolean inSynthetic; |
|
481 |
@Override @DefinedBy(Api.COMPILER_TREE) |
|
482 |
public Void scan(DocTree tree, Void p) { |
|
483 |
if (exception[0] != null) { |
|
484 |
return null; |
|
485 |
} |
|
486 |
boolean prevInSynthetic = inSynthetic; |
|
487 |
try { |
|
488 |
inSynthetic |= syntheticTrees.containsKey(tree); |
|
489 |
return super.scan(tree, p); |
|
490 |
} finally { |
|
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
491 |
if (!inSynthetic && tree != null) { |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
492 |
//for nonsynthetic trees, preserve the ending position as the future |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
493 |
//insertPos (as future missing elements should be inserted behind |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
494 |
//this tree) |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
495 |
//if there is a newline immediately behind this tree, insert behind |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
496 |
//the newline: |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
497 |
long endPos = sp.getEndPosition(null, dcTree, tree); |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
498 |
if (endPos >= 0) { |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
499 |
if (endPos - offset + 1 < docComment.length() && |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
500 |
docComment.charAt((int) (endPos - offset + 1)) == '\n') { |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
501 |
endPos++; |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
502 |
} |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
503 |
if (endPos - offset < docComment.length()) { |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
504 |
insertPos = endPos + 1; |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
505 |
} else { |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
506 |
insertPos = endPos; |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
507 |
} |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
508 |
} |
41865 | 509 |
} |
510 |
inSynthetic = prevInSynthetic; |
|
511 |
} |
|
512 |
} |
|
513 |
||
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
514 |
/* Insert a synthetic tag (toInsert) into the list of tags at |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
515 |
* an appropriate position.*/ |
41865 | 516 |
private void insertTag(List<DocTree> tags, DocTree toInsert, List<String> parameters, List<String> throwsTypes) { |
517 |
Comparator<DocTree> comp = (tag1, tag2) -> { |
|
518 |
if (tag1.getKind() == tag2.getKind()) { |
|
519 |
switch (toInsert.getKind()) { |
|
520 |
case PARAM: { |
|
521 |
ParamTree p1 = (ParamTree) tag1; |
|
522 |
ParamTree p2 = (ParamTree) tag2; |
|
523 |
int i1 = parameters.indexOf(p1.getName().getName().toString()); |
|
524 |
int i2 = parameters.indexOf(p2.getName().getName().toString()); |
|
525 |
||
526 |
return i1 - i2; |
|
527 |
} |
|
528 |
case THROWS: { |
|
529 |
ThrowsTree t1 = (ThrowsTree) tag1; |
|
530 |
ThrowsTree t2 = (ThrowsTree) tag2; |
|
531 |
int i1 = throwsTypes.indexOf(getThrownException(task, el, docCommentTree, t1)); |
|
532 |
int i2 = throwsTypes.indexOf(getThrownException(task, el, docCommentTree, t2)); |
|
533 |
||
534 |
return i1 - i2; |
|
535 |
} |
|
536 |
} |
|
537 |
} |
|
538 |
||
539 |
int i1 = tagOrder.indexOf(tag1.getKind()); |
|
540 |
int i2 = tagOrder.indexOf(tag2.getKind()); |
|
541 |
||
542 |
return i1 - i2; |
|
543 |
}; |
|
544 |
||
545 |
for (int i = 0; i < tags.size(); i++) { |
|
546 |
if (comp.compare(tags.get(i), toInsert) >= 0) { |
|
547 |
tags.add(i, toInsert); |
|
548 |
return ; |
|
549 |
} |
|
550 |
} |
|
551 |
tags.add(toInsert); |
|
552 |
} |
|
553 |
||
554 |
private final List<DocTree.Kind> tagOrder = Arrays.asList(DocTree.Kind.PARAM, DocTree.Kind.THROWS, DocTree.Kind.RETURN); |
|
555 |
}.scan(docCommentTree, null); |
|
556 |
||
557 |
if (replace.isEmpty()) |
|
558 |
return docComment; |
|
559 |
||
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
560 |
//do actually replace {@inheritDoc} with the new text (as scheduled by the visitor |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
561 |
//above): |
41865 | 562 |
StringBuilder replacedInheritDoc = new StringBuilder(docComment); |
563 |
||
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
564 |
for (Entry<int[], List<String>> e : replace.entrySet()) { |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
565 |
replacedInheritDoc.delete(e.getKey()[0] - offset, e.getKey()[1] - offset); |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
566 |
replacedInheritDoc.insert(e.getKey()[0] - offset, |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
567 |
e.getValue().stream().collect(Collectors.joining(""))); |
41865 | 568 |
} |
569 |
||
570 |
return replacedInheritDoc.toString(); |
|
571 |
} |
|
572 |
||
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
573 |
/* Find methods from which the given method may inherit javadoc, in the proper order.*/ |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
574 |
private Stream<ExecutableElement> superMethodsForInheritDoc(JavacTask task, |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
575 |
ExecutableElement method) { |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
576 |
TypeElement type = (TypeElement) method.getEnclosingElement(); |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
577 |
|
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
578 |
return this.superTypeForInheritDoc(task, type) |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
579 |
.flatMap(sup -> ElementFilter.methodsIn(sup.getEnclosedElements()).stream()) |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
580 |
.filter(supMethod -> task.getElements().overrides(method, supMethod, type)); |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
581 |
} |
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
582 |
|
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
583 |
/* Find types from which methods in type may inherit javadoc, in the proper order.*/ |
41865 | 584 |
private Stream<Element> superTypeForInheritDoc(JavacTask task, Element type) { |
585 |
TypeElement clazz = (TypeElement) type; |
|
586 |
Stream<Element> result = interfaces(clazz); |
|
587 |
result = Stream.concat(result, interfaces(clazz).flatMap(el -> superTypeForInheritDoc(task, el))); |
|
588 |
||
589 |
if (clazz.getSuperclass().getKind() == TypeKind.DECLARED) { |
|
590 |
Element superClass = ((DeclaredType) clazz.getSuperclass()).asElement(); |
|
591 |
result = Stream.concat(result, Stream.of(superClass)); |
|
592 |
result = Stream.concat(result, superTypeForInheritDoc(task, superClass)); |
|
593 |
} |
|
594 |
||
595 |
return result; |
|
596 |
} |
|
597 |
//where: |
|
598 |
private Stream<Element> interfaces(TypeElement clazz) { |
|
599 |
return clazz.getInterfaces() |
|
600 |
.stream() |
|
601 |
.filter(tm -> tm.getKind() == TypeKind.DECLARED) |
|
602 |
.map(tm -> ((DeclaredType) tm).asElement()); |
|
603 |
} |
|
604 |
||
605 |
private DocTree parseBlockTag(JavacTask task, String blockTag) { |
|
48028
9e022f580a9d
8190552: Augment the Compiler API tree with APIs to represent HTML content
ksrini
parents:
47216
diff
changeset
|
606 |
DocCommentTree dc = parseDocComment(task, blockTag).fst; |
41865 | 607 |
|
608 |
return dc.getBlockTags().get(0); |
|
609 |
} |
|
610 |
||
48028
9e022f580a9d
8190552: Augment the Compiler API tree with APIs to represent HTML content
ksrini
parents:
47216
diff
changeset
|
611 |
private Pair<DocCommentTree, Integer> parseDocComment(JavacTask task, String javadoc) { |
41865 | 612 |
DocTrees trees = DocTrees.instance(task); |
613 |
try { |
|
48028
9e022f580a9d
8190552: Augment the Compiler API tree with APIs to represent HTML content
ksrini
parents:
47216
diff
changeset
|
614 |
SimpleJavaFileObject fo = |
9e022f580a9d
8190552: Augment the Compiler API tree with APIs to represent HTML content
ksrini
parents:
47216
diff
changeset
|
615 |
new SimpleJavaFileObject(new URI("mem://doc.html"), Kind.HTML) { |
41865 | 616 |
@Override @DefinedBy(Api.COMPILER) |
48028
9e022f580a9d
8190552: Augment the Compiler API tree with APIs to represent HTML content
ksrini
parents:
47216
diff
changeset
|
617 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) |
9e022f580a9d
8190552: Augment the Compiler API tree with APIs to represent HTML content
ksrini
parents:
47216
diff
changeset
|
618 |
throws IOException { |
41865 | 619 |
return "<body>" + javadoc + "</body>"; |
620 |
} |
|
48028
9e022f580a9d
8190552: Augment the Compiler API tree with APIs to represent HTML content
ksrini
parents:
47216
diff
changeset
|
621 |
}; |
9e022f580a9d
8190552: Augment the Compiler API tree with APIs to represent HTML content
ksrini
parents:
47216
diff
changeset
|
622 |
DocCommentTree tree = trees.getDocCommentTree(fo); |
9e022f580a9d
8190552: Augment the Compiler API tree with APIs to represent HTML content
ksrini
parents:
47216
diff
changeset
|
623 |
int offset = (int) trees.getSourcePositions().getStartPosition(null, tree, tree); |
48237
ee130cca69e6
8189778: Jshell crash on tab for StringBuilder.append(
jlahoda
parents:
48028
diff
changeset
|
624 |
offset += "<body>".length(); |
48028
9e022f580a9d
8190552: Augment the Compiler API tree with APIs to represent HTML content
ksrini
parents:
47216
diff
changeset
|
625 |
return Pair.of(tree, offset); |
41865 | 626 |
} catch (URISyntaxException ex) { |
48028
9e022f580a9d
8190552: Augment the Compiler API tree with APIs to represent HTML content
ksrini
parents:
47216
diff
changeset
|
627 |
throw new IllegalStateException(ex); |
41865 | 628 |
} |
629 |
} |
|
630 |
||
631 |
private String getThrownException(JavacTask task, TreePath rootOn, DocCommentTree comment, ThrowsTree tt) { |
|
632 |
DocTrees trees = DocTrees.instance(task); |
|
633 |
Element exc = trees.getElement(new DocTreePath(new DocTreePath(rootOn, comment), tt.getExceptionName())); |
|
634 |
return exc != null ? exc.toString() : null; |
|
635 |
} |
|
636 |
||
637 |
private Pair<JavacTask, TreePath> getSourceElement(JavacTask origin, Element el) throws IOException { |
|
638 |
String handle = elementSignature(el); |
|
639 |
Pair<JavacTask, TreePath> cached = signature2Source.get(handle); |
|
640 |
||
641 |
if (cached != null) { |
|
642 |
return cached.fst != null ? cached : null; |
|
643 |
} |
|
644 |
||
645 |
TypeElement type = topLevelType(el); |
|
646 |
||
647 |
if (type == null) |
|
648 |
return null; |
|
649 |
||
43773 | 650 |
Elements elements = origin.getElements(); |
651 |
String binaryName = elements.getBinaryName(type).toString(); |
|
652 |
ModuleElement module = elements.getModuleOf(type); |
|
653 |
String moduleName = module == null || module.isUnnamed() |
|
654 |
? null |
|
655 |
: module.getQualifiedName().toString(); |
|
656 |
Pair<JavacTask, CompilationUnitTree> source = findSource(moduleName, binaryName); |
|
41865 | 657 |
|
658 |
if (source == null) |
|
659 |
return null; |
|
660 |
||
661 |
fillElementCache(source.fst, source.snd); |
|
662 |
||
663 |
cached = signature2Source.get(handle); |
|
664 |
||
665 |
if (cached != null) { |
|
666 |
return cached; |
|
667 |
} else { |
|
668 |
signature2Source.put(handle, Pair.of(null, null)); |
|
669 |
return null; |
|
670 |
} |
|
671 |
} |
|
672 |
//where: |
|
673 |
private String elementSignature(Element el) { |
|
674 |
switch (el.getKind()) { |
|
675 |
case ANNOTATION_TYPE: case CLASS: case ENUM: case INTERFACE: |
|
676 |
return ((TypeElement) el).getQualifiedName().toString(); |
|
677 |
case FIELD: |
|
678 |
return elementSignature(el.getEnclosingElement()) + "." + el.getSimpleName() + ":" + el.asType(); |
|
679 |
case ENUM_CONSTANT: |
|
680 |
return elementSignature(el.getEnclosingElement()) + "." + el.getSimpleName(); |
|
681 |
case EXCEPTION_PARAMETER: case LOCAL_VARIABLE: case PARAMETER: case RESOURCE_VARIABLE: |
|
682 |
return el.getSimpleName() + ":" + el.asType(); |
|
683 |
case CONSTRUCTOR: case METHOD: |
|
684 |
StringBuilder header = new StringBuilder(); |
|
685 |
header.append(elementSignature(el.getEnclosingElement())); |
|
686 |
if (el.getKind() == ElementKind.METHOD) { |
|
687 |
header.append("."); |
|
688 |
header.append(el.getSimpleName()); |
|
689 |
} |
|
690 |
header.append("("); |
|
691 |
String sep = ""; |
|
692 |
ExecutableElement method = (ExecutableElement) el; |
|
693 |
for (Iterator<? extends VariableElement> i = method.getParameters().iterator(); i.hasNext();) { |
|
694 |
VariableElement p = i.next(); |
|
695 |
header.append(sep); |
|
696 |
header.append(p.asType()); |
|
697 |
sep = ", "; |
|
698 |
} |
|
699 |
header.append(")"); |
|
700 |
return header.toString(); |
|
701 |
default: |
|
702 |
return el.toString(); |
|
703 |
} |
|
704 |
} |
|
705 |
||
706 |
private TypeElement topLevelType(Element el) { |
|
707 |
if (el.getKind() == ElementKind.PACKAGE) |
|
708 |
return null; |
|
709 |
||
710 |
while (el != null && el.getEnclosingElement().getKind() != ElementKind.PACKAGE) { |
|
711 |
el = el.getEnclosingElement(); |
|
712 |
} |
|
713 |
||
714 |
return el != null && (el.getKind().isClass() || el.getKind().isInterface()) ? (TypeElement) el : null; |
|
715 |
} |
|
716 |
||
717 |
private void fillElementCache(JavacTask task, CompilationUnitTree cut) throws IOException { |
|
718 |
Trees trees = Trees.instance(task); |
|
719 |
||
720 |
new TreePathScanner<Void, Void>() { |
|
721 |
@Override @DefinedBy(Api.COMPILER_TREE) |
|
722 |
public Void visitMethod(MethodTree node, Void p) { |
|
723 |
handleDeclaration(); |
|
724 |
return null; |
|
725 |
} |
|
726 |
||
727 |
@Override @DefinedBy(Api.COMPILER_TREE) |
|
728 |
public Void visitClass(ClassTree node, Void p) { |
|
729 |
handleDeclaration(); |
|
730 |
return super.visitClass(node, p); |
|
731 |
} |
|
732 |
||
733 |
@Override @DefinedBy(Api.COMPILER_TREE) |
|
734 |
public Void visitVariable(VariableTree node, Void p) { |
|
735 |
handleDeclaration(); |
|
736 |
return super.visitVariable(node, p); |
|
737 |
} |
|
738 |
||
739 |
private void handleDeclaration() { |
|
740 |
Element currentElement = trees.getElement(getCurrentPath()); |
|
741 |
||
742 |
if (currentElement != null) { |
|
743 |
signature2Source.put(elementSignature(currentElement), Pair.of(task, getCurrentPath())); |
|
744 |
} |
|
745 |
} |
|
746 |
}.scan(cut, null); |
|
747 |
} |
|
748 |
||
43773 | 749 |
private Pair<JavacTask, CompilationUnitTree> findSource(String moduleName, |
750 |
String binaryName) throws IOException { |
|
41865 | 751 |
JavaFileObject jfo = fm.getJavaFileForInput(StandardLocation.SOURCE_PATH, |
752 |
binaryName, |
|
753 |
JavaFileObject.Kind.SOURCE); |
|
754 |
||
755 |
if (jfo == null) |
|
756 |
return null; |
|
757 |
||
758 |
List<JavaFileObject> jfos = Arrays.asList(jfo); |
|
43773 | 759 |
JavaFileManager patchFM = moduleName != null |
760 |
? new PatchModuleFileManager(baseFileManager, jfo, moduleName) |
|
761 |
: baseFileManager; |
|
762 |
JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, patchFM, d -> {}, null, null, jfos); |
|
41865 | 763 |
Iterable<? extends CompilationUnitTree> cuts = task.parse(); |
764 |
||
765 |
task.enter(); |
|
766 |
||
767 |
return Pair.of(task, cuts.iterator().next()); |
|
768 |
} |
|
769 |
||
770 |
@Override |
|
771 |
public void close() throws IOException { |
|
772 |
fm.close(); |
|
773 |
} |
|
43773 | 774 |
|
775 |
private static final class PatchModuleFileManager |
|
776 |
extends ForwardingJavaFileManager<JavaFileManager> { |
|
777 |
||
778 |
private final JavaFileObject file; |
|
779 |
private final String moduleName; |
|
780 |
||
781 |
public PatchModuleFileManager(JavaFileManager fileManager, |
|
782 |
JavaFileObject file, |
|
783 |
String moduleName) { |
|
784 |
super(fileManager); |
|
785 |
this.file = file; |
|
786 |
this.moduleName = moduleName; |
|
787 |
} |
|
788 |
||
789 |
@Override @DefinedBy(Api.COMPILER) |
|
790 |
public Location getLocationForModule(Location location, |
|
44063
5f0cf4126949
8175560: Drop String pkgName from javax.tools.JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName)
jlahoda
parents:
43773
diff
changeset
|
791 |
JavaFileObject fo) throws IOException { |
43773 | 792 |
return fo == file |
793 |
? PATCH_LOCATION |
|
44063
5f0cf4126949
8175560: Drop String pkgName from javax.tools.JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName)
jlahoda
parents:
43773
diff
changeset
|
794 |
: super.getLocationForModule(location, fo); |
43773 | 795 |
} |
796 |
||
797 |
@Override @DefinedBy(Api.COMPILER) |
|
798 |
public String inferModuleName(Location location) throws IOException { |
|
799 |
return location == PATCH_LOCATION |
|
800 |
? moduleName |
|
801 |
: super.inferModuleName(location); |
|
802 |
} |
|
803 |
||
804 |
@Override @DefinedBy(Api.COMPILER) |
|
805 |
public boolean hasLocation(Location location) { |
|
806 |
return location == StandardLocation.PATCH_MODULE_PATH || |
|
807 |
super.hasLocation(location); |
|
808 |
} |
|
809 |
||
810 |
private static final Location PATCH_LOCATION = new Location() { |
|
811 |
@Override @DefinedBy(Api.COMPILER) |
|
812 |
public String getName() { |
|
813 |
return "PATCH_LOCATION"; |
|
814 |
} |
|
815 |
||
816 |
@Override @DefinedBy(Api.COMPILER) |
|
817 |
public boolean isOutputLocation() { |
|
818 |
return false; |
|
819 |
} |
|
820 |
||
821 |
@Override @DefinedBy(Api.COMPILER) |
|
822 |
public boolean isModuleOrientedLocation() { |
|
823 |
return false; |
|
824 |
} |
|
825 |
||
826 |
}; |
|
827 |
} |
|
41865 | 828 |
} |
829 |
||
830 |
} |