From 5bd700ce703a76aa909c3443fba373ce8346865a Mon Sep 17 00:00:00 2001 From: XenoAmess Date: Sat, 30 May 2020 00:42:13 +0800 Subject: [PATCH] [MJAVADOC-653] fix javadoc; fix code smells --- .../javadoc/AbstractFixJavadocMojo.java | 51 +++++------ .../plugins/javadoc/AbstractJavadocMojo.java | 7 +- .../maven/plugins/javadoc/JavadocUtil.java | 84 +++++++++++-------- .../javadoc/AggregatorJavadocReportTest.java | 13 +-- .../plugins/javadoc/FixJavadocMojoTest.java | 13 +-- .../maven/plugins/javadoc/JavadocJarTest.java | 8 +- .../maven/plugins/javadoc/ProxyServer.java | 1 - 7 files changed, 88 insertions(+), 89 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/javadoc/AbstractFixJavadocMojo.java b/src/main/java/org/apache/maven/plugins/javadoc/AbstractFixJavadocMojo.java index c48258f9..86831251 100644 --- a/src/main/java/org/apache/maven/plugins/javadoc/AbstractFixJavadocMojo.java +++ b/src/main/java/org/apache/maven/plugins/javadoc/AbstractFixJavadocMojo.java @@ -802,41 +802,16 @@ private void parseClirrTextOutputFile( File clirrTextOutputFile ) // 7011 - Method Added // 7012 - Method Added to Interface // 8000 - Class Added - List list; - String[] splits2; + // CHECKSTYLE_OFF: MagicNumber switch ( code ) { case 7011: - list = clirrNewMethods.get( split[2].trim() ); - if ( list == null ) - { - list = new ArrayList<>(); - } - splits2 = StringUtils.split( split[3].trim(), "'" ); - if ( splits2.length != 3 ) - { - continue; - } - list.add( splits2[1].trim() ); - clirrNewMethods.put( split[2].trim(), list ); + methodAdded( split ); break; - case 7012: - list = clirrNewMethods.get( split[2].trim() ); - if ( list == null ) - { - list = new ArrayList<>(); - } - splits2 = StringUtils.split( split[3].trim(), "'" ); - if ( splits2.length != 3 ) - { - continue; - } - list.add( splits2[1].trim() ); - clirrNewMethods.put( split[2].trim(), list ); + methodAdded( split ); break; - case 8000: clirrNewClasses.add( split[2].trim() ); break; @@ -856,6 +831,22 @@ private void parseClirrTextOutputFile( File clirrTextOutputFile ) } } + private void methodAdded( String[] split ) + { + List list = clirrNewMethods.get( split[2].trim() ); + if ( list == null ) + { + list = new ArrayList<>(); + } + String[] splits2 = StringUtils.split( split[3].trim(), "'" ); + if ( splits2.length != 3 ) + { + return; + } + list.add( splits2[1].trim() ); + clirrNewMethods.put( split[2].trim(), list ); + } + /** * @param tag not null * @return true if tag is defined in {@link #fixTags}. @@ -1796,11 +1787,13 @@ private void updateJavadocComment( final StringBuilder sb, final String original } } + private static final Pattern REPLACE_LINK_TAGS_PATTERN = Pattern.compile( "\\{@link\\s" ); + static String replaceLinkTags( String comment, JavaAnnotatedElement entity ) { StringBuilder resolvedComment = new StringBuilder(); // scan comment for {@link someClassName} and try to resolve this - Matcher linktagMatcher = Pattern.compile( "\\{@link\\s" ).matcher( comment ); + Matcher linktagMatcher = REPLACE_LINK_TAGS_PATTERN.matcher( comment ); int startIndex = 0; while ( linktagMatcher.find() ) { 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 a20d0701..3c847ce0 100644 --- a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java +++ b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java @@ -564,7 +564,6 @@ public abstract class AbstractJavadocMojo * * @see #links * @see #javaApiLinks - * @see #DEFAULT_JAVA_API_LINKS * @since 2.6 */ @Parameter( property = "detectJavaApiLink", defaultValue = "true" ) @@ -2252,10 +2251,10 @@ protected Map> getFiles( Collection sourcePaths ) for ( Path sourcePath : sourcePaths ) { - List files = new ArrayList<>(); File sourceDirectory = sourcePath.toFile(); - files.addAll( JavadocUtil.getFilesFromSource( sourceDirectory, sourceFileIncludes, sourceFileExcludes, - excludedPackages ) ); + List files = new ArrayList<>( JavadocUtil.getFilesFromSource( sourceDirectory, + sourceFileIncludes, sourceFileExcludes, + excludedPackages ) ); if ( source != null && JavaVersion.parse( source ).isBefore( "9" ) && files.remove( "module-info.java" ) ) 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 43c9d338..0f8c8229 100644 --- a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java +++ b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java @@ -256,24 +256,30 @@ protected static String quotedPathArgument( String value ) if ( StringUtils.isNotEmpty( path ) ) { path = path.replace( '\\', '/' ); - if ( path.contains( "\'" ) ) + if ( path.contains( "'" ) ) { - String split[] = path.split( "\'" ); - path = ""; + StringBuilder pathBuilder = new StringBuilder(); + pathBuilder.append( '\'' ); + String[] split = path.split( "'" ); for ( int i = 0; i < split.length; i++ ) { if ( i != split.length - 1 ) { - path = path + split[i] + "\\'"; + pathBuilder.append( split[i] ).append( "\\'" ); } else { - path = path + split[i]; + pathBuilder.append( split[i] ); } } + pathBuilder.append( '\'' ); + path = pathBuilder.toString(); + } + else + { + path = "'" + path + "'"; } - path = "'" + path + "'"; } return path; @@ -471,10 +477,7 @@ protected static List getFilesFromSource( File sourceDirectory, List files = new ArrayList<>(); if ( fileList.length != 0 ) { - for ( String includedFile : getIncludedFiles( sourceDirectory, fileList, excludePackages ) ) - { - files.add( includedFile ); - } + files.addAll( getIncludedFiles( sourceDirectory, fileList, excludePackages ) ); } return files; @@ -517,7 +520,7 @@ protected static JavaVersion getJavadocVersion( File javadocExe ) { StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() ); msg.append( '\n' ); - msg.append( "Command line was:" + CommandLineUtils.toString( cmd.getCommandline() ) ); + msg.append( "Command line was:" ).append( CommandLineUtils.toString( cmd.getCommandline() ) ); throw new CommandLineException( msg.toString() ); } @@ -533,6 +536,9 @@ else if ( StringUtils.isNotEmpty( out.getOutput() ) ) throw new IllegalArgumentException( "No output found from the command line 'javadoc -J-version'" ); } + private static final Pattern EXTRACT_JAVADOC_VERSION_PATTERN = + Pattern.compile( "(?s).*?[^a-zA-Z](([0-9]+\\.?[0-9]*)(\\.[0-9]+)?).*" ); + /** * Parse the output for 'javadoc -J-version' and return the javadoc version recognized.
* Here are some output for 'javadoc -J-version' depending the JDK used: @@ -581,7 +587,7 @@ protected static String extractJavadocVersion( String output ) throw new IllegalArgumentException( "The output could not be null." ); } - Pattern pattern = Pattern.compile( "(?s).*?[^a-zA-Z](([0-9]+\\.?[0-9]*)(\\.[0-9]+)?).*" ); + Pattern pattern = EXTRACT_JAVADOC_VERSION_PATTERN; Matcher matcher = pattern.matcher( output ); if ( !matcher.matches() ) @@ -593,6 +599,21 @@ protected static String extractJavadocVersion( String output ) return matcher.group( 1 ); } + private static final Pattern PARSE_JAVADOC_MEMORY_PATTERN_0 = + Pattern.compile( "^\\s*(\\d+)\\s*?\\s*$" ); + + private static final Pattern PARSE_JAVADOC_MEMORY_PATTERN_1 = + Pattern.compile( "^\\s*(\\d+)\\s*k(b)?\\s*$", Pattern.CASE_INSENSITIVE ); + + private static final Pattern PARSE_JAVADOC_MEMORY_PATTERN_2 = + Pattern.compile( "^\\s*(\\d+)\\s*m(b)?\\s*$", Pattern.CASE_INSENSITIVE ); + + private static final Pattern PARSE_JAVADOC_MEMORY_PATTERN_3 = + Pattern.compile( "^\\s*(\\d+)\\s*g(b)?\\s*$", Pattern.CASE_INSENSITIVE ); + + private static final Pattern PARSE_JAVADOC_MEMORY_PATTERN_4 = + Pattern.compile( "^\\s*(\\d+)\\s*t(b)?\\s*$", Pattern.CASE_INSENSITIVE ); + /** * Parse a memory string which be used in the JVM arguments -Xms or -Xmx.
* Here are some supported memory string depending the JDK used: @@ -629,39 +650,34 @@ protected static String parseJavadocMemory( String memory ) throw new IllegalArgumentException( "The memory could not be null." ); } - Pattern p = Pattern.compile( "^\\s*(\\d+)\\s*?\\s*$" ); - Matcher m = p.matcher( memory ); - if ( m.matches() ) + Matcher m0 = PARSE_JAVADOC_MEMORY_PATTERN_0.matcher( memory ); + if ( m0.matches() ) { - return m.group( 1 ) + "m"; + return m0.group( 1 ) + "m"; } - p = Pattern.compile( "^\\s*(\\d+)\\s*k(b)?\\s*$", Pattern.CASE_INSENSITIVE ); - m = p.matcher( memory ); - if ( m.matches() ) + Matcher m1 = PARSE_JAVADOC_MEMORY_PATTERN_1.matcher( memory ); + if ( m1.matches() ) { - return m.group( 1 ) + "k"; + return m1.group( 1 ) + "k"; } - p = Pattern.compile( "^\\s*(\\d+)\\s*m(b)?\\s*$", Pattern.CASE_INSENSITIVE ); - m = p.matcher( memory ); - if ( m.matches() ) + Matcher m2 = PARSE_JAVADOC_MEMORY_PATTERN_2.matcher( memory ); + if ( m2.matches() ) { - return m.group( 1 ) + "m"; + return m2.group( 1 ) + "m"; } - p = Pattern.compile( "^\\s*(\\d+)\\s*g(b)?\\s*$", Pattern.CASE_INSENSITIVE ); - m = p.matcher( memory ); - if ( m.matches() ) + Matcher m3 = PARSE_JAVADOC_MEMORY_PATTERN_3.matcher( memory ); + if ( m3.matches() ) { - return ( Integer.parseInt( m.group( 1 ) ) * 1024 ) + "m"; + return ( Integer.parseInt( m3.group( 1 ) ) * 1024 ) + "m"; } - p = Pattern.compile( "^\\s*(\\d+)\\s*t(b)?\\s*$", Pattern.CASE_INSENSITIVE ); - m = p.matcher( memory ); - if ( m.matches() ) + Matcher m4 = PARSE_JAVADOC_MEMORY_PATTERN_4.matcher( memory ); + if ( m4.matches() ) { - return ( Integer.parseInt( m.group( 1 ) ) * 1024 * 1024 ) + "m"; + return ( Integer.parseInt( m4.group( 1 ) ) * 1024 * 1024 ) + "m"; } throw new IllegalArgumentException( "Could convert not to a memory size: " + memory ); @@ -1146,7 +1162,7 @@ private static String getMavenHome( Log log ) { if ( log != null && log.isErrorEnabled() ) { - log.error( "Cannot find Maven application directory. Either specify \'maven.home\' system property, or " + log.error( "Cannot find Maven application directory. Either specify 'maven.home' system property, or " + "M2_HOME environment variable." ); } } @@ -1224,7 +1240,7 @@ private static File getJavaHome( Log log ) { if ( log != null && log.isErrorEnabled() ) { - log.error( "Cannot find Java application directory. Either specify \'java.home\' system property, or " + log.error( "Cannot find Java application directory. Either specify 'java.home' system property, or " + "JAVA_HOME environment variable." ); } } diff --git a/src/test/java/org/apache/maven/plugins/javadoc/AggregatorJavadocReportTest.java b/src/test/java/org/apache/maven/plugins/javadoc/AggregatorJavadocReportTest.java index f8a4e255..7f58c9de 100644 --- a/src/test/java/org/apache/maven/plugins/javadoc/AggregatorJavadocReportTest.java +++ b/src/test/java/org/apache/maven/plugins/javadoc/AggregatorJavadocReportTest.java @@ -171,22 +171,15 @@ private void createTestRepo() private static String readFile( File file ) throws IOException { - String strTmp; StringBuilder str = new StringBuilder( (int) file.length() ); - BufferedReader in = new BufferedReader( new FileReader( file ) ); - try - { - while ( ( strTmp = in.readLine() ) != null ) - { + try (BufferedReader in = new BufferedReader(new FileReader(file))) { + + for ( String strTmp ; ( strTmp = in.readLine() ) != null ; ) { str.append( LINE_SEPARATOR ); str.append( strTmp ); } } - finally - { - in.close(); - } return str.toString(); } diff --git a/src/test/java/org/apache/maven/plugins/javadoc/FixJavadocMojoTest.java b/src/test/java/org/apache/maven/plugins/javadoc/FixJavadocMojoTest.java index 2129e24c..a13d9616 100644 --- a/src/test/java/org/apache/maven/plugins/javadoc/FixJavadocMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/javadoc/FixJavadocMojoTest.java @@ -23,7 +23,7 @@ import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; -import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Properties; @@ -524,25 +524,26 @@ public void testRemoveUnknownExceptions() throws Exception JavaEntityTags javaEntityTags = mojoInstance.parseJavadocTags( source, javaMethod, "", true ); StringBuilder sb = new StringBuilder(); - mojoInstance.writeThrowsTag( sb, javaMethod, javaEntityTags, Arrays.asList( "java.lang.RuntimeException" ) ); + mojoInstance.writeThrowsTag( sb, javaMethod, javaEntityTags, Collections.singletonList("java.lang" + + ".RuntimeException")); assertEquals( " * @throws java.lang.RuntimeException", sb.toString() ); sb = new StringBuilder(); - mojoInstance.writeThrowsTag( sb, javaMethod, javaEntityTags, Arrays.asList( "NumberFormatException" ) ); + mojoInstance.writeThrowsTag( sb, javaMethod, javaEntityTags, Collections.singletonList("NumberFormatException")); assertEquals( " * @throws java.lang.NumberFormatException", sb.toString() ); sb = new StringBuilder(); - mojoInstance.writeThrowsTag( sb, javaMethod, javaEntityTags, Arrays.asList( "java.lang.Exception" ) ); + mojoInstance.writeThrowsTag( sb, javaMethod, javaEntityTags, Collections.singletonList("java.lang.Exception")); assertEquals( "", sb.toString() ); setVariableValueToObject( mojoInstance, "removeUnknownThrows", true ); sb = new StringBuilder(); - mojoInstance.writeThrowsTag( sb, javaMethod, javaEntityTags, Arrays.asList( "com.foo.FatalException" ) ); + mojoInstance.writeThrowsTag( sb, javaMethod, javaEntityTags, Collections.singletonList("com.foo.FatalException")); assertEquals( "", sb.toString() ); setVariableValueToObject( mojoInstance, "removeUnknownThrows", false ); sb = new StringBuilder(); - mojoInstance.writeThrowsTag( sb, javaMethod, javaEntityTags, Arrays.asList( "com.foo.FatalException" ) ); + mojoInstance.writeThrowsTag( sb, javaMethod, javaEntityTags, Collections.singletonList("com.foo.FatalException")); assertEquals( " * @throws com.foo.FatalException if any.", sb.toString() ); } diff --git a/src/test/java/org/apache/maven/plugins/javadoc/JavadocJarTest.java b/src/test/java/org/apache/maven/plugins/javadoc/JavadocJarTest.java index 16d7a4e2..900851cb 100644 --- a/src/test/java/org/apache/maven/plugins/javadoc/JavadocJarTest.java +++ b/src/test/java/org/apache/maven/plugins/javadoc/JavadocJarTest.java @@ -154,7 +154,7 @@ public void testInvalidDestdir() //check if the javadoc jar file was generated File generatedFile = new File( getBasedir(), "target/test/unit/javadocjar-invalid-destdir/target/javadocjar-invalid-destdir-javadoc.jar" ); - assertTrue( !FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); + assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); } public void testContinueIfFailOnErrorIsFalse() throws Exception @@ -200,10 +200,8 @@ public void testIncludeMavenDescriptorWhenExplicitlyConfigured() throws Exceptio expected.add( "META-INF/maven/org.apache.maven.plugins.maven-javadoc-plugin.unit/javadocjar-archive-config/pom.xml" ); expected.add( "META-INF/maven/org.apache.maven.plugins.maven-javadoc-plugin.unit/javadocjar-archive-config/pom.properties" ); - for (int i = 0; i < expected.size(); i++) - { - String entry = expected.get( i ); - assertTrue( "Expected jar to contain " + entry, set.contains( entry ) ); + for (String entry : expected) { + assertTrue("Expected jar to contain " + entry, set.contains(entry)); } } } diff --git a/src/test/java/org/apache/maven/plugins/javadoc/ProxyServer.java b/src/test/java/org/apache/maven/plugins/javadoc/ProxyServer.java index e97356e4..7c1ad2d7 100644 --- a/src/test/java/org/apache/maven/plugins/javadoc/ProxyServer.java +++ b/src/test/java/org/apache/maven/plugins/javadoc/ProxyServer.java @@ -59,7 +59,6 @@ public ProxyServer( AuthAsyncProxyServlet proxyServlet ) /** * @param hostName the server name * @param port the server port - * @param debug true to display System.err, false otherwise. * @param proxyServlet the wanted auth proxy servlet */ public ProxyServer( String hostName, int port, AuthAsyncProxyServlet proxyServlet )