Skip to content

Commit

Permalink
Some miscellaneous linting fixups (#152)
Browse files Browse the repository at this point in the history
* Remove unused imports

* Replace for loops with foreach loops

* Prefer String.contains() over String.indexOf()

* Remove unnecessary unboxing

* Use try-with-resources in JarUtil

* Merge identical catch blocks

* Use <> where we can
  • Loading branch information
rhowe authored Sep 11, 2021
1 parent 93fd2b2 commit 4291bb7
Show file tree
Hide file tree
Showing 16 changed files with 45 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public abstract class AbstractSourceInclusionScanner
implements SourceInclusionScanner
{
private final List<SourceMapping> sourceMappings = new ArrayList<SourceMapping>();
private final List<SourceMapping> sourceMappings = new ArrayList<>();

public final void addSourceMapping( SourceMapping sourceMapping )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected void setUp()
super.setUp();

includes = Collections.singleton( "*.java" );
excludes = new HashSet<String>();
excludes = new HashSet<>();
scanner = new SimpleSourceInclusionScanner( includes, excludes );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void testShouldReturnOneClassFileAndOneXmlFileForSingleJavaFile()

File basedir = new File( "." );

Set<String> targets = new HashSet<String>();
Set<String> targets = new HashSet<>();
targets.add( ".class" );
targets.add( ".xml" );

Expand All @@ -88,7 +88,7 @@ public void testShouldReturnNoTargetFilesWhenSourceFileHasWrongSuffix()

File basedir = new File( "." );

Set<String> targets = new HashSet<String>();
Set<String> targets = new HashSet<>();
targets.add( ".class" );
targets.add( ".xml" );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ public void testDeprecation()

assertFalse( error.isError() );

assertTrue( error.getMessage().indexOf( "Date" ) != -1 );
assertTrue( error.getMessage().contains( "Date" ) );

assertTrue( error.getMessage().indexOf( "deprecated" ) != -1 );
assertTrue( error.getMessage().contains( "deprecated" ) );
}

public void testWarning()
Expand Down Expand Up @@ -111,7 +111,7 @@ public void testWarning()

assertFalse( error.isError() );

assertTrue( error.getMessage().indexOf( "finally block does not complete normally" ) != -1 );
assertTrue( error.getMessage().contains( "finally block does not complete normally" ) );
}

protected List<CompilerMessage> compile( CompilerConfiguration configuration )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ private AjBuildConfig buildCompilerConfig( CompilerConfiguration config )
// buildConfig.setJavaOptions( javaOpts );
}

List<String> cp = new LinkedList<String>( config.getClasspathEntries() );
List<String> cp = new LinkedList<>( config.getClasspathEntries() );

