Skip to content

Commit 4291bb7

Browse files
authored
Some miscellaneous linting fixups (#152)
* 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
1 parent 93fd2b2 commit 4291bb7

File tree

16 files changed

+45
-81
lines changed

16 files changed

+45
-81
lines changed

plexus-compiler-api/src/main/java/org/codehaus/plexus/compiler/util/scan/AbstractSourceInclusionScanner.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
public abstract class AbstractSourceInclusionScanner
3232
implements SourceInclusionScanner
3333
{
34-
private final List<SourceMapping> sourceMappings = new ArrayList<SourceMapping>();
34+
private final List<SourceMapping> sourceMappings = new ArrayList<>();
3535

3636
public final void addSourceMapping( SourceMapping sourceMapping )
3737
{

plexus-compiler-api/src/test/java/org/codehaus/plexus/compiler/util/scan/SimpleSourceInclusionScannerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected void setUp()
3838
super.setUp();
3939

4040
includes = Collections.singleton( "*.java" );
41-
excludes = new HashSet<String>();
41+
excludes = new HashSet<>();
4242
scanner = new SimpleSourceInclusionScanner( includes, excludes );
4343
}
4444

plexus-compiler-api/src/test/java/org/codehaus/plexus/compiler/util/scan/mapping/SuffixMappingTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void testShouldReturnOneClassFileAndOneXmlFileForSingleJavaFile()
6666

6767
File basedir = new File( "." );
6868

69-
Set<String> targets = new HashSet<String>();
69+
Set<String> targets = new HashSet<>();
7070
targets.add( ".class" );
7171
targets.add( ".xml" );
7272

@@ -88,7 +88,7 @@ public void testShouldReturnNoTargetFilesWhenSourceFileHasWrongSuffix()
8888

8989
File basedir = new File( "." );
9090

91-
Set<String> targets = new HashSet<String>();
91+
Set<String> targets = new HashSet<>();
9292
targets.add( ".class" );
9393
targets.add( ".xml" );
9494

plexus-compiler-test/src/main/java/org/codehaus/plexus/compiler/AbstractCompilerTckTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ public void testDeprecation()
7777

7878
assertFalse( error.isError() );
7979

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

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

8585
public void testWarning()
@@ -111,7 +111,7 @@ public void testWarning()
111111

112112
assertFalse( error.isError() );
113113

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

117117
protected List<CompilerMessage> compile( CompilerConfiguration configuration )

plexus-compilers/plexus-compiler-aspectj/src/main/java/org/codehaus/plexus/compiler/ajc/AspectJCompiler.java

+5-9
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ private AjBuildConfig buildCompilerConfig( CompilerConfiguration config )
366366
// buildConfig.setJavaOptions( javaOpts );
367367
}
368368

369-
List<String> cp = new LinkedList<String>( config.getClasspathEntries() );
369+
List<String> cp = new LinkedList<>( config.getClasspathEntries() );
370370

371371
File javaHomeDir = new File( System.getProperty( "java.home" ) );
372372
File[] jars = new File( javaHomeDir, "lib" ).listFiles();
@@ -395,7 +395,7 @@ private AjBuildConfig buildCompilerConfig( CompilerConfiguration config )
395395
checkForAspectJRT( cp );
396396
if ( cp != null && !cp.isEmpty() )
397397
{
398-
List<String> elements = new ArrayList<String>( cp.size() );
398+
List<String> elements = new ArrayList<>( cp.size() );
399399
for ( String path : cp )
400400
{
401401
elements.add( ( new File( path ) ).getAbsolutePath() );
@@ -473,11 +473,7 @@ private List<CompilerMessage> compileInProcess( AjBuildConfig buildConfig )
473473
{
474474
manager.batchBuild( buildConfig, messageHandler );
475475
}
476-
catch ( AbortException e )
477-
{
478-
throw new CompilerException( "Unknown error while compiling", e );
479-
}
480-
catch ( IOException e )
476+
catch ( AbortException | IOException e )
481477
{
482478
throw new CompilerException( "Unknown error while compiling", e );
483479
}
@@ -493,7 +489,7 @@ private List<CompilerMessage> compileInProcess( AjBuildConfig buildConfig )
493489

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

496-
List<CompilerMessage> messages = new ArrayList<CompilerMessage>();
492+
List<CompilerMessage> messages = new ArrayList<>();
497493
if ( errors )
498494
{
499495
IMessage[] errorMessages = messageHandler.getMessages( IMessage.ERROR, true );
@@ -554,7 +550,7 @@ private void checkForAspectJRT( List<String> cp )
554550

555551
private List<File> buildFileList( List<String> locations )
556552
{
557-
List<File> fileList = new LinkedList<File>();
553+
List<File> fileList = new LinkedList<>();
558554
for ( String location : locations )
559555
{
560556
fileList.add( new File( location ) );

plexus-compilers/plexus-compiler-aspectj/src/main/java/org/codehaus/plexus/compiler/ajc/AspectJCompilerConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public Map<String, String> getAJOptions()
109109

110110
public void setSourcePathResources( Map<String, File> sourcePathResources )
111111
{
112-
this.sourcePathResources = new TreeMap<String, File>( sourcePathResources );
112+
this.sourcePathResources = new TreeMap<>( sourcePathResources );
113113
}
114114

115115
public Map<String, File> getSourcePathResources()

plexus-compilers/plexus-compiler-csharp/src/main/java/org/codehaus/plexus/compiler/csharp/CSharpCompiler.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -526,11 +526,7 @@ private List<CompilerMessage> compileOutOfProcess( File workingDirectory, File t
526526

527527
messages = parseCompilerOutput( new BufferedReader( new StringReader( stringWriter.toString() ) ) );
528528
}
529-
catch ( CommandLineException e )
530-
{
531-
throw new CompilerException( "Error while executing the external compiler.", e );
532-
}
533-
catch ( IOException e )
529+
catch ( CommandLineException | IOException e )
534530
{
535531
throw new CompilerException( "Error while executing the external compiler.", e );
536532
}

plexus-compilers/plexus-compiler-csharp/src/main/java/org/codehaus/plexus/compiler/csharp/DefaultCSharpCompilerParser.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private static boolean isOutputWithNoColumnNumber( String line )
6262

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

65-
return ( chunk2.indexOf( "," ) == -1 );
65+
return !chunk2.contains( "," );
6666
}
6767

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

8787
return null;
8888
}
89-
else if ( line.indexOf( MAGIC_LINE_MARKER ) != -1 )
89+
else if ( line.contains( MAGIC_LINE_MARKER ) )
9090
{
9191
int i = line.indexOf( MAGIC_LINE_MARKER );
9292

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

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

105-
error = line.indexOf( ") error" ) != -1;
105+
error = line.contains( ") error" );
106106
}
107107
else
108108
{
@@ -134,7 +134,7 @@ else if ( line.startsWith( COMPILATION_PREFIX ) )
134134
{
135135
return null;
136136
}
137-
else if ( line.indexOf( MAGIC_LINE_MARKER ) != -1 )
137+
else if ( line.contains( MAGIC_LINE_MARKER ) )
138138
{
139139
int i = line.indexOf( MAGIC_LINE_MARKER );
140140

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

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

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

176-
error = line.indexOf( "): error" ) != -1;
176+
error = line.contains( "): error" );
177177
}
178178
else
179179
{

plexus-compilers/plexus-compiler-csharp/src/main/java/org/codehaus/plexus/compiler/csharp/JarUtil.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,13 @@ public static void extract( File destDir, File jarFile ) throws IOException
2121
f.mkdir();
2222
continue;
2323
}
24-
InputStream is = jar.getInputStream( file );
25-
FileOutputStream fos = new FileOutputStream( f );
26-
try
24+
try ( InputStream is = jar.getInputStream( file ); FileOutputStream fos = new FileOutputStream( f ) )
2725
{
2826
while ( is.available() > 0 )
2927
{
3028
fos.write( is.read() );
3129
}
3230
}
33-
finally
34-
{
35-
is.close();
36-
fos.close();
37-
}
3831
}
3932
}
4033
}

plexus-compilers/plexus-compiler-javac-errorprone/src/main/java/org/codehaus/plexus/compiler/javac/errorprone/JavacCompilerWithErrorProne.java

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.codehaus.plexus.component.annotations.Component;
3030

3131
import javax.tools.JavaCompiler;
32-
import javax.tools.ToolProvider;
3332

3433
import java.net.MalformedURLException;
3534
import java.net.URL;

plexus-compilers/plexus-compiler-javac-errorprone/src/test/java/org/codehaus/plexus/compiler/javac/JavacErrorProneCompilerTest.java

-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929

3030
import java.lang.reflect.Field;
3131
import java.net.URLClassLoader;
32-
import java.util.Arrays;
33-
import java.util.Collection;
34-
import java.util.Collections;
3532

3633
/**
3734
* @author <a href="mailto:[email protected]">Jason van Zyl</a>

plexus-compilers/plexus-compiler-javac/src/main/java/org/codehaus/plexus/compiler/javac/JavacCompiler.java

+12-29
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public class JavacCompiler
104104

105105
private static volatile Class<?> JAVAC_CLASS;
106106

107-
private List<Class<?>> javaccClasses = new CopyOnWriteArrayList<Class<?>>();
107+
private List<Class<?>> javaccClasses = new CopyOnWriteArrayList<>();
108108

109109
// ----------------------------------------------------------------------
110110
//
@@ -682,26 +682,14 @@ private static CompilerResult compileInProcess0( Class<?> javacClass, String[] a
682682

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

685-
messages = parseModernStream( ok.intValue(), new BufferedReader( new StringReader( out.toString() ) ) );
685+
messages = parseModernStream( ok, new BufferedReader( new StringReader( out.toString() ) ) );
686686
}
687-
catch ( NoSuchMethodException e )
688-
{
689-
throw new CompilerException( "Error while executing the compiler.", e );
690-
}
691-
catch ( IllegalAccessException e )
692-
{
693-
throw new CompilerException( "Error while executing the compiler.", e );
694-
}
695-
catch ( InvocationTargetException e )
696-
{
697-
throw new CompilerException( "Error while executing the compiler.", e );
698-
}
699-
catch ( IOException e )
687+
catch ( NoSuchMethodException | IOException | InvocationTargetException | IllegalAccessException e )
700688
{
701689
throw new CompilerException( "Error while executing the compiler.", e );
702690
}
703691

704-
boolean success = ok.intValue() == 0;
692+
boolean success = ok == 0;
705693
return new CompilerResult( success, messages );
706694
}
707695

@@ -822,9 +810,9 @@ private static boolean isNote( String line )
822810

823811
private static boolean startsWithPrefix( String line, String[] prefixes )
824812
{
825-
for ( int i = 0; i < prefixes.length; i++ )
813+
for ( String prefix : prefixes )
826814
{
827-
if ( line.startsWith( prefixes[i] ) )
815+
if ( line.startsWith( prefix ) )
828816
{
829817
return true;
830818
}
@@ -966,24 +954,19 @@ else if ( msgLine.endsWith( "^" ) )
966954
catch ( NoSuchElementException e )
967955
{
968956
return new CompilerMessage( "no more tokens - could not parse error message: " + error, isError );
969-
}
970-
catch ( NumberFormatException e )
971-
{
972-
return new CompilerMessage( "could not parse error message: " + error, isError );
973-
}
974-
catch ( Exception e )
957+
} catch ( Exception e )
975958
{
976959
return new CompilerMessage( "could not parse error message: " + error, isError );
977960
}
978961
}
979962

980963
private static String getWarnPrefix( String msg )
981964
{
982-
for ( int i = 0; i < WARNING_PREFIXES.length; i++ )
965+
for ( String warningPrefix : WARNING_PREFIXES )
983966
{
984-
if ( msg.startsWith( WARNING_PREFIXES[i] ) )
967+
if ( msg.startsWith( warningPrefix ) )
985968
{
986-
return WARNING_PREFIXES[i];
969+
return warningPrefix;
987970
}
988971
}
989972
return null;
@@ -1016,9 +999,9 @@ private File createFileWithArguments( String[] args, String outputDirectory )
1016999

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

1019-
for ( int i = 0; i < args.length; i++ )
1002+
for ( String arg : args )
10201003
{
1021-
String argValue = args[i].replace( File.separatorChar, '/' );
1004+
String argValue = arg.replace( File.separatorChar, '/' );
10221005

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

plexus-compilers/plexus-compiler-javac/src/main/java/org/codehaus/plexus/compiler/javac/JavaxToolsCompiler.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public CompilerResult compileInProcess( String[] args, final CompilerConfigurati
113113
}
114114
final String sourceEncoding = config.getSourceEncoding();
115115
final Charset sourceCharset = sourceEncoding == null ? null : Charset.forName( sourceEncoding );
116-
final DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<JavaFileObject>();
116+
final DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<>();
117117
try ( final StandardJavaFileManager standardFileManager =
118118
compiler.getStandardFileManager( collector, null, sourceCharset ) )
119119
{
@@ -133,7 +133,7 @@ public CompilerResult compileInProcess( String[] args, final CompilerConfigurati
133133
final JavaCompiler.CompilationTask task =
134134
compiler.getTask( null, standardFileManager, collector, arguments, null, fileObjects );
135135
final Boolean result = task.call();
136-
final ArrayList<CompilerMessage> compilerMsgs = new ArrayList<CompilerMessage>();
136+
final ArrayList<CompilerMessage> compilerMsgs = new ArrayList<>();
137137
for ( Diagnostic<? extends JavaFileObject> diagnostic : collector.getDiagnostics() )
138138
{
139139
CompilerMessage.Kind kind = convertKind(diagnostic);

plexus-compilers/plexus-compiler-javac/src/test/java/org/codehaus/plexus/compiler/javac/AbstractJavacCompilerTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public void testJRuntimeArguments()
287287
expectedArguments.add( "1.3" );
288288

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

495495
// classpathEntires
496496

497-
List<String> classpathEntries = new ArrayList<String>();
497+
List<String> classpathEntries = new ArrayList<>();
498498

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

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

509509
// sourceRoots
510510

511-
List<String> compileSourceRoots = new ArrayList<String>();
511+
List<String> compileSourceRoots = new ArrayList<>();
512512

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

@@ -580,7 +580,7 @@ private void populateArguments( CompilerConfiguration compilerConfiguration, Lis
580580

581581
// customerCompilerArguments
582582

583-
Map<String, String> customerCompilerArguments = new LinkedHashMap<String, String>();
583+
Map<String, String> customerCompilerArguments = new LinkedHashMap<>();
584584

585585
customerCompilerArguments.put( "arg1", null );
586586

plexus-compilers/plexus-compiler-javac/src/test/java/org/codehaus/plexus/compiler/javac/ErrorMessageParserTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ public void testCRLF_windows()
624624
List<CompilerMessage> compilerMessages =
625625
JavacCompiler.parseModernStream( 0, new BufferedReader( new StringReader( errors ) ) );
626626
assertEquals( "count", 187, compilerMessages.size() );
627-
List<CompilerMessage> compilerErrors = new ArrayList<CompilerMessage>( 3 );
627+
List<CompilerMessage> compilerErrors = new ArrayList<>( 3 );
628628
for ( CompilerMessage message : compilerMessages ) {
629629
if ( message.getKind() != CompilerMessage.Kind.OTHER ) {
630630
compilerErrors.add( message );

0 commit comments

Comments
 (0)