[MJAVADOC-556] javadoc:aggregate fails with "No source files for package" for packages that are not exported
diff --git a/pom.xml b/pom.xml
index dbead88..bba7bfa 100644
--- a/pom.xml
+++ b/pom.xml
@@ -536,7 +536,7 @@
             <artifactId>maven-surefire-plugin</artifactId>
             <configuration>
               <excludes>
-                <exlude>**/*Test*.java</exlude>
+                <exclude>**/*Test*.java</exclude>
               </excludes>
             </configuration>
           </plugin>
diff --git a/src/it/projects/MJAVADOC-556_hide-non-exported-packages/invoker.properties b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/invoker.properties
new file mode 100644
index 0000000..81ca246
--- /dev/null
+++ b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/invoker.properties
@@ -0,0 +1,19 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+invoker.java.version=9+
+invoker.goals=javadoc:aggregate
diff --git a/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/pom.xml b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/pom.xml
new file mode 100644
index 0000000..e5bea45
--- /dev/null
+++ b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/pom.xml
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing,
+  software distributed under the License is distributed on an
+  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  KIND, either express or implied.  See the License for the
+  specific language governing permissions and limitations
+  under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <groupId>org.apache.maven.plugins.maven-javadoc-plugin.it</groupId>
+        <artifactId>MJAVADOC-556</artifactId>
+        <version>1.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+    <artifactId>module</artifactId>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <version>3.8.0</version>
+                <configuration>
+                    <source>9</source>
+                    <target>9</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>
\ No newline at end of file
diff --git a/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/src/main/java/module-info.java b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/src/main/java/module-info.java
new file mode 100644
index 0000000..ccb7c17
--- /dev/null
+++ b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/src/main/java/module-info.java
@@ -0,0 +1,23 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+module module
+{
+    exports package1;
+}
\ No newline at end of file
diff --git a/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/src/main/java/package1/Main1.java b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/src/main/java/package1/Main1.java
new file mode 100644
index 0000000..f380ab9
--- /dev/null
+++ b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/src/main/java/package1/Main1.java
@@ -0,0 +1,24 @@
+package package1;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+public final class Main1
+{
+}
diff --git a/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/src/main/java/package2/Main2.java b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/src/main/java/package2/Main2.java
new file mode 100644
index 0000000..22de4c5
--- /dev/null
+++ b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/src/main/java/package2/Main2.java
@@ -0,0 +1,24 @@
+package package2;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+public final class Main2
+{
+}
diff --git a/src/it/projects/MJAVADOC-556_hide-non-exported-packages/pom.xml b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/pom.xml
new file mode 100644
index 0000000..7a12e5f
--- /dev/null
+++ b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/pom.xml
@@ -0,0 +1,49 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing,
+  software distributed under the License is distributed on an
+  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  KIND, either express or implied.  See the License for the
+  specific language governing permissions and limitations
+  under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>org.apache.maven.plugins.maven-javadoc-plugin.it</groupId>
+    <artifactId>MJAVADOC-556</artifactId>
+    <version>1.0-SNAPSHOT</version>
+    <packaging>pom</packaging>
+
+    <url>https://issues.apache.org/jira/browse/MJAVADOC-556</url>
+
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+
+    <build>
+        <pluginManagement>
+            <plugins>
+                <plugin>
+                    <groupId>org.apache.maven.plugins</groupId>
+                    <artifactId>maven-javadoc-plugin</artifactId>
+                    <version>@project.version@</version>
+                </plugin>
+            </plugins>
+        </pluginManagement>
+    </build>
+    <modules>
+        <module>module</module>
+    </modules>
+</project>
diff --git a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
index 1e79932..e7c8bbc 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
@@ -82,6 +82,7 @@
 import org.codehaus.plexus.archiver.manager.ArchiverManager;

 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;

 import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;

+import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor;

 import org.codehaus.plexus.languages.java.jpms.LocationManager;

 import org.codehaus.plexus.languages.java.jpms.ResolvePathsRequest;

 import org.codehaus.plexus.languages.java.jpms.ResolvePathsResult;

@@ -1997,9 +1998,6 @@
             return;

         }

 

-        List<String> packageNames = getPackageNames( collectedSourcePaths, files );

-        List<String> filesWithUnnamedPackages = getFilesWithUnnamedPackages( collectedSourcePaths, files );

-

         // ----------------------------------------------------------------------

         // Find the javadoc executable and version

         // ----------------------------------------------------------------------

