You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2018/12/27 16:55:49 UTC

[maven-javadoc-plugin] 06/08: Move from String to Path

This is an automated email from the ASF dual-hosted git repository.

rfscholte pushed a commit to branch refactor
in repository https://gitbox.apache.org/repos/asf/maven-javadoc-plugin.git

commit 24edaded33ebd5252151f6d5ba1963fef0388b0b
Author: rfscholte <rf...@apache.org>
AuthorDate: Thu Dec 27 13:50:52 2018 +0100

    Move from String to Path
---
 .../maven/plugins/javadoc/AbstractJavadocMojo.java | 82 +++++++++++-----------
 .../maven/plugins/javadoc/JavadocReport.java       |  2 +-
 .../apache/maven/plugins/javadoc/JavadocUtil.java  | 16 ++---
 .../plugins/javadoc/resolver/ResourceResolver.java | 21 +++---
 .../maven/plugins/javadoc/JavadocUtilTest.java     | 12 +++-
 5 files changed, 68 insertions(+), 65 deletions(-)

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 2253375..f0414f9 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
@@ -1989,9 +1989,9 @@ public abstract class AbstractJavadocMojo
             throw new MavenReportException( "Failed to generate javadoc options file: " + e.getMessage(), e );
         }
 
-        Map<String, Collection<String>> sourcePaths = getSourcePaths();
+        Map<String, Collection<Path>> sourcePaths = getSourcePaths();
         
-        Collection<String> collectedSourcePaths = collect( sourcePaths.values() );
+        Collection<Path> collectedSourcePaths = collect( sourcePaths.values() );
         
         Map<Path, Collection<String>> files = getFiles( collectedSourcePaths );
         if ( !canGenerateReport( files ) )
@@ -2206,10 +2206,10 @@ public abstract class AbstractJavadocMojo
         }
     }
 
-    protected final Collection<String> collect( Collection<Collection<String>> sourcePaths )
+    protected final <T> Collection<T> collect( Collection<Collection<T>> sourcePaths )
     {
-        Collection<String> collectedSourcePaths = new LinkedHashSet<>();
-        for ( Collection<String> sp : sourcePaths )
+        Collection<T> collectedSourcePaths = new LinkedHashSet<>();
+        for ( Collection<T> sp : sourcePaths )
         {
             collectedSourcePaths.addAll( sp );
         }
@@ -2223,7 +2223,7 @@ public abstract class AbstractJavadocMojo
      * @return a List that contains the specific path for every source file
      * @throws MavenReportException {@link MavenReportException}
      */
-    protected Map<Path, Collection<String>> getFiles( Collection<String> sourcePaths )
+    protected Map<Path, Collection<String>> getFiles( Collection<Path> sourcePaths )
         throws MavenReportException
     {
         Map<Path, Collection<String>> mappedFiles = new LinkedHashMap<>( sourcePaths.size() );
@@ -2231,13 +2231,13 @@ public abstract class AbstractJavadocMojo
         {
             Collection<String> excludedPackages = getExcludedPackages();
             
-            for ( String sourcePath : sourcePaths )
+            for ( Path sourcePath : sourcePaths )
             {
                 List<String> files = new ArrayList<>();
-                File sourceDirectory = new File( sourcePath );
+                File sourceDirectory = sourcePath.toFile();
                 files.addAll( JavadocUtil.getFilesFromSource( sourceDirectory, sourceFileIncludes, sourceFileExcludes,
                                                               excludedPackages ) );
-                mappedFiles.put( Paths.get( sourcePath ), files );
+                mappedFiles.put( sourcePath, files );
             }
         }
 
@@ -2252,15 +2252,15 @@ public abstract class AbstractJavadocMojo
      * @throws MavenReportException {@link MavenReportException}
      * @see JavadocUtil#pruneDirs(MavenProject, Collection)
      */
-    protected Map<String, Collection<String>> getSourcePaths()
+    protected Map<String, Collection<Path>> getSourcePaths()
         throws MavenReportException
     {
-        Map<String, Collection<String>> mappedSourcePaths = new LinkedHashMap<>();
+        Map<String, Collection<Path>> mappedSourcePaths = new LinkedHashMap<>();
 
         if ( StringUtils.isEmpty( sourcepath ) )
         {
             
-            Set<String> sourcePaths =
+            Set<Path> sourcePaths =
                 new LinkedHashSet<>( JavadocUtil.pruneDirs( project, getProjectSourceRoots( project ) ) );
 
             if ( project.getExecutionProject() != null )
@@ -2278,7 +2278,7 @@ public abstract class AbstractJavadocMojo
                 File javadocDir = getJavadocDirectory();
                 if ( javadocDir.exists() && javadocDir.isDirectory() )
                 {
-                    Collection<String> l = JavadocUtil.pruneDirs( project, Collections.singletonList(
+                    Collection<Path> l = JavadocUtil.pruneDirs( project, Collections.singletonList(
                         getJavadocDirectory().getAbsolutePath() ) );
                     sourcePaths.addAll( l );
                 }
@@ -2297,7 +2297,7 @@ public abstract class AbstractJavadocMojo
                 {
                     if ( subProject != project )
                     {
-                        Collection<String> additionalSourcePaths = new ArrayList<>();
+                        Collection<Path> additionalSourcePaths = new ArrayList<>();
 
                         List<String> sourceRoots = getProjectSourceRoots( subProject );
 
@@ -2320,7 +2320,7 @@ public abstract class AbstractJavadocMojo
                             File javadocDir = new File( subProject.getBasedir(), javadocDirRelative );
                             if ( javadocDir.exists() && javadocDir.isDirectory() )
                             {
-                                Collection<String> l = JavadocUtil.pruneDirs( subProject, Collections.singletonList(
+                                Collection<Path> l = JavadocUtil.pruneDirs( subProject, Collections.singletonList(
                                         javadocDir.getAbsolutePath() ) );
                                 additionalSourcePaths.addAll( l );
                             }
@@ -2334,11 +2334,12 @@ public abstract class AbstractJavadocMojo
         }
         else
         {
-            Collection<String> sourcePaths = new ArrayList<>( Arrays.asList( JavadocUtil.splitPath( sourcepath ) ) );
-            sourcePaths = JavadocUtil.pruneDirs( project, sourcePaths );
+            Collection<Path> sourcePaths =
+                JavadocUtil.pruneDirs( project,
+                                       new ArrayList<>( Arrays.asList( JavadocUtil.splitPath( sourcepath ) ) ) );
             if ( getJavadocDirectory() != null )
             {
-                Collection<String> l = JavadocUtil.pruneDirs( project, Collections.singletonList(
+                Collection<Path> l = JavadocUtil.pruneDirs( project, Collections.singletonList(
                     getJavadocDirectory().getAbsolutePath() ) );
                 sourcePaths.addAll( l );
             }
@@ -2417,7 +2418,7 @@ public abstract class AbstractJavadocMojo
      * @return List of source paths.
      * @throws MavenReportException {@link MavenReportException}
      */
-    protected final Map<String, Collection<String>> getDependencySourcePaths()
+    protected final Map<String, Collection<Path>> getDependencySourcePaths()
         throws MavenReportException
     {
         try
@@ -2537,17 +2538,11 @@ public abstract class AbstractJavadocMojo
      * @return a String that contains the exclude argument that will be used by javadoc
      * @throws MavenReportException
      */
-    private String getExcludedPackages( Collection<String> sourceFolders )
+    private String getExcludedPackages( Collection<Path> sourcePaths )
         throws MavenReportException
     {
         List<String> excludedNames = null;
         
-        Collection<Path> sourcePaths = new ArrayList<>( sourceFolders.size() );
-        for ( String folder : sourceFolders )
-        {
-            sourcePaths.add( Paths.get( folder ) );
-        }
-
         if ( StringUtils.isNotEmpty( sourcepath ) && StringUtils.isNotEmpty( subpackages ) )
         {
             Collection<String> excludedPackages = getExcludedPackages();
@@ -2573,7 +2568,7 @@ public abstract class AbstractJavadocMojo
      *         string (colon (<code>:</code>) on Solaris or semi-colon (<code>;</code>) on Windows).
      * @see File#pathSeparator
      */
-    private String getSourcePath( Collection<String> sourcePaths )
+    private String getSourcePath( Collection<Path> sourcePaths )
     {
         String sourcePath = null;
 
@@ -3155,15 +3150,14 @@ public abstract class AbstractJavadocMojo
                 taglet.getTagletArtifact().getArtifactId() ) ) && ( StringUtils.isNotEmpty(
                 taglet.getTagletArtifact().getVersion() ) ) )
             {
-                pathParts.addAll( getArtifactsAbsolutePath( taglet.getTagletArtifact() ) );
-
-                pathParts = JavadocUtil.pruneFiles( pathParts );
+                pathParts.addAll( JavadocUtil.pruneFiles( getArtifactsAbsolutePath( taglet.getTagletArtifact() ) ) );
             }
             else if ( StringUtils.isNotEmpty( taglet.getTagletpath() ) )
             {
-                pathParts.add( taglet.getTagletpath() );
-
-                pathParts = JavadocUtil.pruneDirs( project, pathParts );
+                for ( Path dir : JavadocUtil.pruneDirs( project, Collections.singletonList( taglet.getTagletpath() ) ) )
+                {
+                    pathParts.add( dir.toString()  );
+                }
             }
         }
 
@@ -4412,7 +4406,7 @@ public abstract class AbstractJavadocMojo
      * @see #getFiles
      * @see #getSourcePaths()
      */
-    private List<String> getPackageNamesRespectingJavaModules( Map<String, Collection<String>> allSourcePaths )
+    private List<String> getPackageNamesRespectingJavaModules( Map<String, Collection<Path>> allSourcePaths )
             throws MavenReportException
     {
         List<String> returnList = new ArrayList<>();
@@ -4423,7 +4417,7 @@ public abstract class AbstractJavadocMojo
         }
         LocationManager locationManager = new LocationManager();
 
-        for ( Collection<String> artifactSourcePaths: allSourcePaths.values() )
+        for ( Collection<Path> artifactSourcePaths: allSourcePaths.values() )
         {
             Set<String> exportedPackages = new HashSet<>();
             boolean exportAllPackages;
@@ -4821,10 +4815,10 @@ public abstract class AbstractJavadocMojo
      */
     private void addJavadocOptions( File javadocOutputDirectory,
                                     List<String> arguments,
-                                    Map<String, Collection<String>> allSourcePaths )
+                                    Map<String, Collection<Path>> allSourcePaths )
         throws MavenReportException
     {
-        Collection<String> sourcePaths = collect( allSourcePaths.values() );
+        Collection<Path> sourcePaths = collect( allSourcePaths.values() );
         
         validateJavadocOptions();
 
@@ -4866,7 +4860,7 @@ public abstract class AbstractJavadocMojo
         Path moduleSourceDir = null;
         if ( allSourcePaths.size() > 1 )
         {
-            for ( Map.Entry<String, Collection<String>> projectSourcepaths : allSourcePaths.entrySet() )
+            for ( Map.Entry<String, Collection<Path>> projectSourcepaths : allSourcePaths.entrySet() )
             {
                 if ( reactorKeys.contains( projectSourcepaths.getKey() ) )
                 {
@@ -4915,7 +4909,11 @@ public abstract class AbstractJavadocMojo
             }
         }
 
-        List<String> roots = getProjectSourceRoots( getProject() );
+        Collection<Path> roots = new ArrayList<>();
+        for ( String path : getProjectSourceRoots( getProject() ) )
+        {
+            roots.add( Paths.get( path ) );
+        }
         
         File mainDescriptor = findMainDescriptor( roots );
 
@@ -5036,11 +5034,11 @@ public abstract class AbstractJavadocMojo
         }
     }
 
-    private File findMainDescriptor( Collection<String> roots )
+    private File findMainDescriptor( Collection<Path> roots )
     {
-        for ( String root : roots )
+        for ( Path root : roots )
         {
-            File descriptorFile = new File( root, "module-info.java" ).getAbsoluteFile();
+            File descriptorFile = root.resolve( "module-info.java" ).toAbsolutePath().toFile();
             if ( descriptorFile.exists() )
             {
                 return descriptorFile;
diff --git a/src/main/java/org/apache/maven/plugins/javadoc/JavadocReport.java b/src/main/java/org/apache/maven/plugins/javadoc/JavadocReport.java
index f147a31..bdef0fb 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/JavadocReport.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/JavadocReport.java
@@ -229,7 +229,7 @@ public class JavadocReport
 
         if ( this.isAggregator() || !"pom".equals( this.project.getPackaging() ) )
         {
-            Collection<String> sourcePaths;
+            Collection<Path> sourcePaths;
             Map<Path, Collection<String>> files;
             try
             {
diff --git a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
index d57a5b0..31c39b5 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
@@ -125,9 +125,11 @@ public class JavadocUtil
      * @param dirs the collection of <code>String</code> directories path that will be validated.
      * @return a List of valid <code>String</code> directories absolute paths.
      */
-    public static Collection<String> pruneDirs( MavenProject project, Collection<String> dirs )
+    public static Collection<Path> pruneDirs( MavenProject project, Collection<String> dirs )
     {
-        Set<String> pruned = new LinkedHashSet<>( dirs.size() );
+        final Path projectBasedir = project.getBasedir().toPath();
+        
+        Set<Path> pruned = new LinkedHashSet<>( dirs.size() );
         for ( String dir : dirs )
         {
             if ( dir == null )
@@ -135,15 +137,11 @@ public class JavadocUtil
                 continue;
             }
 
-            File directory = new File( dir );
-            if ( !directory.isAbsolute() )
-            {
-                directory = new File( project.getBasedir(), directory.getPath() );
-            }
+            Path directory = projectBasedir.resolve( dir );
 
-            if ( directory.isDirectory() )
+            if ( Files.isDirectory( directory ) )
             {
-                pruned.add( directory.getAbsolutePath() );
+                pruned.add( directory.toAbsolutePath() );
             }
         }
 
diff --git a/src/main/java/org/apache/maven/plugins/javadoc/resolver/ResourceResolver.java b/src/main/java/org/apache/maven/plugins/javadoc/resolver/ResourceResolver.java
index 5e3eeda..99034b2 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/resolver/ResourceResolver.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/resolver/ResourceResolver.java
@@ -24,6 +24,7 @@ import static org.codehaus.plexus.util.IOUtil.close;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.nio.file.Path;
 import java.util.AbstractMap;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -148,10 +149,10 @@ public final class ResourceResolver extends AbstractLogEnabled
      * @throws ArtifactResolutionException {@link ArtifactResolutionException}
      * @throws ArtifactNotFoundException {@link ArtifactNotFoundException}
      */
-    public Map<String, Collection<String>> resolveDependencySourcePaths( final SourceResolverConfig config )
+    public Map<String, Collection<Path>> resolveDependencySourcePaths( final SourceResolverConfig config )
         throws ArtifactResolutionException, ArtifactNotFoundException
     {
-        final Map<String, Collection<String>> mappedDirs = new LinkedHashMap<>();
+        final Map<String, Collection<Path>> mappedDirs = new LinkedHashMap<>();
         
         final Map<String, MavenProject> projectMap = new HashMap<>();
         if ( config.reactorProjects() != null )
@@ -179,7 +180,7 @@ public final class ResourceResolver extends AbstractLogEnabled
             }
         }
 
-        for ( Map.Entry<String, String> entry : resolveFromArtifacts( config, forResourceResolution ) )
+        for ( Map.Entry<String, Path> entry : resolveFromArtifacts( config, forResourceResolution ) )
         {
             mappedDirs.put( entry.getKey(), Collections.singletonList( entry.getValue() ) );
         }
@@ -271,10 +272,10 @@ public final class ResourceResolver extends AbstractLogEnabled
         List<String> dirs = new ArrayList<>( toResolve.size() );
         try
         {
-            for ( Map.Entry<String, String> entry : resolveAndUnpack( toResolve, config, RESOURCE_VALID_CLASSIFIERS,
+            for ( Map.Entry<String, Path> entry : resolveAndUnpack( toResolve, config, RESOURCE_VALID_CLASSIFIERS,
                                                                       false ) )
             {
-                dirs.add( entry.getValue() );
+                dirs.add( entry.getValue().toString() );
             }
         }
         catch ( ArtifactResolutionException | ArtifactNotFoundException e )
@@ -313,7 +314,7 @@ public final class ResourceResolver extends AbstractLogEnabled
         return result;
     }
 
-    private Collection<Entry<String, String>> resolveFromArtifacts( final SourceResolverConfig config,
+    private Collection<Entry<String, Path>> resolveFromArtifacts( final SourceResolverConfig config,
                                                       final List<Artifact> artifacts )
         throws ArtifactResolutionException, ArtifactNotFoundException
     {
@@ -365,7 +366,7 @@ public final class ResourceResolver extends AbstractLogEnabled
      * @throws ArtifactResolutionException if an exception occurs
      * @throws ArtifactNotFoundException if an exception occurs
      */
-    private Collection<Map.Entry<String, String>> resolveAndUnpack( final List<Artifact> artifacts,
+    private Collection<Map.Entry<String, Path>> resolveAndUnpack( final List<Artifact> artifacts,
                                                                     final SourceResolverConfig config,
                                                                     final List<String> validClassifiers,
                                                                     final boolean propagateErrors )
@@ -386,7 +387,7 @@ public final class ResourceResolver extends AbstractLogEnabled
             filter = null;
         }
         
-        final List<Map.Entry<String, String>> result = new ArrayList<>( artifacts.size() );
+        final List<Map.Entry<String, Path>> result = new ArrayList<>( artifacts.size() );
         for ( final Artifact a : artifactSet )
         {
             if ( !validClassifiers.contains( a.getClassifier() ) || ( filter != null && !filter.include( a ) ) )
@@ -421,7 +422,7 @@ public final class ResourceResolver extends AbstractLogEnabled
 
                 unArchiver.extract();
 
-                result.add( new AbstractMap.SimpleEntry<>( a.getDependencyConflictId(), d.getAbsolutePath() ) );
+                result.add( new AbstractMap.SimpleEntry<>( a.getDependencyConflictId(), d.toPath().toAbsolutePath() ) );
             }
             catch ( final NoSuchArchiverException e )
             {
@@ -443,7 +444,7 @@ public final class ResourceResolver extends AbstractLogEnabled
         return result;
     }
 
-    private static Collection<String> resolveFromProject( final SourceResolverConfig config,
+    private static Collection<Path> resolveFromProject( final SourceResolverConfig config,
                                                     final MavenProject reactorProject, final Artifact artifact )
     {
         final List<String> dirs = new ArrayList<>();
diff --git a/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java b/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java
index 139a09f..79b94ba 100644
--- a/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java
+++ b/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java
@@ -28,6 +28,8 @@ import java.io.OutputStream;
 import java.net.SocketTimeoutException;
 import java.net.URI;
 import java.net.URL;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -42,7 +44,9 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.commons.lang3.builder.EqualsBuilder;
+import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
 import org.apache.maven.plugins.javadoc.ProxyServer.AuthAsyncProxyServlet;
+import org.apache.maven.project.MavenProject;
 import org.apache.maven.settings.Proxy;
 import org.apache.maven.settings.Settings;
 import org.codehaus.plexus.PlexusTestCase;
@@ -739,10 +743,12 @@ public class JavadocUtilTest
         list.add( getBasedir() + "/target/classes" );
         list.add( getBasedir() + "/target/classes" );
 
-        String FS = System.getProperty( "file.separator" );
-        Set<String> expected = Collections.singleton( getBasedir() + FS +"target" + FS + "classes" );
+        Set<Path> expected = Collections.singleton( Paths.get( getBasedir(), "target/classes" ) );
+        
+        MavenProjectStub project = new MavenProjectStub();
+        project.setFile( new File( getBasedir(), "pom.xml" ) );
 
-        assertEquals( expected, JavadocUtil.pruneDirs( null, list ) );
+        assertEquals( expected, JavadocUtil.pruneDirs( project, list ) );
     }
 
     /**