Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/langtools/jdk/internal/shellsupport/doc/JavadocHelperTest.java
41153 views
1
/*
2
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 8131019 8189778 8190552
27
* @summary Test JavadocHelper
28
* @library /tools/lib
29
* @modules jdk.compiler/com.sun.tools.javac.api
30
* jdk.compiler/com.sun.tools.javac.main
31
* jdk.compiler/jdk.internal.shellsupport.doc
32
* @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
33
* @run testng/timeout=900/othervm JavadocHelperTest
34
*/
35
36
import java.io.IOException;
37
import java.net.URI;
38
import java.net.URISyntaxException;
39
import java.nio.file.DirectoryStream;
40
import java.nio.file.FileSystem;
41
import java.nio.file.FileSystems;
42
import java.nio.file.Files;
43
import java.nio.file.Path;
44
import java.nio.file.Paths;
45
import java.util.ArrayList;
46
import java.util.Arrays;
47
import java.util.Collections;
48
import java.util.List;
49
import java.util.function.Function;
50
import java.util.jar.JarEntry;
51
import java.util.jar.JarOutputStream;
52
53
import javax.lang.model.element.Element;
54
import javax.lang.model.element.ModuleElement;
55
import javax.lang.model.element.ModuleElement.ExportsDirective;
56
import javax.lang.model.element.TypeElement;
57
import javax.lang.model.util.ElementFilter;
58
import javax.tools.Diagnostic.Kind;
59
import javax.tools.DiagnosticListener;
60
import javax.tools.JavaCompiler;
61
import javax.tools.JavaFileObject;
62
import javax.tools.SimpleJavaFileObject;
63
import javax.tools.StandardJavaFileManager;
64
import javax.tools.StandardLocation;
65
import javax.tools.ToolProvider;
66
67
import com.sun.source.util.JavacTask;
68
import jdk.internal.shellsupport.doc.JavadocHelper;
69
import org.testng.annotations.Test;
70
import static org.testng.Assert.assertEquals;
71
import static org.testng.Assert.assertTrue;
72
73
@Test
74
public class JavadocHelperTest {
75
76
public void testJavadoc() throws Exception {
77
doTestJavadoc("",
78
t -> t.getElements().getTypeElement("test.Super"),
79
"Top level. ");
80
doTestJavadoc("",
81
t -> getFirstMethod(t, "test.Super"),
82
" javadoc1\n" +
83
"\n" +
84
" @param p1 param1\n" +
85
" @param p2 param2\n" +
86
" @param p3 param3\n" +
87
" @throws IllegalStateException exc1\n" +
88
" @throws IllegalArgumentException exc2\n" +
89
" @throws IllegalAccessException exc3\n" +
90
" @return value\n");
91
}
92
93
private Element getFirstMethod(JavacTask task, String typeName) {
94
return ElementFilter.methodsIn(task.getElements().getTypeElement(typeName).getEnclosedElements()).get(0);
95
}
96
97
private Function<JavacTask, Element> getSubTest = t -> getFirstMethod(t, "test.Sub");
98
99
public void testInheritNoJavadoc() throws Exception {
100
doTestJavadoc("",
101
getSubTest,
102
" javadoc1\n" +
103
"\n" +
104
" @param p1 param1\n" +
105
" @param p2 param2\n" +
106
" @param p3 param3\n" +
107
" @throws IllegalStateException exc1\n" +
108
" @throws IllegalArgumentException exc2\n" +
109
" @throws IllegalAccessException exc3\n" +
110
" @return value\n");
111
}
112
113
public void testInheritFull() throws Exception {
114
doTestJavadoc(" /**\n" +
115
" * Prefix {@inheritDoc} suffix.\n" +
116
" *\n" +
117
" * @param p1 prefix {@inheritDoc} suffix\n" +
118
" * @param p2 prefix {@inheritDoc} suffix\n" +
119
" * @param p3 prefix {@inheritDoc} suffix\n" +
120
" * @throws IllegalStateException prefix {@inheritDoc} suffix\n" +
121
" * @throws IllegalArgumentException prefix {@inheritDoc} suffix\n" +
122
" * @throws IllegalAccessException prefix {@inheritDoc} suffix\n" +
123
" * @return prefix {@inheritDoc} suffix\n" +
124
" */\n",
125
getSubTest,
126
" Prefix javadoc1 suffix.\n" +
127
"\n" +
128
" @param p1 prefix param1 suffix\n" +
129
" @param p2 prefix param2 suffix\n" +
130
" @param p3 prefix param3 suffix\n" +
131
" @throws IllegalStateException prefix exc1 suffix\n" +
132
" @throws IllegalArgumentException prefix exc2 suffix\n" +
133
" @throws IllegalAccessException prefix exc3 suffix\n" +
134
" @return prefix value suffix\n");
135
}
136
137
public void testInheritMissingParam() throws Exception {
138
doTestJavadoc(" /**\n" +
139
" * Prefix {@inheritDoc} suffix.\n" +
140
" *\n" +
141
" * @param p1 prefix {@inheritDoc} suffix\n" +
142
" * @param p3 prefix {@inheritDoc} suffix\n" +
143
" * @throws IllegalStateException prefix {@inheritDoc} suffix\n" +
144
" * @throws IllegalArgumentException prefix {@inheritDoc} suffix\n" +
145
" * @throws IllegalAccessException prefix {@inheritDoc} suffix\n" +
146
" * @return prefix {@inheritDoc} suffix\n" +
147
" */\n",
148
getSubTest,
149
" Prefix javadoc1 suffix.\n" +
150
"\n" +
151
" @param p1 prefix param1 suffix\n" +
152
"@param p2 param2\n" +
153
" @param p3 prefix param3 suffix\n" +
154
" @throws IllegalStateException prefix exc1 suffix\n" +
155
" @throws IllegalArgumentException prefix exc2 suffix\n" +
156
" @throws IllegalAccessException prefix exc3 suffix\n" +
157
" @return prefix value suffix\n");
158
}
159
160
public void testInheritMissingFirstParam() throws Exception {
161
doTestJavadoc(" /**\n" +
162
" * Prefix {@inheritDoc} suffix.\n" +
163
" *\n" +
164
" * @param p2 prefix {@inheritDoc} suffix\n" +
165
" * @param p3 prefix {@inheritDoc} suffix\n" +
166
" * @throws IllegalStateException prefix {@inheritDoc} suffix\n" +
167
" * @throws IllegalArgumentException prefix {@inheritDoc} suffix\n" +
168
" * @throws IllegalAccessException prefix {@inheritDoc} suffix\n" +
169
" * @return prefix {@inheritDoc} suffix\n" +
170
" */\n",
171
getSubTest,
172
" Prefix javadoc1 suffix.\n" +
173
"\n" +
174
"@param p1 param1\n" +
175
" @param p2 prefix param2 suffix\n" +
176
" @param p3 prefix param3 suffix\n" +
177
" @throws IllegalStateException prefix exc1 suffix\n" +
178
" @throws IllegalArgumentException prefix exc2 suffix\n" +
179
" @throws IllegalAccessException prefix exc3 suffix\n" +
180
" @return prefix value suffix\n");
181
}
182
183
public void testInheritMissingThrows() throws Exception {
184
doTestJavadoc(" /**\n" +
185
" * Prefix {@inheritDoc} suffix.\n" +
186
" *\n" +
187
" * @param p1 prefix {@inheritDoc} suffix\n" +
188
" * @param p2 prefix {@inheritDoc} suffix\n" +
189
" * @param p3 prefix {@inheritDoc} suffix\n" +
190
" * @throws IllegalStateException prefix {@inheritDoc} suffix\n" +
191
" * @throws IllegalAccessException prefix {@inheritDoc} suffix\n" +
192
" * @return prefix {@inheritDoc} suffix\n" +
193
" */\n",
194
getSubTest,
195
" Prefix javadoc1 suffix.\n" +
196
"\n" +
197
" @param p1 prefix param1 suffix\n" +
198
" @param p2 prefix param2 suffix\n" +
199
" @param p3 prefix param3 suffix\n" +
200
" @throws IllegalStateException prefix exc1 suffix\n" +
201
"@throws java.lang.IllegalArgumentException exc2\n" +
202
" @throws IllegalAccessException prefix exc3 suffix\n" +
203
" @return prefix value suffix\n");
204
}
205
206
public void testInheritMissingReturn() throws Exception {
207
doTestJavadoc(" /**\n" +
208
" * Prefix {@inheritDoc} suffix.\n" +
209
" *\n" +
210
" * @param p1 prefix {@inheritDoc} suffix\n" +
211
" * @param p2 prefix {@inheritDoc} suffix\n" +
212
" * @param p3 prefix {@inheritDoc} suffix\n" +
213
" * @throws IllegalStateException prefix {@inheritDoc} suffix\n" +
214
" * @throws IllegalArgumentException prefix {@inheritDoc} suffix\n" +
215
" * @throws IllegalAccessException prefix {@inheritDoc} suffix\n" +
216
" */\n",
217
getSubTest,
218
" Prefix javadoc1 suffix.\n" +
219
"\n" +
220
" @param p1 prefix param1 suffix\n" +
221
" @param p2 prefix param2 suffix\n" +
222
" @param p3 prefix param3 suffix\n" +
223
" @throws IllegalStateException prefix exc1 suffix\n" +
224
" @throws IllegalArgumentException prefix exc2 suffix\n" +
225
" @throws IllegalAccessException prefix exc3 suffix\n" +
226
"@return value\n");
227
}
228
229
public void testInheritAllButOne() throws Exception {
230
doTestJavadoc(" /**\n" +
231
" * @throws IllegalArgumentException {@inheritDoc}\n" +
232
" */\n",
233
getSubTest,
234
"javadoc1\n" +
235
"@param p1 param1\n" +
236
"@param p2 param2\n" +
237
"@param p3 param3\n" +
238
"@throws java.lang.IllegalStateException exc1\n" +
239
" @throws IllegalArgumentException exc2\n" +
240
"@throws java.lang.IllegalAccessException exc3\n" +
241
"@return value\n");
242
}
243
244
public void testInheritEmpty() throws Exception {
245
doTestJavadoc(" /**\n" +
246
" */\n",
247
" /**@param p1\n" +
248
" * @param p2\n" +
249
" * @param p3\n" +
250
" * @throws IllegalStateException\n" +
251
" * @throws IllegalArgumentException\n" +
252
" * @throws IllegalAccessException\n" +
253
" * @return\n" +
254
" */\n",
255
getSubTest,
256
"\n" +
257
"@param p1 \n" +
258
"@param p2 \n" +
259
"@param p3 \n" +
260
"@throws java.lang.IllegalStateException \n" +
261
"@throws java.lang.IllegalArgumentException \n" +
262
"@throws java.lang.IllegalAccessException \n" +
263
"@return \n");
264
}
265
266
public void testEmptyValue() throws Exception {
267
doTestJavadoc(" /**\n" +
268
" */\n",
269
" /**@param p1 {@value}\n" +
270
" * @param p2\n" +
271
" * @param p3\n" +
272
" * @throws IllegalStateException\n" +
273
" * @throws IllegalArgumentException\n" +
274
" * @throws IllegalAccessException\n" +
275
" * @return\n" +
276
" */\n",
277
getSubTest,
278
"\n" +
279
"@param p1 {@value}\n" +
280
"@param p2 \n" +
281
"@param p3 \n" +
282
"@throws java.lang.IllegalStateException \n" +
283
"@throws java.lang.IllegalArgumentException \n" +
284
"@throws java.lang.IllegalAccessException \n" +
285
"@return \n");
286
}
287
288
public void testShortComment() throws Exception {
289
doTestJavadoc(" /**Test.*/\n",
290
getSubTest,
291
"Test." +
292
"@param p1 param1\n" +
293
"@param p2 param2\n" +
294
"@param p3 param3\n" +
295
"@throws java.lang.IllegalStateException exc1\n" +
296
"@throws java.lang.IllegalArgumentException exc2\n" +
297
"@throws java.lang.IllegalAccessException exc3\n" +
298
"@return value\n");
299
}
300
301
private void doTestJavadoc(String origJavadoc, Function<JavacTask, Element> getElement, String expectedJavadoc) throws Exception {
302
doTestJavadoc(origJavadoc,
303
" /**\n" +
304
" * javadoc1\n" +
305
" *\n" +
306
" * @param p1 param1\n" +
307
" * @param p2 param2\n" +
308
" * @param p3 param3\n" +
309
" * @throws IllegalStateException exc1\n" +
310
" * @throws IllegalArgumentException exc2\n" +
311
" * @throws IllegalAccessException exc3\n" +
312
" * @return value\n" +
313
" */\n",
314
getElement, expectedJavadoc);
315
}
316
317
private void doTestJavadoc(String origJavadoc,
318
String superJavadoc,
319
Function<JavacTask, Element> getElement,
320
String expectedJavadoc) throws Exception {
321
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
322
String subClass =
323
"package test;\n" +
324
"public class Sub extends Super {\n" +
325
origJavadoc +
326
" public String test(int p1, int p2, int p3) throws IllegalStateException, IllegalArgumentException, IllegalAccessException { return null;} \n" +
327
"}\n";
328
String superClass =
329
"package test;\n" +
330
"/**Top level." +
331
" */\n" +
332
"public class Super {\n" +
333
superJavadoc +
334
" public String test(int p1, int p2, int p3) throws IllegalStateException, IllegalArgumentException, IllegalAccessException { return null;} \n" +
335
"}\n";
336
337
Path srcZip = Paths.get("src.zip");
338
339
try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(srcZip))) {
340
out.putNextEntry(new JarEntry("test/Sub.java"));
341
out.write(subClass.getBytes());
342
out.putNextEntry(new JarEntry("test/Super.java"));
343
out.write(superClass.getBytes());
344
} catch (IOException ex) {
345
throw new IllegalStateException(ex);
346
}
347
348
DiagnosticListener<? super JavaFileObject> noErrors = d -> {
349
if (d.getKind() == Kind.ERROR) {
350
throw new AssertionError(d.getMessage(null));
351
}
352
};
353
354
assertTrue(compiler.getTask(null, null, noErrors, Arrays.asList("-d", "."), null, Arrays.asList(new JFOImpl("Super", superClass), new JFOImpl("Sub", subClass))).call());
355
356
try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
357
fm.setLocationFromPaths(StandardLocation.CLASS_PATH, Arrays.asList(Paths.get(".").toAbsolutePath()));
358
JavacTask task = (JavacTask) compiler.getTask(null, fm, noErrors, null, null, null);
359
360
Element el = getElement.apply(task);
361
362
try (JavadocHelper helper = JavadocHelper.create(task, Arrays.asList(srcZip))) {
363
String javadoc = helper.getResolvedDocComment(el);
364
365
assertEquals(javadoc, expectedJavadoc);
366
}
367
}
368
}
369
370
private static final class JFOImpl extends SimpleJavaFileObject {
371
372
private final String code;
373
374
public JFOImpl(String name, String code) throws URISyntaxException {
375
super(new URI("mem:///" + name + ".java"), Kind.SOURCE);
376
this.code = code;
377
}
378
379
@Override
380
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
381
return code;
382
}
383
384
}
385
386
public void testAllDocs() throws IOException {
387
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
388
DiagnosticListener<? super JavaFileObject> noErrors = d -> {
389
if (d.getKind() == Kind.ERROR) {
390
throw new AssertionError(d.getMessage(null));
391
}
392
};
393
394
List<Path> sources = new ArrayList<>();
395
Path home = Paths.get(System.getProperty("java.home"));
396
Path srcZip = home.resolve("lib").resolve("src.zip");
397
if (Files.isReadable(srcZip)) {
398
URI uri = URI.create("jar:" + srcZip.toUri());
399
try (FileSystem zipFO = FileSystems.newFileSystem(uri, Collections.emptyMap())) {
400
Path root = zipFO.getRootDirectories().iterator().next();
401
402
//modular format:
403
try (DirectoryStream<Path> ds = Files.newDirectoryStream(root)) {
404
for (Path p : ds) {
405
if (Files.isDirectory(p)) {
406
sources.add(p);
407
}
408
}
409
}
410
try (StandardJavaFileManager fm =
411
compiler.getStandardFileManager(null, null, null)) {
412
JavacTask task =
413
(JavacTask) compiler.getTask(null, fm, noErrors, null, null, null);
414
task.getElements().getTypeElement("java.lang.Object");
415
for (ModuleElement me : task.getElements().getAllModuleElements()) {
416
List<ExportsDirective> exports =
417
ElementFilter.exportsIn(me.getDirectives());
418
for (ExportsDirective ed : exports) {
419
try (JavadocHelper helper = JavadocHelper.create(task, sources)) {
420
List<? extends Element> content =
421
ed.getPackage().getEnclosedElements();
422
for (TypeElement clazz : ElementFilter.typesIn(content)) {
423
for (Element el : clazz.getEnclosedElements()) {
424
helper.getResolvedDocComment(el);
425
}
426
}
427
}
428
}
429
}
430
}
431
}
432
}
433
}
434
}
435
436