8182736: javadoc generates bad names and broken module graph links
Reviewed-by: jjg, bpatel, darcy, ksrini
Contributed-by: bhavesh.patel@oracle.com, jonathan.gibbons@oracle.com
diff --git a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/AbstractIndexWriter.java b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/AbstractIndexWriter.java
index fd7d811..da32047 100644
--- a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/AbstractIndexWriter.java
+++ b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/AbstractIndexWriter.java
@@ -140,7 +140,9 @@
         int j = 0;
         Content dl = new HtmlTree(HtmlTag.DL);
         while (i < memberListSize && j < searchListSize) {
-            String name = utils.getSimpleName(memberlist.get(i));
+            Element elem = memberlist.get(i);
+            String name = (utils.isModule(elem))
+                    ? utils.getFullyQualifiedName(elem) : utils.getSimpleName(elem);
             if (name.compareTo(searchList.get(j).getLabel()) < 0) {
                 addDescription(dl, memberlist.get(i));
                 i++;
@@ -222,7 +224,7 @@
      * @param dlTree the content tree to which the description will be added
      */
     protected void addDescription(ModuleElement mdle, Content dlTree, SearchIndexItem si) {
-        String moduleName = utils.getSimpleName(mdle);
+        String moduleName = utils.getFullyQualifiedName(mdle);
         Content link = getModuleLink(mdle, new StringContent(moduleName));
         si.setLabel(moduleName);
         si.setCategory(resources.getText("doclet.Modules"));
@@ -246,7 +248,7 @@
     protected void addDescription(PackageElement pkg, Content dlTree, SearchIndexItem si) {
         Content link = getPackageLink(pkg, new StringContent(utils.getPackageName(pkg)));
         if (configuration.showModules) {
-            si.setContainingModule(utils.getSimpleName(utils.containingModule(pkg)));
+            si.setContainingModule(utils.getFullyQualifiedName(utils.containingModule(pkg)));
         }
         si.setLabel(utils.getPackageName(pkg));
         si.setCategory(resources.getText("doclet.Packages"));
diff --git a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/TagletWriterImpl.java b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/TagletWriterImpl.java
index 2892801..bb0eba3 100644
--- a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/TagletWriterImpl.java
+++ b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/TagletWriterImpl.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2017, 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
@@ -115,7 +115,7 @@
                 @Override
                 public Void visitModule(ModuleElement e, Void p) {
                     si.setUrl(DocPaths.moduleSummary(e).getPath() + "#" + anchorName);
-                    si.setHolder(utils.getSimpleName(element));
+                    si.setHolder(utils.getFullyQualifiedName(element));
                     return null;
                 }
 
diff --git a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/builders/ModuleSummaryBuilder.java b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/builders/ModuleSummaryBuilder.java
index 47b20f8..d9fa636 100644
--- a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/builders/ModuleSummaryBuilder.java
+++ b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/builders/ModuleSummaryBuilder.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2017, 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
@@ -131,7 +131,7 @@
      * @throws DocletException if there is a problem while building the documentation
      */
     public void buildModuleDoc(XMLNode node, Content contentTree) throws DocletException {
-        contentTree = moduleWriter.getModuleHeader(mdle.getSimpleName().toString());
+        contentTree = moduleWriter.getModuleHeader(mdle.getQualifiedName().toString());
         buildChildren(node, contentTree);
         moduleWriter.addModuleFooter(contentTree);
         moduleWriter.printDocument(contentTree);
diff --git a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/IndexBuilder.java b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/IndexBuilder.java
index 454cc8f..e913d4e 100644
--- a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/IndexBuilder.java
+++ b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/IndexBuilder.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2017, 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
@@ -199,7 +199,7 @@
      */
     protected void addModulesToIndexMap() {
         for (ModuleElement mdle : configuration.modules) {
-            String mdleName = mdle.getSimpleName().toString();
+            String mdleName = mdle.getQualifiedName().toString();
             char ch = (mdleName.length() == 0)
                     ? '*'
                     : Character.toUpperCase(mdleName.charAt(0));
diff --git a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/Utils.java b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/Utils.java
index 9143002..f909e5a 100644
--- a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/Utils.java
+++ b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/Utils.java
@@ -1682,7 +1682,7 @@
         return new Utils.ElementComparator<Element>() {
             @Override
             public int compare(Element mod1, Element mod2) {
-                return compareNames(mod1, mod2);
+                return compareFullyQualifiedNames(mod1, mod2);
             }
         };
     }
@@ -1772,9 +1772,8 @@
 
     /**
      * Returns a Comparator for index file presentations, and are sorted as follows.
-     *  If comparing modules then simply compare the simple names,
-     *  comparing packages then simply compare the qualified names, if comparing a package with a
-     *  module/type/member then compare the FullyQualifiedName of the package
+     *  If comparing modules and packages then simply compare the qualified names, if comparing a module
+     *  or a package with a type/member then compare the FullyQualifiedName of the module or a package
      *  with the SimpleName of the entity, otherwise
      *  1. compare the ElementKind ex: Module, Package, Interface etc.
      *  2a. if equal and if the type is of ExecutableElement(Constructor, Methods),
@@ -1786,10 +1785,9 @@
     public Comparator<Element> makeIndexUseComparator() {
         return new Utils.ElementComparator<Element>() {
             /**
-             * Compare two given elements, if comparing two modules, return the
-             * comparison of SimpleName, if comparing two packages, return the
-             * comparison of FullyQualifiedName, if comparing a package with a
-             * module/type/member then compare the FullyQualifiedName of the package
+             * Compare two given elements, if comparing two modules or two packages, return the
+             * comparison of FullyQualifiedName, if comparing a module or a package with a
+             * type/member then compare the FullyQualifiedName of the module or the package
              * with the SimpleName of the entity, then sort on the kinds, then on
              * the parameters only if the type is an ExecutableElement,
              * the parameters are compared and finally the qualified names.
@@ -1802,16 +1800,17 @@
             @Override
             public int compare(Element e1, Element e2) {
                 int result = 0;
-                if (isModule(e1) && isModule(e2)) {
-                    return compareNames(e1, e2);
+                if ((isModule(e1) || isPackage(e1)) && (isModule(e2) || isPackage(e2))) {
+                    result = compareFullyQualifiedNames(e1, e2);
+                    if (result != 0) {
+                        return result;
+                    }
+                    return compareElementTypeKinds(e1, e2);
                 }
-                if (isPackage(e1) && isPackage(e2)) {
-                    return compareFullyQualifiedNames(e1, e2);
-                }
-                if (isPackage(e1) || isPackage(e2)) {
-                    result = (isPackage(e1))
-                            ? compareStrings(getFullyQualifiedName(e1), getSimpleName(e2))
-                            : compareStrings(getSimpleName(e1), getFullyQualifiedName(e2));
+                if (isModule(e1) || isPackage(e1)) {
+                    result = compareStrings(getFullyQualifiedName(e1), getSimpleName(e2));
+                } else if (isModule(e2) || isPackage(e2)) {
+                    result = compareStrings(getSimpleName(e1), getFullyQualifiedName(e2));
                 } else {
                     result = compareNames(e1, e2);
                 }
@@ -1916,6 +1915,11 @@
     public String getFullyQualifiedName(Element e, final boolean outer) {
         return new SimpleElementVisitor9<String, Void>() {
             @Override
+            public String visitModule(ModuleElement e, Void p) {
+                return e.getQualifiedName().toString();
+            }
+
+            @Override
             public String visitPackage(PackageElement e, Void p) {
                 return e.getQualifiedName().toString();
             }
@@ -2527,7 +2531,7 @@
             snvisitor = new SimpleElementVisitor9<String, Void>() {
                 @Override
                 public String visitModule(ModuleElement e, Void p) {
-                    return e.getSimpleName().toString();
+                    return e.getQualifiedName().toString();  // temp fix for 8182736
                 }
 
                 @Override
diff --git a/test/jdk/javadoc/doclet/testModules/TestModules.java b/test/jdk/javadoc/doclet/testModules/TestModules.java
index 950211f..316d8b6 100644
--- a/test/jdk/javadoc/doclet/testModules/TestModules.java
+++ b/test/jdk/javadoc/doclet/testModules/TestModules.java
@@ -372,7 +372,20 @@
                 "--module", "moduleB",
                 "testpkg2mdlB", "testpkgmdlB");
         checkExit(Exit.OK);
-        //checkOverviewSummaryPackages();
+        checkGroupOptionSingleModule();
+    }
+
+    /**
+     * Test -group option for a single module.
+     */
+    @Test
+    void testModuleName() {
+        javadoc("-d", "out-modulename", "-use",
+                "--module-source-path", testSrc,
+                "--module", "moduleB,test.moduleFullName",
+                "testpkg2mdlB", "testpkgmdlB", "testpkgmdlfullname");
+        checkExit(Exit.OK);
+        checkModuleName(true);
     }
 
     void checkDescription(boolean found) {
@@ -1089,4 +1102,35 @@
                 "<table class=\"overviewSummary\" summary=\"Modules table, listing modules, and an explanation\">\n"
                 + "<caption><span>Modules</span><span class=\"tabEnd\">&nbsp;</span></caption>");
     }
+
+    void checkModuleName(boolean found) {
+        checkOutput("test.moduleFullName-summary.html", found,
+                "<div class=\"header\">\n"
+                + "<h1 title=\"Module\" class=\"title\">Module&nbsp;test.moduleFullName</h1>\n"
+                + "</div>");
+        checkOutput("index-all.html", found,
+                "<h2 class=\"title\">T</h2>\n"
+                + "<dl>\n"
+                + "<dt><a href=\"test.moduleFullName-summary.html\">test.moduleFullName</a> - module test.moduleFullName</dt>\n"
+                + "<dd>\n"
+                + "<div class=\"block\">This is a test description for the test.moduleFullName.</div>\n"
+                + "</dd>");
+        checkOutput("module-overview-frame.html", found,
+                "<h2 title=\"Modules\">Modules</h2>\n"
+                + "<ul title=\"Modules\">\n"
+                + "<li><a href=\"moduleB-frame.html\" target=\"packageListFrame\" onclick=\"updateModuleFrame('moduleB-type-frame.html','moduleB-summary.html');\">moduleB</a></li>\n"
+                + "<li><a href=\"test.moduleFullName-frame.html\" target=\"packageListFrame\" onclick=\"updateModuleFrame('test.moduleFullName-type-frame.html','test.moduleFullName-summary.html');\">test.moduleFullName</a></li>\n"
+                + "</ul>");
+        checkOutput("test.moduleFullName-summary.html", !found,
+                "<div class=\"header\">\n"
+                + "<h1 title=\"Module\" class=\"title\">Module&nbsp;moduleFullName</h1>\n"
+                + "</div>");
+        checkOutput("index-all.html", !found,
+                "<dl>\n"
+                + "<dt><a href=\"test.moduleFullName-summary.html\">moduleFullName</a> - module moduleFullName</dt>\n"
+                + "<dd>\n"
+                + "<div class=\"block\">This is a test description for the test.moduleFullName.</div>\n"
+                + "</dd>\n"
+                + "</dl>");
+}
 }
diff --git a/test/jdk/javadoc/doclet/testModules/test.moduleFullName/module-info.java b/test/jdk/javadoc/doclet/testModules/test.moduleFullName/module-info.java
new file mode 100644
index 0000000..c162ca0
--- /dev/null
+++ b/test/jdk/javadoc/doclet/testModules/test.moduleFullName/module-info.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2017, 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.
+ */
+
+/**
+  * This is a test description for the test.moduleFullName.
+  *
+  */
+module test.moduleFullName {
+    exports testpkgmdlfullname;
+}
diff --git a/test/jdk/javadoc/doclet/testModules/test.moduleFullName/testpkgmdlfullname/TestClassInTestModuleFullName.java b/test/jdk/javadoc/doclet/testModules/test.moduleFullName/testpkgmdlfullname/TestClassInTestModuleFullName.java
new file mode 100644
index 0000000..947033a
--- /dev/null
+++ b/test/jdk/javadoc/doclet/testModules/test.moduleFullName/testpkgmdlfullname/TestClassInTestModuleFullName.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2017, 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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 testpkgmdlfullname;
+
+public class TestClassInTestModuleFullName {
+    public void testMethod() { }
+}