@@ -2015,6 +2013,17 @@
         }

         setFJavadocVersion( new File( jExecutable ) );

 

+        List<String> packageNames;

+        if ( javadocRuntimeVersion.isAtLeast( "9" ) )

+        {

+            packageNames = getPackageNamesRespectingJavaModules( sourcePaths );

+        }

+        else

+        {

+            packageNames = getPackageNames( collectedSourcePaths, files );

+        }

+        List<String> filesWithUnnamedPackages = getFilesWithUnnamedPackages( collectedSourcePaths, files );

+

         // ----------------------------------------------------------------------

         // Javadoc output directory as File

         // ----------------------------------------------------------------------

@@ -2514,7 +2523,7 @@
      * Method to get the excluded source files from the javadoc and create the argument string

      * that will be included in the javadoc commandline execution.

      *

-     * @param sourcePaths the collection of paths to the source files

+     * @param sourceFolders the collection of paths to the source files

      * @return a String that contains the exclude argument that will be used by javadoc

      * @throws MavenReportException

      */

@@ -4343,6 +4352,102 @@
     }

 

     /**

+     * @param allSourcePaths     not null, containing absolute and relative paths

+     * @return a list of exported package names for files in allSourcePaths

+     * @throws MavenReportException if any

+     * @see #getFiles

+     * @see #getSourcePaths()

+     */

+    private List<String> getPackageNamesRespectingJavaModules( Map<String, Collection<String>> allSourcePaths )

+            throws MavenReportException

+    {

+        List<String> returnList = new ArrayList<>();

+

+        if ( !StringUtils.isEmpty( sourcepath ) )

+        {

+            return returnList;

+        }

+        LocationManager locationManager = new LocationManager();

+

+        for ( Collection<String> artifactSourcePaths: allSourcePaths.values() )

+        {

+            Set<String> exportedPackages = new HashSet<>();

+            boolean exportAllPackages;

+            File mainDescriptor = findMainDescriptor( artifactSourcePaths );

+            if ( mainDescriptor != null && !isTest() )

+            {

+                ResolvePathsRequest<File> request =

+                        ResolvePathsRequest.withFiles( Collections.<File>emptyList() ).

+                                setMainModuleDescriptor( mainDescriptor );

+    

+                try

+                {

+                    Set<JavaModuleDescriptor.JavaExports> exports = locationManager.resolvePaths( request ).

+                            getMainModuleDescriptor().exports();

+                    if ( exports.isEmpty() )

+                    {

+                        continue;

+                    }

+                    for ( JavaModuleDescriptor.JavaExports export : exports )

+                    {

+                        exportedPackages.add( export.source() );

+                    }

+                }

+                catch ( IOException e )

+                {

+                    throw new MavenReportException( e.getMessage(), e );

+                }

+                exportAllPackages = false;

+            }

+            else

+            {

+                exportAllPackages = true;

+            }

+            

+            for ( String currentFile : getFiles( artifactSourcePaths ) )

+            {

+                currentFile = currentFile.replace( '\\', '/' );

+

+                for ( String currentSourcePath : artifactSourcePaths )

+                {

+                    currentSourcePath = currentSourcePath.replace( '\\', '/' );

+

+                    if ( currentFile.contains( currentSourcePath ) )

+                    {

+                        if ( !currentSourcePath.endsWith( "/" ) )

+                        {

+                            currentSourcePath += "/";

+                        }

+                        String packagename = currentFile.substring( currentSourcePath.length() + 1 );

+

+                        /*

+                         * Remove the miscellaneous files

+                         * http://docs.oracle.com/javase/1.4.2/docs/tooldocs/solaris/javadoc.html#unprocessed

+                         */

+                        if ( packagename.contains( "doc-files" ) )

+                        {

+                            continue;

+                        }

+

+                        if ( packagename.lastIndexOf( "/" ) != -1 )

+                        {

+                            packagename = packagename.substring( 0, packagename.lastIndexOf( "/" ) );

+                            packagename = packagename.replace( '/', '.' );

+

+                            if ( exportAllPackages || exportedPackages.contains( packagename ) )

+                            {

+                                returnList.add( packagename );

+                            }

+                        }

+                    }

+                }

+            }

+        }

+

+        return returnList;

+    }

+

+    /**

      * @param sourcePaths could be null

      * @param files       not null

      * @return a list files with unnamed package names for files in the sourcePaths