|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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 8272944 |
| 27 | + * @summary Use snippets in jdk.javadoc documentation |
| 28 | + * @library /tools/lib ../../lib |
| 29 | + * @modules jdk.compiler/com.sun.tools.javac.api |
| 30 | + * jdk.compiler/com.sun.tools.javac.main |
| 31 | + * jdk.javadoc/jdk.javadoc.internal.tool |
| 32 | + * @build snippets.SnippetUtils toolbox.JavacTask toolbox.ToolBox javadoc.tester.* |
| 33 | + * @run main TestDocletExample |
| 34 | + */ |
| 35 | + |
| 36 | +import java.nio.file.Files; |
| 37 | +import java.nio.file.Path; |
| 38 | +import java.util.spi.ToolProvider; |
| 39 | +import java.util.stream.Stream; |
| 40 | + |
| 41 | +import snippets.SnippetUtils; |
| 42 | +import toolbox.Task; |
| 43 | +import toolbox.TestRunner; |
| 44 | +import toolbox.ToolBox; |
| 45 | + |
| 46 | +import javax.tools.DiagnosticCollector; |
| 47 | +import javax.tools.JavaFileObject; |
| 48 | + |
| 49 | + |
| 50 | +public class TestDocletExample extends TestRunner { |
| 51 | + public static void main(String... args) throws Exception { |
| 52 | + var t = new TestDocletExample(); |
| 53 | + t.runTests(m -> new Object[] { Path.of(m.getName()) }); |
| 54 | + } |
| 55 | + |
| 56 | + SnippetUtils snippets = new SnippetUtils("jdk.javadoc"); |
| 57 | + ToolBox tb = new ToolBox(); |
| 58 | + |
| 59 | + TestDocletExample() { |
| 60 | + super(System.out); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testEntryPoint(Path base) throws Exception { |
| 65 | + var docletPkg = snippets.getElements().getPackageElement("jdk.javadoc.doclet"); |
| 66 | + var dc = snippets.getDocTrees().getDocCommentTree(docletPkg); |
| 67 | + var entryPointSnippet = snippets.getSnippetById(dc, "entry-point"); |
| 68 | + var entryPointCode = entryPointSnippet.getBody().getBody(); |
| 69 | + var code = """ |
| 70 | + class C { |
| 71 | + %s { } |
| 72 | + } |
| 73 | + """.formatted(entryPointCode); |
| 74 | + DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<>(); |
| 75 | + snippets.parse(code, null, collector); |
| 76 | + var diags = collector.getDiagnostics(); |
| 77 | + if (diags.isEmpty()) { |
| 78 | + out.println("parsed entry point snippet"); |
| 79 | + } else { |
| 80 | + diags.forEach(out::println); |
| 81 | + throw new Exception("parse failed"); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testDocletExample(Path base) throws Exception { |
| 87 | + |
| 88 | + // get source code |
| 89 | + var docletPkg = snippets.getElements().getPackageElement("jdk.javadoc.doclet"); |
| 90 | + var dc = snippets.getDocTrees().getDocCommentTree(docletPkg); |
| 91 | + var exampleSnippet = snippets.getSnippetById(dc, "Example.java"); |
| 92 | + var exampleCode = exampleSnippet.getBody().getBody(); |
| 93 | + |
| 94 | + // compile it |
| 95 | + Path src = base.resolve("src"); |
| 96 | + Path classes = base.resolve("classes"); |
| 97 | + Files.createDirectories(classes); |
| 98 | + |
| 99 | + tb.writeJavaFiles(src, exampleCode); |
| 100 | + new toolbox.JavacTask(tb) |
| 101 | + .outdir(classes) |
| 102 | + .files(tb.findJavaFiles(src)) |
| 103 | + .run(Task.Expect.SUCCESS) |
| 104 | + .writeAll(); |
| 105 | + |
| 106 | + // get demo command |
| 107 | + var cmdSnippet = snippets.getSnippetById(dc, "run-doclet"); |
| 108 | + var cmd = cmdSnippet.getBody().getBody() |
| 109 | + .replaceAll("\\s+//.*", "") // remove markup |
| 110 | + .replaceAll("\\\\\n", " ") // join lines |
| 111 | + .trim(); |
| 112 | + out.println(cmd); |
| 113 | + |
| 114 | + tb.writeFile(src.resolve("overview.html"), |
| 115 | + """ |
| 116 | + <!doctype html> |
| 117 | + <html><title>Overview</title> |
| 118 | + <body> |
| 119 | + Overview |
| 120 | + </body> |
| 121 | + </html> |
| 122 | + """); |
| 123 | + |
| 124 | + var cmdWords = Stream.of(cmd.split("\\s+")) |
| 125 | + .map(s -> s.replace("source-location", src.toString())) |
| 126 | + .map(s -> s.replace("doclet-classes", classes.toString())) |
| 127 | + .toList(); |
| 128 | + var toolName = cmdWords.get(0); |
| 129 | + var toolArgs = cmdWords.subList(1, cmdWords.size()); |
| 130 | + |
| 131 | + ToolProvider tool = ToolProvider.findFirst(toolName) |
| 132 | + .orElseThrow(() -> new Exception("tool not found: " + toolName)); |
| 133 | + int rc = tool.run(System.out, System.err, toolArgs.toArray(new String[0])); |
| 134 | + if (rc != 0) { |
| 135 | + throw new Exception("ecommand return code: " + rc); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments