Discussion:
[maven-javadoc-plugin] 02/05: Remove index based loops
r***@apache.org
2018-12-09 14:45:43 UTC
Permalink
This is an automated email from the ASF dual-hosted git repository.

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

commit 58ff79cbfddf077fa169b04c0564f488700a522a
Author: rfscholte <***@apache.org>
AuthorDate: Sat Dec 8 20:52:27 2018 +0100

Remove index based loops
---
.../apache/maven/plugins/javadoc/JavadocUtil.java | 23 +++++++++-------------
1 file changed, 9 insertions(+), 14 deletions(-)

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 23fd546..d6a8ae6 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
@@ -200,8 +200,7 @@ public class JavadocUtil
List<String> excludedNames = new ArrayList<>();
for ( String path : sourcePaths )
{
- List<String> excludes = getExcludedPackages( path, excludedPackages );
- excludedNames.addAll( excludes );
+ excludedNames.addAll( getExcludedPackages( path, excludedPackages ) );
}

return excludedNames;
@@ -380,29 +379,28 @@ public class JavadocUtil
* @param excludePackagenames package names to be excluded in the javadoc
* @return a List of the packagenames to be excluded
*/
- protected static List<String> getExcludedPackages( String sourceDirectory, String[] excludePackagenames )
+ protected static Collection<String> getExcludedPackages( String sourceDirectory, String[] excludePackagenames )
{
List<String> files = new ArrayList<>();
for ( String excludePackagename : excludePackagenames )
{
+ String[] excludeName = excludePackagename.split( "[*]" );
+
String[] fileList = FileUtils.getFilesFromExtension( sourceDirectory, new String[] { "java" } );
for ( String aFileList : fileList )
{
- String[] excludeName = excludePackagename.split( "[*]" );
- int u = 0;
- while ( u < excludeName.length )
+ for ( String excludePart : excludeName )
{
- if ( !"".equals( excludeName[u].trim() ) && aFileList.contains( excludeName[u] )
- && !sourceDirectory.contains( excludeName[u] ) )
+ if ( !"".equals( excludePart.trim() ) && aFileList.contains( excludePart )
+ && !sourceDirectory.contains( excludePart ) )
{
files.add( aFileList );
}
- u++;
}
}
}

- List<String> excluded = new ArrayList<>();
+ Set<String> excluded = new LinkedHashSet<>();
for ( String file : files )
{
int idx = file.lastIndexOf( File.separatorChar );
@@ -411,10 +409,7 @@ public class JavadocUtil
String[] srcSplit = tmpStr.split( Pattern.quote( sourceDirectory.replace( '\\', '/' ) + '/' ) );
String excludedPackage = srcSplit[1].replace( '/', '.' );

- if ( !excluded.contains( excludedPackage ) )
- {
- excluded.add( excludedPackage );
- }
+ excluded.add( excludedPackage );
}

return excluded;
r***@apache.org
2018-12-09 14:45:42 UTC
Permalink
This is an automated email from the ASF dual-hosted git repository.

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

commit 9e053910f8b99814c1baec789f8938ec502a9b74
Author: rfscholte <***@apache.org>
AuthorDate: Fri Dec 7 10:31:54 2018 +0100

[MJAVADOC-548] Directoryname mixed up with excludePackageNames
---
.gitignore | 1 +
.../com/foo/bar/internal/{NotApi.java => Api.java} | 4 +-
src/it/projects/MJAVADOC-384/verify.bsh | 16 +++-
.../maven/plugins/javadoc/AbstractJavadocMojo.java | 4 +-
.../apache/maven/plugins/javadoc/JavadocUtil.java | 92 +++++++---------------
.../maven/plugins/javadoc/JavadocUtilTest.java | 14 ++++
6 files changed, 61 insertions(+), 70 deletions(-)

diff --git a/.gitignore b/.gitignore
index 384de6f..bdbdf67 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,3 +15,4 @@ out/
.java-version
/javadoc-options-javadoc-resources.xml
.checkstyle
+/src/it/mrm/3rdparty/_doclet-1.0.jar
diff --git a/src/it/projects/MJAVADOC-384/src/main/java/com/foo/bar/internal/NotApi.java b/src/it/projects/MJAVADOC-384/src/main/java/com/foo/bar/internal/Api.java
similarity index 95%
rename from src/it/projects/MJAVADOC-384/src/main/java/com/foo/bar/internal/NotApi.java
rename to src/it/projects/MJAVADOC-384/src/main/java/com/foo/bar/internal/Api.java
index 53480de..7121d40 100644
--- a/src/it/projects/MJAVADOC-384/src/main/java/com/foo/bar/internal/NotApi.java
+++ b/src/it/projects/MJAVADOC-384/src/main/java/com/foo/bar/internal/Api.java
@@ -19,7 +19,7 @@ package com.foo.bar.internal;
* under the License.
*/

-/** Not API */
-public class NotApi {
+/** API */
+public class Api {

}
diff --git a/src/it/projects/MJAVADOC-384/verify.bsh b/src/it/projects/MJAVADOC-384/verify.bsh
index dce4671..1c80f44 100644
--- a/src/it/projects/MJAVADOC-384/verify.bsh
+++ b/src/it/projects/MJAVADOC-384/verify.bsh
@@ -36,11 +36,19 @@ for ( File javadocFile : nonExistingJavadocFiles )
}
}

