Skip to content

Commit

Permalink
Merge pull request #48 from XenoAmess/fix_code_smells
Browse files Browse the repository at this point in the history
[MJAVADOC-653] fix javadoc; fix code smells
  • Loading branch information
elharo committed Aug 19, 2020
2 parents 7a1b7fb + 5bd700c commit 71fb290
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 89 deletions.
Expand Up @@ -802,41 +802,16 @@ private void parseClirrTextOutputFile( File clirrTextOutputFile )
// 7011 - Method Added
// 7012 - Method Added to Interface
// 8000 - Class Added
List<String> 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;
Expand All @@ -856,6 +831,22 @@ private void parseClirrTextOutputFile( File clirrTextOutputFile )
}
}

private void methodAdded( String[] split )
{
List<String> 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 <code>true</code> if <code>tag</code> is defined in {@link #fixTags}.
Expand Down Expand Up @@ -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() )
{
Expand Down
Expand Up @@ -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" )
Expand Down Expand Up @@ -2252,10 +2251,10 @@ protected Map<Path, Collection<String>> getFiles( Collection<Path> sourcePaths )

for ( Path sourcePath : sourcePaths )
{
List<String> files = new ArrayList<>();
File sourceDirectory = sourcePath.toFile();
files.addAll( JavadocUtil.getFilesFromSource( sourceDirectory, sourceFileIncludes, sourceFileExcludes,
excludedPackages ) );
List<String> files = new ArrayList<>( JavadocUtil.getFilesFromSource( sourceDirectory,
sourceFileIncludes, sourceFileExcludes,
excludedPackages ) );

if ( source != null && JavaVersion.parse( source ).isBefore( "9" )
&& files.remove( "module-info.java" ) )
Expand Down
84 changes: 50 additions & 34 deletions src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
Expand Up @@ -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;
Expand Down Expand Up @@ -471,10 +477,7 @@ protected static List<String> getFilesFromSource( File sourceDirectory, List<Str
List<String> 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;
Expand Down Expand Up @@ -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() );
}

Expand All @@ -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. <br>
* Here are some output for 'javadoc -J-version' depending the JDK used:
Expand Down Expand Up @@ -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() )
Expand All @@ -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 <code>-Xms</code> or <code>-Xmx</code>. <br>
* Here are some supported memory string depending the JDK used:
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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." );
}
}
Expand Down Expand Up @@ -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." );
}
}
Expand Down
Expand Up @@ -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();
}
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -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() );
}

Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
}
}
Expand Up @@ -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 )
Expand Down

0 comments on commit 71fb290

Please sign in to comment.