File javaHomeDir = new File( System.getProperty( "java.home" ) );
File[] jars = new File( javaHomeDir, "lib" ).listFiles();
Expand Down Expand Up @@ -395,7 +395,7 @@ private AjBuildConfig buildCompilerConfig( CompilerConfiguration config )
checkForAspectJRT( cp );
if ( cp != null && !cp.isEmpty() )
{
List<String> elements = new ArrayList<String>( cp.size() );
List<String> elements = new ArrayList<>( cp.size() );
for ( String path : cp )
{
elements.add( ( new File( path ) ).getAbsolutePath() );
Expand Down Expand Up @@ -473,11 +473,7 @@ private List<CompilerMessage> compileInProcess( AjBuildConfig buildConfig )
{
manager.batchBuild( buildConfig, messageHandler );
}
catch ( AbortException e )
{
throw new CompilerException( "Unknown error while compiling", e );
}
catch ( IOException e )
catch ( AbortException | IOException e )
{
throw new CompilerException( "Unknown error while compiling", e );
}
Expand All @@ -493,7 +489,7 @@ private List<CompilerMessage> compileInProcess( AjBuildConfig buildConfig )

boolean errors = messageHandler.hasAnyMessage( IMessage.ERROR, true );

List<CompilerMessage> messages = new ArrayList<CompilerMessage>();
List<CompilerMessage> messages = new ArrayList<>();
if ( errors )
{
IMessage[] errorMessages = messageHandler.getMessages( IMessage.ERROR, true );
Expand Down Expand Up @@ -554,7 +550,7 @@ private void checkForAspectJRT( List<String> cp )

private List<File> buildFileList( List<String> locations )
{
List<File> fileList = new LinkedList<File>();
List<File> fileList = new LinkedList<>();
for ( String location : locations )
{
fileList.add( new File( location ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public Map<String, String> getAJOptions()

public void setSourcePathResources( Map<String, File> sourcePathResources )
{
this.sourcePathResources = new TreeMap<String, File>( sourcePathResources );
this.sourcePathResources = new TreeMap<>( sourcePathResources );
}

public Map<String, File> getSourcePathResources()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,11 +526,7 @@ private List<CompilerMessage> compileOutOfProcess( File workingDirectory, File t

messages = parseCompilerOutput( new BufferedReader( new StringReader( stringWriter.toString() ) ) );
}
catch ( CommandLineException e )
{
throw new CompilerException( "Error while executing the external compiler.", e );
}
catch ( IOException e )
catch ( CommandLineException | IOException e )
{
throw new CompilerException( "Error while executing the external compiler.", e );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private static boolean isOutputWithNoColumnNumber( String line )

String chunk2 = chunk1.substring( 0, j );

return ( chunk2.indexOf( "," ) == -1 );
return !chunk2.contains( "," );
}

private static CompilerMessage parseLineWithNoColumnNumber( String line )
Expand All @@ -86,7 +86,7 @@ else if ( line.startsWith( COMPILATION_PREFIX ) )

return null;
}
else if ( line.indexOf( MAGIC_LINE_MARKER ) != -1 )
else if ( line.contains( MAGIC_LINE_MARKER ) )
{
int i = line.indexOf( MAGIC_LINE_MARKER );

Expand All @@ -102,7 +102,7 @@ else if ( line.indexOf( MAGIC_LINE_MARKER ) != -1 )

message = line.substring( j + 1 + ERROR_PREFIX.length() );

error = line.indexOf( ") error" ) != -1;
error = line.contains( ") error" );
}
else
{
Expand Down Expand Up @@ -134,7 +134,7 @@ else if ( line.startsWith( COMPILATION_PREFIX ) )
{
return null;
}
else if ( line.indexOf( MAGIC_LINE_MARKER ) != -1 )
else if ( line.contains( MAGIC_LINE_MARKER ) )
{
int i = line.indexOf( MAGIC_LINE_MARKER );

Expand All @@ -147,7 +147,7 @@ else if ( line.indexOf( MAGIC_LINE_MARKER ) != -1 )
String linenum = null;
String colnum = null;

if ( linecol.indexOf( "," ) > -1 && linecol.split( "," ).length == 2 )
if ( linecol.contains( "," ) && linecol.split( "," ).length == 2 )
{
linenum = linecol.split( "," )[0];
colnum = linecol.split( "," )[1];
Expand All @@ -173,7 +173,7 @@ else if ( linecol.split( "," ).length == 1 )

message = line.substring( j + 1 + ERROR_PREFIX.length() );

error = line.indexOf( "): error" ) != -1;
error = line.contains( "): error" );
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,13 @@ public static void extract( File destDir, File jarFile ) throws IOException
f.mkdir();
continue;
}
InputStream is = jar.getInputStream( file );
FileOutputStream fos = new FileOutputStream( f );
try
try ( InputStream is = jar.getInputStream( file ); FileOutputStream fos = new FileOutputStream( f ) )
{
while ( is.available() > 0 )
{
fos.write( is.read() );
}
}
finally
{
is.close();
fos.close();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.codehaus.plexus.component.annotations.Component;

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;

import java.net.MalformedURLException;
import java.net.URL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@

import java.lang.reflect.Field;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

/**
* @author <a href="mailto:jason@plexus.org">Jason van Zyl</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public class JavacCompiler

private static volatile Class<?> JAVAC_CLASS;

private List<Class<?>> javaccClasses = new CopyOnWriteArrayList<Class<?>>();
private List<Class<?>> javaccClasses = new CopyOnWriteArrayList<>();

// ----------------------------------------------------------------------
//
Expand Down Expand Up @@ -682,26 +682,14 @@ private static CompilerResult compileInProcess0( Class<?> javacClass, String[] a

ok = (Integer) compile.invoke( null, new Object[]{ args, new PrintWriter( out ) } );

messages = parseModernStream( ok.intValue(), new BufferedReader( new StringReader( out.toString() ) ) );
messages = parseModernStream( ok, new BufferedReader( new StringReader( out.toString() ) ) );
}
catch ( NoSuchMethodException e )
{
throw new CompilerException( "Error while executing the compiler.", e );
}
catch ( IllegalAccessException e )
{
throw new CompilerException( "Error while executing the compiler.", e );
}
catch ( InvocationTargetException e )
{
throw new CompilerException( "Error while executing the compiler.", e );
}
catch ( IOException e )
catch ( NoSuchMethodException | IOException | InvocationTargetException | IllegalAccessException e )
{
throw new CompilerException( "Error while executing the compiler.", e );
}

boolean success = ok.intValue() == 0;
boolean success = ok == 0;
return new CompilerResult( success, messages );
}

Expand Down Expand Up @@ -822,9 +810,9 @@ private static boolean isNote( String line )

private static boolean startsWithPrefix( String line, String[] prefixes )
{
for ( int i = 0; i < prefixes.length; i++ )
for ( String prefix : prefixes )
{
if ( line.startsWith( prefixes[i] ) )
if ( line.startsWith( prefix ) )
{
return true;
}
Expand Down Expand Up @@ -966,24 +954,19 @@ else if ( msgLine.endsWith( "^" ) )
catch ( NoSuchElementException e )
{
return new CompilerMessage( "no more tokens - could not parse error message: " + error, isError );
}
catch ( NumberFormatException e )
{
return new CompilerMessage( "could not parse error message: " + error, isError );
}
catch ( Exception e )
} catch ( Exception e )
{
return new CompilerMessage( "could not parse error message: " + error, isError );
}
}

private static String getWarnPrefix( String msg )
{
for ( int i = 0; i < WARNING_PREFIXES.length; i++ )
for ( String warningPrefix : WARNING_PREFIXES )
{
if ( msg.startsWith( WARNING_PREFIXES[i] ) )
if ( msg.startsWith( warningPrefix ) )
{
return WARNING_PREFIXES[i];
return warningPrefix;
}
}
return null;
Expand Down Expand Up @@ -1016,9 +999,9 @@ private File createFileWithArguments( String[] args, String outputDirectory )

writer = new PrintWriter( new FileWriter( tempFile ) );

for ( int i = 0; i < args.length; i++ )
for ( String arg : args )
{
String argValue = args[i].replace( File.separatorChar, '/' );
String argValue = arg.replace( File.separatorChar, '/' );

writer.write( "\"" + argValue + "\"" );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public CompilerResult compileInProcess( String[] args, final CompilerConfigurati
}
final String sourceEncoding = config.getSourceEncoding();
final Charset sourceCharset = sourceEncoding == null ? null : Charset.forName( sourceEncoding );
final DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<JavaFileObject>();
final DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<>();
try ( final StandardJavaFileManager standardFileManager =
compiler.getStandardFileManager( collector, null, sourceCharset ) )
{
Expand All @@ -133,7 +133,7 @@ public CompilerResult compileInProcess( String[] args, final CompilerConfigurati
final JavaCompiler.CompilationTask task =
compiler.getTask( null, standardFileManager, collector, arguments, null, fileObjects );
final Boolean result = task.call();
final ArrayList<CompilerMessage> compilerMsgs = new ArrayList<CompilerMessage>();
final ArrayList<CompilerMessage> compilerMsgs = new ArrayList<>();
for ( Diagnostic<? extends JavaFileObject> diagnostic : collector.getDiagnostics() )
{
CompilerMessage.Kind kind = convertKind(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public void testJRuntimeArguments()
expectedArguments.add( "1.3" );

// customCompilerArguments
Map<String, String> customCompilerArguments = new LinkedHashMap<String, String>();
Map<String, String> customCompilerArguments = new LinkedHashMap<>();
customCompilerArguments.put( "-J-Duser.language=en_us", null );
compilerConfiguration.setCustomCompilerArgumentsAsMap( customCompilerArguments );
// don't expect this argument!!
Expand Down Expand Up @@ -494,7 +494,7 @@ private void populateArguments( CompilerConfiguration compilerConfiguration, Lis

// classpathEntires

List<String> classpathEntries = new ArrayList<String>();
List<String> classpathEntries = new ArrayList<>();

classpathEntries.add( "/myjar1.jar" );

Expand All @@ -508,7 +508,7 @@ private void populateArguments( CompilerConfiguration compilerConfiguration, Lis

// sourceRoots

List<String> compileSourceRoots = new ArrayList<String>();
List<String> compileSourceRoots = new ArrayList<>();

compileSourceRoots.add( "/src/main/one" );

Expand Down Expand Up @@ -580,7 +580,7 @@ private void populateArguments( CompilerConfiguration compilerConfiguration, Lis

// customerCompilerArguments

Map<String, String> customerCompilerArguments = new LinkedHashMap<String, String>();
Map<String, String> customerCompilerArguments = new LinkedHashMap<>();

customerCompilerArguments.put( "arg1", null );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ public void testCRLF_windows()
List<CompilerMessage> compilerMessages =
JavacCompiler.parseModernStream( 0, new BufferedReader( new StringReader( errors ) ) );
assertEquals( "count", 187, compilerMessages.size() );
List<CompilerMessage> compilerErrors = new ArrayList<CompilerMessage>( 3 );
List<CompilerMessage> compilerErrors = new ArrayList<>( 3 );
for ( CompilerMessage message : compilerMessages ) {
if ( message.getKind() != CompilerMessage.Kind.OTHER ) {
compilerErrors.add( message );
Expand Down
Loading

0 comments on commit 4291bb7

Please sign in to comment.