-File javadocFile = new File ( basedir, "target/site/apidocs/com/foo/bar/Api.html" );
-if ( !javadocFile.exists() )
+File[] existingJavadocFiles = new File[] {
+ new File( basedir, "target/site/apidocs/com/foo/bar/Api.html" ),
+ new File( basedir, "target/site/apidocs/com/foo/bar/internal/Api.html" ),
+};
+
+for ( File javadocFile : existingJavadocFiles )
{
- System.err.println( javadocFile.getAbsolutePath() + " is missing." );
- return false;
+ if ( !javadocFile.exists() )
+ {
+ System.err.println( javadocFile.getAbsolutePath() + " is missing." );
+ return false;
+ }
}

+
return true;
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 addcd36..2b65b2e 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
@@ -2214,8 +2214,8 @@ public abstract class AbstractJavadocMojo
for ( String sourcePath : sourcePaths )
{
File sourceDirectory = new File( sourcePath );
- JavadocUtil.addFilesFromSource( files, sourceDirectory, sourceFileIncludes, sourceFileExcludes,
- excludedPackages );
+ files.addAll( JavadocUtil.getFilesFromSource( sourceDirectory, sourceFileIncludes, sourceFileExcludes,
+ excludedPackages ) );
}
}

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 22cab64..23fd546 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
@@ -333,70 +333,39 @@ public class JavadocUtil
* Method that gets the files or classes that would be included in the javadocs using the subpackages parameter.
*
* @param sourceDirectory the directory where the source files are located
- * @param fileList the list of all files found in the sourceDirectory
+ * @param fileList the list of all relative files found in the sourceDirectory
* @param excludePackages package names to be excluded in the javadoc
* @return a StringBuilder that contains the appended file names of the files to be included in the javadoc
*/
protected static List<String> getIncludedFiles( File sourceDirectory, String[] fileList, String[] excludePackages )
{
List<String> files = new ArrayList<>();
+
+ List<Pattern> excludePackagePatterns = new ArrayList<>( excludePackages.length );
+ for ( String excludePackage : excludePackages )
+ {
+ excludePackagePatterns.add( Pattern.compile( excludePackage.replace( "\\", "\\\\" )
+ .replace( ".", "\\." )
+ .replace( "*", ".+" )
+ .concat( "[\\\\/][^\\\\/]+\\.java" )
+ ) );
+ }

- for ( String aFileList : fileList )
+ for ( String file : fileList )
{
- boolean include = true;
- for ( int k = 0; k < excludePackages.length && include; k++ )
+ boolean excluded = false;
+ for ( Pattern excludePackagePattern : excludePackagePatterns )
{
- // handle wildcards (*) in the excludePackageNames
- String[] excludeName = excludePackages[k].split( "[*]" );
-
- if ( excludeName.length == 0 )
+ if ( excludePackagePattern.matcher( file ).matches() )
{
- continue;
- }
-
- if ( excludeName.length > 1 )
- {
- int u = 0;
- while ( include && u < excludeName.length )
- {
- if ( !"".equals( excludeName[u].trim() ) && aFileList.contains( excludeName[u] ) )
- {
- include = false;
- }
- u++;
- }
- }
- else
- {
- if ( aFileList.startsWith( sourceDirectory.toString() + File.separatorChar + excludeName[0] ) )
- {
- if ( excludeName[0].endsWith( String.valueOf( File.separatorChar ) ) )
- {
- int i = aFileList.lastIndexOf( File.separatorChar );
- String packageName = aFileList.substring( 0, i + 1 );
- File currentPackage = new File( packageName );
- File excludedPackage = new File( sourceDirectory, excludeName[0] );
- if ( currentPackage.equals( excludedPackage )
- && aFileList.substring( i ).contains( ".java" ) )
- {
- include = true;
- }
- else
- {
- include = false;
- }
- }
- else
- {
- include = false;
- }
- }
+ excluded = true;
+ break;
}
}
-
- if ( include )
+
+ if ( !excluded )
{
- files.add( quotedPathArgument( aFileList ) );
+ files.add( file );
}
}

@@ -455,13 +424,12 @@ public class JavadocUtil
* Convenience method that gets the files to be included in the javadoc.
*
* @param sourceDirectory the directory where the source files are located
- * @param files the variable that contains the appended filenames of the files to be included in the javadoc
* @param excludePackages the packages to be excluded in the javadocs
* @param sourceFileIncludes files to include.
* @param sourceFileExcludes files to exclude.
*/
- protected static void addFilesFromSource( List<String> files, File sourceDirectory, List<String> sourceFileIncludes,
- List<String> sourceFileExcludes, String[] excludePackages )
+ protected static List<String> getFilesFromSource( File sourceDirectory, List<String> sourceFileIncludes,
+ List<String> sourceFileExcludes, String[] excludePackages )
{
DirectoryScanner ds = new DirectoryScanner();
if ( sourceFileIncludes == null )
@@ -477,17 +445,17 @@ public class JavadocUtil
ds.scan();

String[] fileList = ds.getIncludedFiles();
- String[] pathList = new String[fileList.length];
- for ( int x = 0; x < fileList.length; x++ )
- {
- pathList[x] = new File( sourceDirectory, fileList[x] ).getAbsolutePath();
- }

- if ( pathList.length != 0 )
+ List<String> files = new ArrayList<>();
+ if ( fileList.length != 0 )
{
- List<String> tmpFiles = getIncludedFiles( sourceDirectory, pathList, excludePackages );
- files.addAll( tmpFiles );
+ for ( String includedFile : getIncludedFiles( sourceDirectory, fileList, excludePackages ) )
+ {
+ files.add( quotedPathArgument( new File( sourceDirectory, includedFile ).getAbsolutePath() ) );
+ }
}
+
+ return files;
}

/**
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 825c2a0..b4db5c5 100644
--- a/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java
+++ b/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java
@@ -1,5 +1,7 @@
package org.apache.maven.plugins.javadoc;

+import static org.junit.Assert.assertArrayEquals;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -775,6 +777,18 @@ public class JavadocUtilTest
assertEquals( path1 + ps + path2 + ps + path1 + ps + path2, JavadocUtil.unifyPathSeparator( path1 + ";"
+ path2 + ":" + path1 + ":" + path2 ) );
}
+
+
+ public void testGetIncludedFiles() throws Exception
+ {
+ File sourceDirectory = new File("target/it").getAbsoluteFile();
+ String[] fileList = new String[] { "Main.java" };
+ String[] excludePackages = new String[] { "*.it" };
+
+ List<String> includedFiles = JavadocUtil.getIncludedFiles( sourceDirectory, fileList, excludePackages );
+
+ assertArrayEquals( fileList, includedFiles.toArray( new String[0] ) );
+ }

private void stopSilently( Server server )
{
r***@apache.org
2018-12-09 14:45:45 UTC
Permalink
This is an automated email from the ASF dual-hosted git repository.

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

commit 555fd97d486c9009bd47704561f579c6f1cb875e
Author: rfscholte <***@apache.org>
AuthorDate: Sun Dec 9 14:00:44 2018 +0100

Refactor
---
.../maven/plugins/javadoc/AbstractJavadocMojo.java | 11 +++++++--
.../apache/maven/plugins/javadoc/JavadocUtil.java | 27 ++++++++--------------
2 files changed, 18 insertions(+), 20 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 efc45bc..c0eaaca 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
@@ -111,6 +111,7 @@ import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Arrays;
@@ -2463,16 +2464,22 @@ 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> sourcePaths )
+ private String getExcludedPackages( Collection<String> sourceFolders )
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();

- excludedNames = JavadocUtil.getExcludedNames( sourcePaths, excludedPackages );
+ excludedNames = JavadocUtil.getExcludedPackages( sourcePaths, excludedPackages );
}

String excludeArg = "";
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 a9a547c..a6b0b9b 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
@@ -81,7 +81,6 @@ import java.nio.charset.IllegalCharsetNameException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
-import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
@@ -199,22 +198,15 @@ public class JavadocUtil
*
* @param sourcePaths the path to the source files
* @param excludedPackages the package names to be excluded in the javadoc
- * @return a List of the source files to be excluded in the generated javadoc
+ * @return a List of the packages to be excluded in the generated javadoc
*/
- protected static List<String> getExcludedNames( Collection<String> sourcePaths,
- Collection<String> excludedPackages )
+ protected static List<String> getExcludedPackages( Collection<Path> sourcePaths,
+ Collection<String> excludedPackages )
{
List<String> excludedNames = new ArrayList<>();
- for ( String path : sourcePaths )
+ for ( Path sourcePath : sourcePaths )
{
- for ( String file : getExcludedPackages( Paths.get( path ), excludedPackages ) )
- {
- String tmpStr = file.replace( '\\', '/' );
-
- String[] srcSplit = tmpStr.split( Pattern.quote( path.replace( '\\', '/' ) + '/' ) );
-
- excludedNames.add( srcSplit[1].replace( '/', '.' ) );
- }
+ excludedNames.addAll( getExcludedPackages( sourcePath, excludedPackages ) );
}

return excludedNames;
@@ -394,7 +386,7 @@ public class JavadocUtil
* @param excludePackagenames package names to be excluded in the javadoc
* @return a List of the packagenames to be excluded
*/
- protected static Collection<String> getExcludedPackages( Path sourceDirectory,
+ protected static Collection<String> getExcludedPackages( final Path sourceDirectory,
Collection<String> excludePackagenames )
{
final Collection<String> fileList = new ArrayList<>();
@@ -409,7 +401,7 @@ public class JavadocUtil
{
if ( file.getFileName().toString().endsWith( ".java" ) )
{
- fileList.add( file.toAbsolutePath().toString() );
+ fileList.add( sourceDirectory.relativize( file ).toString() );
}
return FileVisitResult.CONTINUE;
}
@@ -429,12 +421,11 @@ public class JavadocUtil
{
for ( String excludePart : excludeName )
{
- if ( !"".equals( excludePart.trim() ) && aFileList.contains( excludePart )
- && !sourceDirectory.toString().contains( excludePart ) )
+ if ( !"".equals( excludePart.trim() ) && aFileList.contains( excludePart ) )
{
int idx = aFileList.lastIndexOf( File.separatorChar );

- files.add( aFileList.substring( 0, idx ) );
+ files.add( aFileList.substring( 0, idx ).replace( File.separatorChar, '.' ) );
}
}
}
r***@apache.org
2018-12-09 14:45:46 UTC
Permalink
This is an automated email from the ASF dual-hosted git repository.

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

commit 56733178c0241deb114e014609b16c9d9d7a9588
Author: rfscholte <***@apache.org>
AuthorDate: Sun Dec 9 15:45:32 2018 +0100

Use regular expression to match excludePackageNames
---
src/it/projects/MJAVADOC-497/pom.xml | 2 +-
.../maven/plugins/javadoc/AbstractJavadocMojo.java | 10 ++++++++-
.../apache/maven/plugins/javadoc/JavadocUtil.java | 24 +++++++++++++---------
3 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/src/it/projects/MJAVADOC-497/pom.xml b/src/it/projects/MJAVADOC-497/pom.xml
index 9b89c69..c664cd7 100644
--- a/src/it/projects/MJAVADOC-497/pom.xml
+++ b/src/it/projects/MJAVADOC-497/pom.xml
@@ -46,7 +46,7 @@
<version>@pom.version@</version>
<configuration>
<subpackages>com.example.foo</subpackages>
- <excludePackageNames>*.impl.*</excludePackageNames>
+ <excludePackageNames>**.impl</excludePackageNames>
</configuration>
</plugin>
</plugins>
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 c0eaaca..4ff10ec 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
@@ -766,7 +766,14 @@ public abstract class AbstractJavadocMojo
* Unconditionally excludes the specified packages and their subpackages from the list formed by
* <code>-subpackages</code>. Multiple packages can be separated by commas (<code>,</code>), colons (<code>:</code>)
* or semicolons (<code>;</code>).
- * <br/>
+ * <p>
+ * Wildcards work as followed:
+ * <ul>
+ * <li>a wildcard at the beginning should match 1 or more folders</li>
+ * <li>any other wildcard must match exactly one folder</li>
+ * </ul>
+ * </p>
+ * <p>
* Example:
* <pre>
* &lt;excludePackageNames&gt;*.internal:org.acme.exclude1.*:org.acme.exclude2&lt;/excludePackageNames&gt;
@@ -776,6 +783,7 @@ public abstract class AbstractJavadocMojo
* <br/>
* Since <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/javadoc/whatsnew-1.4.html#summary">Java
* 1.4</a>.
+ * </p>
*/
@Parameter( property = "excludePackageNames" )
private String excludePackageNames;
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 a6b0b9b..2e24e06 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
@@ -389,6 +389,8 @@ public class JavadocUtil
protected static Collection<String> getExcludedPackages( final Path sourceDirectory,
Collection<String> excludePackagenames )
{
+ final String regexFileSeparator = File.separator.replace( "\\", "\\\\" );
+
final Collection<String> fileList = new ArrayList<>();

try
@@ -401,7 +403,7 @@ public class JavadocUtil
{
if ( file.getFileName().toString().endsWith( ".java" ) )
{
- fileList.add( sourceDirectory.relativize( file ).toString() );
+ fileList.add( sourceDirectory.relativize( file.getParent() ).toString() );
}
return FileVisitResult.CONTINUE;
}
@@ -415,18 +417,20 @@ public class JavadocUtil
List<String> files = new ArrayList<>();
for ( String excludePackagename : excludePackagenames )
{
- String[] excludeName = excludePackagename.replace( '.', File.separatorChar ).split( "[*]" );
-
+ // Usage of wildcard was bad specified and bad implemented, i.e. using String.contains()
+ // without respecting surrounding context
+ // Following implementation should match requirements as defined in the examples:
+ // - A wildcard at the beginning should match 1 or more folders
+ // - Any other wildcard must match exactly one folder
+ Pattern p = Pattern.compile( excludePackagename.replace( ".", regexFileSeparator )
+ .replaceFirst( "^\\*", ".+" )
+ .replace( "*", "[^" + regexFileSeparator + "]+" ) );
+
for ( String aFileList : fileList )
{
- for ( String excludePart : excludeName )
+ if ( p.matcher( aFileList ).matches() )
{
- if ( !"".equals( excludePart.trim() ) && aFileList.contains( excludePart ) )
- {
- int idx = aFileList.lastIndexOf( File.separatorChar );
-
- files.add( aFileList.substring( 0, idx ).replace( File.separatorChar, '.' ) );
- }
+ files.add( aFileList.replace( File.separatorChar, '.' ) );
}
}
}
r***@apache.org
2018-12-09 14:45:44 UTC
Permalink
This is an automated email from the ASF dual-hosted git repository.

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

commit 138a9c53336bf74508acafddc402535b243333a9
Author: rfscholte <***@apache.org>
AuthorDate: Sun Dec 9 11:24:39 2018 +0100

Refactoring
---
.../maven/plugins/javadoc/AbstractJavadocMojo.java | 24 ++-----
.../apache/maven/plugins/javadoc/JavadocUtil.java | 79 +++++++++++++++-------
.../maven/plugins/javadoc/JavadocUtilTest.java | 3 +-
3 files changed, 62 insertions(+), 44 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 2b65b2e..efc45bc 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
@@ -2209,8 +2209,8 @@ public abstract class AbstractJavadocMojo
List<String> files = new ArrayList<>();
if ( StringUtils.isEmpty( subpackages ) )
{
- String[] excludedPackages = getExcludedPackages();
-
+ Collection<String> excludedPackages = getExcludedPackages();
+
for ( String sourcePath : sourcePaths )
{
File sourceDirectory = new File( sourcePath );
@@ -2470,9 +2470,8 @@ public abstract class AbstractJavadocMojo

if ( StringUtils.isNotEmpty( sourcepath ) && StringUtils.isNotEmpty( subpackages ) )
{
- String[] excludedPackages = getExcludedPackages();
- String[] subpackagesList = subpackages.split( "[:]" );
-
+ Collection<String> excludedPackages = getExcludedPackages();
+
excludedNames = JavadocUtil.getExcludedNames( sourcePaths, excludedPackages );
}

@@ -2513,7 +2512,7 @@ public abstract class AbstractJavadocMojo
* @return an array of String objects that contain the package names
* @throws MavenReportException
*/
- private String[] getExcludedPackages()
+ private Collection<String> getExcludedPackages()
throws MavenReportException
{
Set<String> excluded = new LinkedHashSet<>();
@@ -2550,18 +2549,7 @@ public abstract class AbstractJavadocMojo
excluded.addAll( trimValues( packageNames ) );
}

- String[] result = new String[excluded.size()];
- if ( isNotEmpty( excluded ) )
- {
- int idx = 0;
- for ( String exclude : excluded )
- {
- result[idx] = exclude.replace( '.', File.separatorChar );
- idx++;
- }
- }
-
- return result;
+ return excluded;
}

private static List<String> trimValues( List<String> items )
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 d6a8ae6..a9a547c 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
@@ -78,6 +78,12 @@ import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -195,12 +201,20 @@ public class JavadocUtil
* @param excludedPackages the package names to be excluded in the javadoc
* @return a List of the source files to be excluded in the generated javadoc
*/
- protected static List<String> getExcludedNames( Collection<String> sourcePaths, String[] excludedPackages )
+ protected static List<String> getExcludedNames( Collection<String> sourcePaths,
+ Collection<String> excludedPackages )
{
List<String> excludedNames = new ArrayList<>();
for ( String path : sourcePaths )
{
- excludedNames.addAll( getExcludedPackages( path, excludedPackages ) );
+ for ( String file : getExcludedPackages( Paths.get( path ), excludedPackages ) )
+ {
+ String tmpStr = file.replace( '\\', '/' );
+
+ String[] srcSplit = tmpStr.split( Pattern.quote( path.replace( '\\', '/' ) + '/' ) );
+
+ excludedNames.add( srcSplit[1].replace( '/', '.' ) );
+ }
}

return excludedNames;
@@ -336,15 +350,16 @@ public class JavadocUtil
* @param excludePackages package names to be excluded in the javadoc
* @return a StringBuilder that contains the appended file names of the files to be included in the javadoc
*/
- protected static List<String> getIncludedFiles( File sourceDirectory, String[] fileList, String[] excludePackages )
+ protected static List<String> getIncludedFiles( File sourceDirectory, String[] fileList,
+ Collection<String> excludePackages )
{
List<String> files = new ArrayList<>();

- List<Pattern> excludePackagePatterns = new ArrayList<>( excludePackages.length );
+ List<Pattern> excludePackagePatterns = new ArrayList<>( excludePackages.size() );
for ( String excludePackage : excludePackages )
{
- excludePackagePatterns.add( Pattern.compile( excludePackage.replace( "\\", "\\\\" )
- .replace( ".", "\\." )
+ excludePackagePatterns.add( Pattern.compile( excludePackage.replace( '.', File.separatorChar )
+ .replace( "\\", "\\\\" )
.replace( "*", ".+" )
.concat( "[\\\\/][^\\\\/]+\\.java" )
) );
@@ -379,40 +394,53 @@ public class JavadocUtil
* @param excludePackagenames package names to be excluded in the javadoc
* @return a List of the packagenames to be excluded
*/
- protected static Collection<String> getExcludedPackages( String sourceDirectory, String[] excludePackagenames )
+ protected static Collection<String> getExcludedPackages( Path sourceDirectory,
+ Collection<String> excludePackagenames )
{
+ final Collection<String> fileList = new ArrayList<>();
+
+ try
+ {
+ Files.walkFileTree( sourceDirectory, new SimpleFileVisitor<Path>()
+ {
+ @Override
+ public FileVisitResult visitFile( Path file, BasicFileAttributes attrs )
+ throws IOException
+ {
+ if ( file.getFileName().toString().endsWith( ".java" ) )
+ {
+ fileList.add( file.toAbsolutePath().toString() );
+ }
+ return FileVisitResult.CONTINUE;
+ }
+ } );
+ }
+ catch ( IOException e )
+ {
+ // noop
+ }
+
List<String> files = new ArrayList<>();
for ( String excludePackagename : excludePackagenames )
{
- String[] excludeName = excludePackagename.split( "[*]" );
+ String[] excludeName = excludePackagename.replace( '.', File.separatorChar ).split( "[*]" );

- String[] fileList = FileUtils.getFilesFromExtension( sourceDirectory, new String[] { "java" } );
for ( String aFileList : fileList )
{
for ( String excludePart : excludeName )
{
if ( !"".equals( excludePart.trim() ) && aFileList.contains( excludePart )
- && !sourceDirectory.contains( excludePart ) )
+ && !sourceDirectory.toString().contains( excludePart ) )
{
- files.add( aFileList );
+ int idx = aFileList.lastIndexOf( File.separatorChar );
+
+ files.add( aFileList.substring( 0, idx ) );
}
}
}
}

- Set<String> excluded = new LinkedHashSet<>();
- for ( String file : files )
- {
- int idx = file.lastIndexOf( File.separatorChar );
- String tmpStr = file.substring( 0, idx );
- tmpStr = tmpStr.replace( '\\', '/' );
- String[] srcSplit = tmpStr.split( Pattern.quote( sourceDirectory.replace( '\\', '/' ) + '/' ) );
- String excludedPackage = srcSplit[1].replace( '/', '.' );
-
- excluded.add( excludedPackage );
- }
-
- return excluded;
+ return files;
}

/**
@@ -424,7 +452,8 @@ public class JavadocUtil
* @param sourceFileExcludes files to exclude.
*/
protected static List<String> getFilesFromSource( File sourceDirectory, List<String> sourceFileIncludes,
- List<String> sourceFileExcludes, String[] excludePackages )
+ List<String> sourceFileExcludes,
+ Collection<String> excludePackages )
{
DirectoryScanner ds = new DirectoryScanner();
if ( sourceFileIncludes == null )
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 b4db5c5..139a09f 100644
--- a/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java
+++ b/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java
@@ -29,6 +29,7 @@ import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@@ -783,7 +784,7 @@ public class JavadocUtilTest
{
File sourceDirectory = new File("target/it").getAbsoluteFile();
String[] fileList = new String[] { "Main.java" };
- String[] excludePackages = new String[] { "*.it" };
+ Collection<String> excludePackages = Collections.singleton( "*.it" );

List<String> includedFiles = JavadocUtil.getIncludedFiles( sourceDirectory, fileList, excludePackages );
Loading...