Skip to content

Commit 2d1efa5

Browse files
authored
add some java8 sugar syntax usage (#148)
* add some java8 sugar syntax usage Signed-off-by: olivier lamy <olamy@apache.org> * more java8 sugar syntax usage Signed-off-by: olivier lamy <olamy@apache.org>
1 parent f767508 commit 2d1efa5

File tree

15 files changed

+140
-266
lines changed

15 files changed

+140
-266
lines changed

animal-sniffer-ant-tasks/src/main/java/org/codehaus/mojo/animal_sniffer/ant/BuildSignaturesTask.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ public class BuildSignaturesTask
4747

4848
private File destfile;
4949

50-
private Vector<Path> paths = new Vector<Path>();
50+
private Vector<Path> paths = new Vector<>();
5151

52-
private Vector<Signature> signatures = new Vector<Signature>();
52+
private Vector<Signature> signatures = new Vector<>();
5353

54-
private Vector<Ignore> includeClasses = new Vector<Ignore>();
54+
private Vector<Ignore> includeClasses = new Vector<>();
5555

56-
private Vector<Ignore> excludeClasses = new Vector<Ignore>();
56+
private Vector<Ignore> excludeClasses = new Vector<>();
5757

5858
public void setDestfile( File dest )
5959
{
@@ -130,15 +130,15 @@ public void execute()
130130
validate();
131131
try
132132
{
133-
Vector<InputStream> inStreams = new Vector<InputStream>();
133+
Vector<InputStream> inStreams = new Vector<>();
134134
for ( Signature signature : signatures )
135135
{
136136
log( "Importing signatures from " + signature.getSrc() );
137137
inStreams.add( new FileInputStream( signature.getSrc() ) );
138138
}
139139

140140
SignatureBuilder builder =
141-
new SignatureBuilder( inStreams.toArray( new InputStream[inStreams.size()] ),
141+
new SignatureBuilder( inStreams.toArray( new InputStream[0] ),
142142
new FileOutputStream( destfile ), new AntLogger( this ) );
143143
for ( Ignore tmp: includeClasses )
144144
{
@@ -151,10 +151,10 @@ public void execute()
151151
for ( Path path : paths )
152152
{
153153
final String[] files = path.list();
154-
for ( int j = 0; j < files.length; j++ )
154+
for ( String file : files )
155155
{
156-
log( "Capturing signatures from " + files[j], Project.MSG_INFO );
157-
process( builder, new File( files[j] ) );
156+
log( "Capturing signatures from " + file, Project.MSG_INFO );
157+
process( builder, new File( file ) );
158158
}
159159
}
160160
builder.close();

animal-sniffer-ant-tasks/src/main/java/org/codehaus/mojo/animal_sniffer/ant/CheckSignatureTask.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.util.ArrayList;
3232
import java.util.Collection;
3333
import java.util.HashSet;
34-
import java.util.Iterator;
3534
import java.util.List;
3635
import java.util.Set;
3736
import java.util.Vector;
@@ -64,11 +63,11 @@ public class CheckSignatureTask
6463

6564
private boolean failOnError = true;
6665

67-
private Vector<Path> paths = new Vector<Path>();
66+
private Vector<Path> paths = new Vector<>();
6867

69-
private Vector<Ignore> ignores = new Vector<Ignore>();
68+
private Vector<Ignore> ignores = new Vector<>();
7069

71-
private Vector<Annotation> annotations = new Vector<Annotation>();
70+
private Vector<Annotation> annotations = new Vector<>();
7271

7372
public void addPath( Path path )
7473
{
@@ -185,22 +184,20 @@ public void execute()
185184
final SignatureChecker signatureChecker =
186185
new SignatureChecker( new FileInputStream( signature ), ignoredPackages, new AntLogger( this ) );
187186

188-
final List<File> tmp = new ArrayList<File>();
187+
final List<File> tmp = new ArrayList<>();
189188
if (sourcepath != null) {
190-
Iterator<?> i = sourcepath.iterator();
191-
while ( i.hasNext() )
189+
for ( Object next : sourcepath )
192190
{
193-
Object next = i.next();
194191
if ( next instanceof FileResource )
195192
{
196193
final File file = ( (FileResource) next ).getFile();
197-
tmp.add(file);
194+
tmp.add( file );
198195
}
199196
}
200197
}
201198
signatureChecker.setSourcePath(tmp);
202199

203-
final Collection<String> annotationTypes = new HashSet<String>();
200+
final Collection<String> annotationTypes = new HashSet<>();
204201
for ( Annotation annotation : annotations )
205202
{
206203
if ( annotation != null && annotation.getClassName() != null )
@@ -213,9 +210,9 @@ public void execute()
213210
for ( Path path : paths )
214211
{
215212
final String[] files = path.list();
216-
for ( int j = 0; j < files.length; j++ )
213+
for ( String file : files )
217214
{
218-
signatureChecker.process( new File( files[j] ) );
215+
signatureChecker.process( new File( file ) );
219216
}
220217
}
221218

@@ -269,18 +266,16 @@ private void apply( ClassFileVisitor v )
269266
for ( Path path : paths )
270267
{
271268
final String[] files = path.list();
272-
for ( int j = 0; j < files.length; j++ )
269+
for ( String file : files )
273270
{
274-
log( "Ignoring the signatures from file to be checked: " + files[j], Project.MSG_INFO );
275-
v.process( new File( files[j] ) );
271+
log( "Ignoring the signatures from file to be checked: " + file, Project.MSG_INFO );
272+
v.process( new File( file ) );
276273
}
277274
}
278275
if ( classpath != null )
279276
{
280-
Iterator<?> i = classpath.iterator();
281-
while ( i.hasNext() )
277+
for ( Object next : classpath )
282278
{
283-
Object next = i.next();
284279
if ( next instanceof FileResource )
285280
{
286281
final File file = ( (FileResource) next ).getFile();
@@ -290,4 +285,4 @@ private void apply( ClassFileVisitor v )
290285
}
291286
}
292287
}
293-
}
288+
}

animal-sniffer-enforcer-rule/src/main/java/org/codehaus/mojo/animal_sniffer/enforcer/CheckSignatureRule.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,8 @@ public void execute( EnforcerRuleHelper helper )
190190

191191
if ( ignores != null )
192192
{
193-
for ( int i = 0; i < ignores.length; i++ )
193+
for ( String ignore : ignores )
194194
{
195-
String ignore = ignores[i];
196195
if ( ignore == null )
197196
{
198197
continue;
@@ -322,48 +321,49 @@ private void apply( ClassFileVisitor v, File outputDirectory, File testOutputDir
322321
}
323322

324323
logger.debug( "Building list of classes from dependencies" );
325-
for ( Iterator i = project.getArtifacts().iterator(); i.hasNext(); )
324+
for ( Object o : project.getArtifacts() )
326325
{
327326

328-
Artifact artifact = (Artifact) i.next();
327+
Artifact artifact = (Artifact) o;
329328

330-
if ( !artifact.getArtifactHandler().isAddedToClasspath() ) {
331-
logger.debug( "Skipping artifact " + artifactId( artifact )
332-
+ " as it is not added to the classpath." );
329+
if ( !artifact.getArtifactHandler().isAddedToClasspath() )
330+
{
331+
logger.debug(
332+
"Skipping artifact " + artifactId( artifact ) + " as it is not added to the classpath." );
333333
continue;
334334
}
335335

336336
if ( !classpathScopes.contains( artifact.getScope() ) )
337337
{
338-
logger.debug( "Skipping artifact " + artifactId( artifact )
339-
+ " as it is not on the "
340-
+ ( checkTestClasses ? "test" : "compile" ) + " classpath." );
338+
logger.debug( "Skipping artifact " + artifactId( artifact ) + " as it is not on the " + (
339+
checkTestClasses
340+
? "test"
341+
: "compile" ) + " classpath." );
341342
continue;
342343
}
343344

344345
if ( includesFilter != null && !includesFilter.include( artifact ) )
345346
{
346347
logger.debug( "Skipping classes in artifact " + artifactId( artifact )
347-
+ " as it does not match include rules." );
348+
+ " as it does not match include rules." );
348349
continue;
349350
}
350351

351352
if ( excludesFilter != null && !excludesFilter.include( artifact ) )
352353
{
353354
logger.debug( "Skipping classes in artifact " + artifactId( artifact )
354-
+ " as it does matches exclude rules." );
355+
+ " as it does matches exclude rules." );
355356
continue;
356357
}
357358

358359
if ( artifact.getFile() == null )
359360
{
360361
logger.warn( "Skipping classes in artifact " + artifactId( artifact )
361-
+ " as there are unresolved dependencies." );
362+
+ " as there are unresolved dependencies." );
362363
continue;
363364
}
364365

365-
logger.debug( "Adding classes in artifact " + artifactId( artifact ) +
366-
" to the ignores" );
366+
logger.debug( "Adding classes in artifact " + artifactId( artifact ) + " to the ignores" );
367367
v.process( artifact.getFile() );
368368
}
369369
}

animal-sniffer-maven-plugin/src/main/java/org/codehaus/mojo/animal_sniffer/maven/BuildSignaturesMojo.java

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -380,20 +380,20 @@ else if ( tc == null && jdk != null && jdk.getParameters() != null )
380380
if ( includeClasses != null )
381381
{
382382
getLog().info( "Restricting signatures to include only the following classes:" );
383-
for ( int i = 0; i < includeClasses.length; i++ )
383+
for ( String includeClass : includeClasses )
384384
{
385-
getLog().info( " " + includeClasses[i] );
386-
builder.addInclude( includeClasses[i] );
385+
getLog().info( " " + includeClass );
386+
builder.addInclude( includeClass );
387387
}
388388
}
389389

390390
if ( excludeClasses != null )
391391
{
392392
getLog().info( "Restricting signatures to exclude the following classes:" );
393-
for ( int i = 0; i < excludeClasses.length; i++ )
393+
for ( String excludeClass : excludeClasses )
394394
{
395-
getLog().info( " " + excludeClasses[i] );
396-
builder.addExclude( excludeClasses[i] );
395+
getLog().info( " " + excludeClass );
396+
builder.addExclude( excludeClass );
397397
}
398398
}
399399

@@ -422,7 +422,7 @@ private boolean detectJavaBootClasspath( String javaExecutable )
422422
Artifact javaBootClasspathDetector = null;
423423
while ( i.hasNext() && javaBootClasspathDetector == null )
424424
{
425-
Artifact candidate = (Artifact) i.next();
425+
Artifact candidate = i.next();
426426

427427
if ( StringUtils.equals( jbcpdGroupId, candidate.getGroupId() )
428428
&& StringUtils.equals( jbcpdArtifactId, candidate.getArtifactId() ) && candidate.getFile() != null
@@ -528,10 +528,8 @@ private void processModuleDependencies( SignatureBuilder builder )
528528
? null
529529
: new PatternExcludesArtifactFilter( Arrays.asList( excludeDependencies ) );
530530

531-
for ( Iterator<Artifact> i = project.getArtifacts().iterator(); i.hasNext(); )
531+
for ( Artifact artifact : (Iterable<Artifact>) project.getArtifacts() )
532532
{
533-
Artifact artifact = i.next();
534-
535533
if ( includesFilter != null && !includesFilter.include( artifact ) )
536534
{
537535
getLog().debug( "Artifact " + artifactId( artifact ) + " ignored as it does not match include rules." );
@@ -569,17 +567,17 @@ private void processJavaBootClasspath( SignatureBuilder builder )
569567
if ( includeJavaHome && javaHomeClassPath != null && javaHomeClassPath.length > 0 )
570568
{
571569
getLog().debug( "Parsing signatures java classpath:" );
572-
for ( int i = 0; i < javaHomeClassPath.length; i++ )
570+
for ( File file : javaHomeClassPath )
573571
{
574-
if ( javaHomeClassPath[i].isFile() || javaHomeClassPath[i].isDirectory() )
572+
if ( file.isFile() || file.isDirectory() )
575573
{
576-
getLog().debug( "Processing " + javaHomeClassPath[i] );
577-
builder.process( javaHomeClassPath[i] );
574+
getLog().debug( "Processing " + file );
575+
builder.process( file );
578576
}
579577
else
580578
{
581-
getLog().warn( "Could not add signatures from boot classpath element: " + javaHomeClassPath[i]
582-
+ " as it does not exist." );
579+
getLog().warn(
580+
"Could not add signatures from boot classpath element: " + file + " as it does not exist." );
583581
}
584582
}
585583
}
@@ -588,19 +586,16 @@ private void processJavaBootClasspath( SignatureBuilder builder )
588586
private InputStream[] getBaseSignatures()
589587
throws FileNotFoundException
590588
{
591-
List<InputStream> baseSignatures = new ArrayList<InputStream>();
592-
for ( Iterator<Artifact> i = project.getArtifacts().iterator(); i.hasNext(); )
589+
List<InputStream> baseSignatures = new ArrayList<>();
590+
for ( Artifact artifact : (Iterable<Artifact>) project.getArtifacts() )
593591
{
594-
Artifact artifact = i.next();
595592
if ( StringUtils.equals( "signature", artifact.getType() ) )
596593
{
597594
getLog().info( "Importing sigantures from " + artifact.getFile() );
598595
baseSignatures.add( new FileInputStream( artifact.getFile() ) );
599596
}
600597
}
601-
final InputStream[] baseSignatureInputStreams =
602-
(InputStream[]) baseSignatures.toArray( new InputStream[baseSignatures.size()] );
603-
return baseSignatureInputStreams;
598+
return baseSignatures.toArray( new InputStream[0] );
604599
}
605600

606601
/**
@@ -650,12 +645,12 @@ private Toolchain getJdkToolchainFromConfiguration()
650645
try
651646
{
652647
final ToolchainPrivate[] tcp = getToolchains( "jdk" );
653-
for ( int i = 0; i < tcp.length; i++ )
648+
for ( ToolchainPrivate toolchainPrivate : tcp )
654649
{
655-
if ( tcp[i].getType().equals( "jdk" ) /* MNG-5716 */
656-
&& tcp[i].matchesRequirements( jdk.getParameters() ) )
650+
if ( toolchainPrivate.getType().equals( "jdk" ) /* MNG-5716 */
651+
&& toolchainPrivate.matchesRequirements( jdk.getParameters() ) )
657652
{
658-
return tcp[i];
653+
return toolchainPrivate;
659654
}
660655
}
661656
}
@@ -715,9 +710,9 @@ private ToolchainPrivate[] getToolchains( String type )
715710
buf.append( "\n\nCannot find a suitable 'getToolchainsForType' method. Available methods are:\n" );
716711

717712
Method[] methods = managerClass.getMethods();
718-
for ( int i = 0; i < methods.length; i++ )
713+
for ( Method method : methods )
719714
{
720-
buf.append( " " ).append( methods[i] ).append( '\n' );
715+
buf.append( " " ).append( method ).append( '\n' );
721716
}
722717
throw new MojoExecutionException( buf.toString(), e );
723718
}

0 commit comments

Comments
 (0)