8234051: doclet crashes if HTML files in module doc-files directories
authorjjg
Thu, 21 Nov 2019 13:41:24 -0800
changeset 59210 78184b74af6e
parent 59209 8a24f1e73c0d
child 59211 71a84cee0c39
8234051: doclet crashes if HTML files in module doc-files directories Reviewed-by: hannesw
src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/DocFilesHandlerImpl.java
src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/DocFileElement.java
test/langtools/jdk/javadoc/doclet/testDocFiles/TestDocFiles.java
test/langtools/jdk/javadoc/doclet/testDocFiles/pkg/Test.java
test/langtools/jdk/javadoc/doclet/testDocFiles/pkg/doc-files/test.txt
--- a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/DocFilesHandlerImpl.java	Thu Nov 21 18:54:21 2019 +0000
+++ b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/DocFilesHandlerImpl.java	Thu Nov 21 13:41:24 2019 -0800
@@ -169,7 +169,7 @@
     private void handleHtmlFile(DocFile srcfile, DocPath dstPath) throws DocFileIOException {
         Utils utils = configuration.utils;
         FileObject fileObject = srcfile.getFileObject();
-        DocFileElement dfElement = new DocFileElement(element, fileObject);
+        DocFileElement dfElement = new DocFileElement(utils, element, fileObject);
 
         DocPath dfilePath = dstPath.resolve(srcfile.getName());
         HtmlDocletWriter docletWriter = new DocFileWriter(configuration, dfilePath, element);
@@ -181,8 +181,8 @@
         String title = getWindowTitle(docletWriter, dfElement).trim();
         HtmlTree htmlContent = docletWriter.getBody(title);
         docletWriter.addTop(htmlContent);
-        PackageElement pkg = (PackageElement) element;
-        this.navBar = new Navigation(pkg, configuration, docletWriter.fixedNavDiv,
+        PackageElement pkg = dfElement.getPackageElement();
+        this.navBar = new Navigation(element, configuration, docletWriter.fixedNavDiv,
                 PageMode.DOCFILE, docletWriter.path);
         Content mdleLinkContent = docletWriter.getModuleLink(utils.elementUtils.getModuleOf(pkg),
                 docletWriter.contents.moduleLabel);
@@ -299,8 +299,6 @@
 
     private static class DocFileWriter extends HtmlDocletWriter {
 
-        final PackageElement pkg;
-
         /**
          * Constructor to construct the HtmlDocletWriter object.
          *
@@ -312,7 +310,7 @@
             super(configuration, path);
             switch (e.getKind()) {
                 case PACKAGE:
-                    pkg = (PackageElement)e;
+                case MODULE:
                     break;
                 default:
                     throw new AssertionError("unsupported element: " + e.getKind());
--- a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/DocFileElement.java	Thu Nov 21 18:54:21 2019 +0000
+++ b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/DocFileElement.java	Thu Nov 21 13:41:24 2019 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,19 +25,12 @@
 
 package jdk.javadoc.internal.doclets.toolkit;
 
-import java.lang.annotation.Annotation;
-import java.util.Set;
-
-import javax.lang.model.element.AnnotationMirror;
 import javax.lang.model.element.Element;
-import javax.lang.model.element.ElementKind;
-import javax.lang.model.element.ElementVisitor;
-import javax.lang.model.element.Name;
+import javax.lang.model.element.ModuleElement;
 import javax.lang.model.element.PackageElement;
-import javax.lang.model.type.TypeMirror;
 import javax.tools.FileObject;
 
-import jdk.javadoc.doclet.DocletEnvironment;
+import jdk.javadoc.internal.doclets.toolkit.util.Utils;
 
 /**
  * This is a pseudo element wrapper for doc-files html contents, essentially to
@@ -50,26 +43,30 @@
  */
 public class DocFileElement implements DocletElement {
 
-    private final Element element;
+    private final PackageElement packageElement;
     private final FileObject fo;
 
-    public DocFileElement(Element element, FileObject fo) {
-        this.element = element;
+    public DocFileElement(Utils utils, Element element, FileObject fo) {
         this.fo = fo;
+
+        switch(element.getKind()) {
+            case MODULE:
+                ModuleElement moduleElement = (ModuleElement) element;
+                packageElement = utils.elementUtils.getPackageElement(moduleElement, "");
+                break;
+
+            case PACKAGE:
+                packageElement = (PackageElement) element;
+                break;
+
+            default:
+                throw new AssertionError("unknown kind: " + element.getKind());
+        }
     }
 
     @Override
     public PackageElement getPackageElement() {
-        switch(element.getKind()) {
-            case MODULE:
-                // uncomment to support doc-files in modules
-                // return configuration.workArounds.getUnnamedPackage();
-                throw new UnsupportedOperationException("not implemented");
-            case PACKAGE:
-                return (PackageElement)element;
-            default:
-                throw new AssertionError("unknown kind: " + element.getKind());
-        }
+        return packageElement;
     }
 
     @Override
--- a/test/langtools/jdk/javadoc/doclet/testDocFiles/TestDocFiles.java	Thu Nov 21 18:54:21 2019 +0000
+++ b/test/langtools/jdk/javadoc/doclet/testDocFiles/TestDocFiles.java	Thu Nov 21 13:41:24 2019 -0800
@@ -23,31 +23,109 @@
 
 /*
  * @test
- * @bug 8008949
- * @summary verify that doc-files get copied
- * @library ../../lib
+ * @bug 8008949 8234051
+ * @summary doclet crashes if HTML files in module doc-files directories
+ * @library /tools/lib ../../lib
  * @modules jdk.javadoc/jdk.javadoc.internal.tool
- * @build javadoc.tester.*
+ * @build toolbox.ToolBox javadoc.tester.*
  * @run main TestDocFiles
  */
 
+import java.io.IOException;
+import java.nio.file.Path;
+
+import toolbox.ToolBox;
 import javadoc.tester.JavadocTester;
 
 public class TestDocFiles extends JavadocTester {
 
     public static void main(String... args) throws Exception {
         TestDocFiles tester = new TestDocFiles();
-        tester.runTests();
+        tester.runTests(m -> new Object[] { Path.of(m.getName()) });
+    }
+
+    ToolBox tb = new ToolBox();
+
+    /**
+     * Check doc-files support for a package that is not in a module.
+     * @param base the base directory for scratch files
+     * @throws IOException if an exception occurs
+     */
+    @Test
+    public void testPackage(Path base) throws IOException {
+        Path src = base.resolve("src");
+
+        // write the skeletal Java files
+        tb.writeJavaFiles(src,
+                "package p; public class C { }\n");
+
+        // write the doc files for the package
+        Path pkgDocFiles = src.resolve("p").resolve("doc-files");
+        tb.writeFile(pkgDocFiles.resolve("pkg-file.txt"),
+                "package text file\n");
+        tb.writeFile(pkgDocFiles.resolve("pkg-file.html"),
+                "<html>\n"
+                + "<head><title>Package HTML file</title></head>\n"
+                + "<body><h1>Package HTML file</h1>File content</body>\n"
+                + "</html>\n");
+
+        javadoc("-d", base.resolve("out").toString(),
+                "--source-path", src.toString(),
+                "p");
+        checkExit(Exit.OK);
+
+        checkOutput("p/doc-files/pkg-file.txt", true,
+                "package text file");
+        checkOutput("p/doc-files/pkg-file.html", true,
+                "Package HTML file");
     }
 
+    /**
+     * Check doc-files support for a module and a package that is in a module.
+     * @param base the base directory for scratch files
+     * @throws IOException if an exception occurs
+     */
     @Test
-    public void test() {
-        javadoc("-d", "out",
-                "-sourcepath", testSrc,
-                "pkg");
+    public void testModules(Path base) throws IOException {
+        Path src = base.resolve("src");
+
+        // write the skeletal Java files
+        tb.writeJavaFiles(src,
+                "module m { exports p; }\n",
+                "package p; public class C { }\n");
+
+        // write the doc files for the module
+        Path mdlDocFiles = src.resolve("doc-files");
+        tb.writeFile(mdlDocFiles.resolve("mdl-file.txt"),
+                "module text file\n");
+        tb.writeFile(mdlDocFiles.resolve("mdl-file.html"),
+                "<html>\n"
+                + "<head><title>Module HTML file</title></head>\n"
+                + "<body><h1>Module HTML file</h1>File content</body>\n"
+                + "</html>\n");
+
+        // write the doc files for a package in the module
+        Path pkgDocFiles = src.resolve("p").resolve("doc-files");
+        tb.writeFile(pkgDocFiles.resolve("pkg-file.txt"),
+                "package text file\n");
+        tb.writeFile(pkgDocFiles.resolve("pkg-file.html"),
+                "<html>\n"
+                + "<head><title>Package HTML file</title></head>\n"
+                + "<body><h1>Package HTML file</h1>File content</body>\n"
+                + "</html>\n");
+
+        javadoc("-d", base.resolve("out").toString(),
+                "--source-path", src.toString(),
+                "--module", "m");
         checkExit(Exit.OK);
 
-        checkOutput("pkg/doc-files/test.txt", true,
-                "test file");
+        checkOutput("m/doc-files/mdl-file.txt", true,
+                "module text file");
+        checkOutput("m/doc-files/mdl-file.html", true,
+                "Module HTML file");
+        checkOutput("m/p/doc-files/pkg-file.txt", true,
+                "package text file");
+        checkOutput("m/p/doc-files/pkg-file.html", true,
+                "Package HTML file");
     }
 }
--- a/test/langtools/jdk/javadoc/doclet/testDocFiles/pkg/Test.java	Thu Nov 21 18:54:21 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,27 +0,0 @@
-/*
- * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-package pkg;
-
-public class Test { }
-
--- a/test/langtools/jdk/javadoc/doclet/testDocFiles/pkg/doc-files/test.txt	Thu Nov 21 18:54:21 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,2 +0,0 @@
-this is a test file
-