diff --git a/animal-sniffer-ant-tasks/src/main/java/org/codehaus/mojo/animal_sniffer/ant/BuildSignaturesTask.java b/animal-sniffer-ant-tasks/src/main/java/org/codehaus/mojo/animal_sniffer/ant/BuildSignaturesTask.java index cb4c2494..ffcf1020 100644 --- a/animal-sniffer-ant-tasks/src/main/java/org/codehaus/mojo/animal_sniffer/ant/BuildSignaturesTask.java +++ b/animal-sniffer-ant-tasks/src/main/java/org/codehaus/mojo/animal_sniffer/ant/BuildSignaturesTask.java @@ -47,13 +47,13 @@ public class BuildSignaturesTask private File destfile; - private Vector paths = new Vector(); + private Vector paths = new Vector<>(); - private Vector signatures = new Vector(); + private Vector signatures = new Vector<>(); - private Vector includeClasses = new Vector(); + private Vector includeClasses = new Vector<>(); - private Vector excludeClasses = new Vector(); + private Vector excludeClasses = new Vector<>(); public void setDestfile( File dest ) { @@ -130,7 +130,7 @@ public void execute() validate(); try { - Vector inStreams = new Vector(); + Vector inStreams = new Vector<>(); for ( Signature signature : signatures ) { log( "Importing signatures from " + signature.getSrc() ); @@ -138,7 +138,7 @@ public void execute() } SignatureBuilder builder = - new SignatureBuilder( inStreams.toArray( new InputStream[inStreams.size()] ), + new SignatureBuilder( inStreams.toArray( new InputStream[0] ), new FileOutputStream( destfile ), new AntLogger( this ) ); for ( Ignore tmp: includeClasses ) { @@ -151,10 +151,10 @@ public void execute() for ( Path path : paths ) { final String[] files = path.list(); - for ( int j = 0; j < files.length; j++ ) + for ( String file : files ) { - log( "Capturing signatures from " + files[j], Project.MSG_INFO ); - process( builder, new File( files[j] ) ); + log( "Capturing signatures from " + file, Project.MSG_INFO ); + process( builder, new File( file ) ); } } builder.close(); diff --git a/animal-sniffer-ant-tasks/src/main/java/org/codehaus/mojo/animal_sniffer/ant/CheckSignatureTask.java b/animal-sniffer-ant-tasks/src/main/java/org/codehaus/mojo/animal_sniffer/ant/CheckSignatureTask.java index 2a40432d..a115ae5e 100644 --- a/animal-sniffer-ant-tasks/src/main/java/org/codehaus/mojo/animal_sniffer/ant/CheckSignatureTask.java +++ b/animal-sniffer-ant-tasks/src/main/java/org/codehaus/mojo/animal_sniffer/ant/CheckSignatureTask.java @@ -31,7 +31,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; -import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.Vector; @@ -64,11 +63,11 @@ public class CheckSignatureTask private boolean failOnError = true; - private Vector paths = new Vector(); + private Vector paths = new Vector<>(); - private Vector ignores = new Vector(); + private Vector ignores = new Vector<>(); - private Vector annotations = new Vector(); + private Vector annotations = new Vector<>(); public void addPath( Path path ) { @@ -185,22 +184,20 @@ public void execute() final SignatureChecker signatureChecker = new SignatureChecker( new FileInputStream( signature ), ignoredPackages, new AntLogger( this ) ); - final List tmp = new ArrayList(); + final List tmp = new ArrayList<>(); if (sourcepath != null) { - Iterator i = sourcepath.iterator(); - while ( i.hasNext() ) + for ( Object next : sourcepath ) { - Object next = i.next(); if ( next instanceof FileResource ) { final File file = ( (FileResource) next ).getFile(); - tmp.add(file); + tmp.add( file ); } } } signatureChecker.setSourcePath(tmp); - final Collection annotationTypes = new HashSet(); + final Collection annotationTypes = new HashSet<>(); for ( Annotation annotation : annotations ) { if ( annotation != null && annotation.getClassName() != null ) @@ -213,9 +210,9 @@ public void execute() for ( Path path : paths ) { final String[] files = path.list(); - for ( int j = 0; j < files.length; j++ ) + for ( String file : files ) { - signatureChecker.process( new File( files[j] ) ); + signatureChecker.process( new File( file ) ); } } @@ -269,18 +266,16 @@ private void apply( ClassFileVisitor v ) for ( Path path : paths ) { final String[] files = path.list(); - for ( int j = 0; j < files.length; j++ ) + for ( String file : files ) { - log( "Ignoring the signatures from file to be checked: " + files[j], Project.MSG_INFO ); - v.process( new File( files[j] ) ); + log( "Ignoring the signatures from file to be checked: " + file, Project.MSG_INFO ); + v.process( new File( file ) ); } } if ( classpath != null ) { - Iterator i = classpath.iterator(); - while ( i.hasNext() ) + for ( Object next : classpath ) { - Object next = i.next(); if ( next instanceof FileResource ) { final File file = ( (FileResource) next ).getFile(); @@ -290,4 +285,4 @@ private void apply( ClassFileVisitor v ) } } } -} \ No newline at end of file +} diff --git a/animal-sniffer-enforcer-rule/src/main/java/org/codehaus/mojo/animal_sniffer/enforcer/CheckSignatureRule.java b/animal-sniffer-enforcer-rule/src/main/java/org/codehaus/mojo/animal_sniffer/enforcer/CheckSignatureRule.java index 3b031b6e..702339cd 100644 --- a/animal-sniffer-enforcer-rule/src/main/java/org/codehaus/mojo/animal_sniffer/enforcer/CheckSignatureRule.java +++ b/animal-sniffer-enforcer-rule/src/main/java/org/codehaus/mojo/animal_sniffer/enforcer/CheckSignatureRule.java @@ -190,9 +190,8 @@ public void execute( EnforcerRuleHelper helper ) if ( ignores != null ) { - for ( int i = 0; i < ignores.length; i++ ) + for ( String ignore : ignores ) { - String ignore = ignores[i]; if ( ignore == null ) { continue; @@ -322,48 +321,49 @@ private void apply( ClassFileVisitor v, File outputDirectory, File testOutputDir } logger.debug( "Building list of classes from dependencies" ); - for ( Iterator i = project.getArtifacts().iterator(); i.hasNext(); ) + for ( Object o : project.getArtifacts() ) { - Artifact artifact = (Artifact) i.next(); + Artifact artifact = (Artifact) o; - if ( !artifact.getArtifactHandler().isAddedToClasspath() ) { - logger.debug( "Skipping artifact " + artifactId( artifact ) - + " as it is not added to the classpath." ); + if ( !artifact.getArtifactHandler().isAddedToClasspath() ) + { + logger.debug( + "Skipping artifact " + artifactId( artifact ) + " as it is not added to the classpath." ); continue; } if ( !classpathScopes.contains( artifact.getScope() ) ) { - logger.debug( "Skipping artifact " + artifactId( artifact ) - + " as it is not on the " - + ( checkTestClasses ? "test" : "compile" ) + " classpath." ); + logger.debug( "Skipping artifact " + artifactId( artifact ) + " as it is not on the " + ( + checkTestClasses + ? "test" + : "compile" ) + " classpath." ); continue; } if ( includesFilter != null && !includesFilter.include( artifact ) ) { logger.debug( "Skipping classes in artifact " + artifactId( artifact ) - + " as it does not match include rules." ); + + " as it does not match include rules." ); continue; } if ( excludesFilter != null && !excludesFilter.include( artifact ) ) { logger.debug( "Skipping classes in artifact " + artifactId( artifact ) - + " as it does matches exclude rules." ); + + " as it does matches exclude rules." ); continue; } if ( artifact.getFile() == null ) { logger.warn( "Skipping classes in artifact " + artifactId( artifact ) - + " as there are unresolved dependencies." ); + + " as there are unresolved dependencies." ); continue; } - logger.debug( "Adding classes in artifact " + artifactId( artifact ) + - " to the ignores" ); + logger.debug( "Adding classes in artifact " + artifactId( artifact ) + " to the ignores" ); v.process( artifact.getFile() ); } } diff --git a/animal-sniffer-maven-plugin/src/main/java/org/codehaus/mojo/animal_sniffer/maven/BuildSignaturesMojo.java b/animal-sniffer-maven-plugin/src/main/java/org/codehaus/mojo/animal_sniffer/maven/BuildSignaturesMojo.java index 83ba9f0d..d56b5c37 100644 --- a/animal-sniffer-maven-plugin/src/main/java/org/codehaus/mojo/animal_sniffer/maven/BuildSignaturesMojo.java +++ b/animal-sniffer-maven-plugin/src/main/java/org/codehaus/mojo/animal_sniffer/maven/BuildSignaturesMojo.java @@ -380,20 +380,20 @@ else if ( tc == null && jdk != null && jdk.getParameters() != null ) if ( includeClasses != null ) { getLog().info( "Restricting signatures to include only the following classes:" ); - for ( int i = 0; i < includeClasses.length; i++ ) + for ( String includeClass : includeClasses ) { - getLog().info( " " + includeClasses[i] ); - builder.addInclude( includeClasses[i] ); + getLog().info( " " + includeClass ); + builder.addInclude( includeClass ); } } if ( excludeClasses != null ) { getLog().info( "Restricting signatures to exclude the following classes:" ); - for ( int i = 0; i < excludeClasses.length; i++ ) + for ( String excludeClass : excludeClasses ) { - getLog().info( " " + excludeClasses[i] ); - builder.addExclude( excludeClasses[i] ); + getLog().info( " " + excludeClass ); + builder.addExclude( excludeClass ); } } @@ -422,7 +422,7 @@ private boolean detectJavaBootClasspath( String javaExecutable ) Artifact javaBootClasspathDetector = null; while ( i.hasNext() && javaBootClasspathDetector == null ) { - Artifact candidate = (Artifact) i.next(); + Artifact candidate = i.next(); if ( StringUtils.equals( jbcpdGroupId, candidate.getGroupId() ) && StringUtils.equals( jbcpdArtifactId, candidate.getArtifactId() ) && candidate.getFile() != null @@ -528,10 +528,8 @@ private void processModuleDependencies( SignatureBuilder builder ) ? null : new PatternExcludesArtifactFilter( Arrays.asList( excludeDependencies ) ); - for ( Iterator i = project.getArtifacts().iterator(); i.hasNext(); ) + for ( Artifact artifact : (Iterable) project.getArtifacts() ) { - Artifact artifact = i.next(); - if ( includesFilter != null && !includesFilter.include( artifact ) ) { getLog().debug( "Artifact " + artifactId( artifact ) + " ignored as it does not match include rules." ); @@ -569,17 +567,17 @@ private void processJavaBootClasspath( SignatureBuilder builder ) if ( includeJavaHome && javaHomeClassPath != null && javaHomeClassPath.length > 0 ) { getLog().debug( "Parsing signatures java classpath:" ); - for ( int i = 0; i < javaHomeClassPath.length; i++ ) + for ( File file : javaHomeClassPath ) { - if ( javaHomeClassPath[i].isFile() || javaHomeClassPath[i].isDirectory() ) + if ( file.isFile() || file.isDirectory() ) { - getLog().debug( "Processing " + javaHomeClassPath[i] ); - builder.process( javaHomeClassPath[i] ); + getLog().debug( "Processing " + file ); + builder.process( file ); } else { - getLog().warn( "Could not add signatures from boot classpath element: " + javaHomeClassPath[i] - + " as it does not exist." ); + getLog().warn( + "Could not add signatures from boot classpath element: " + file + " as it does not exist." ); } } } @@ -588,19 +586,16 @@ private void processJavaBootClasspath( SignatureBuilder builder ) private InputStream[] getBaseSignatures() throws FileNotFoundException { - List baseSignatures = new ArrayList(); - for ( Iterator i = project.getArtifacts().iterator(); i.hasNext(); ) + List baseSignatures = new ArrayList<>(); + for ( Artifact artifact : (Iterable) project.getArtifacts() ) { - Artifact artifact = i.next(); if ( StringUtils.equals( "signature", artifact.getType() ) ) { getLog().info( "Importing sigantures from " + artifact.getFile() ); baseSignatures.add( new FileInputStream( artifact.getFile() ) ); } } - final InputStream[] baseSignatureInputStreams = - (InputStream[]) baseSignatures.toArray( new InputStream[baseSignatures.size()] ); - return baseSignatureInputStreams; + return baseSignatures.toArray( new InputStream[0] ); } /** @@ -650,12 +645,12 @@ private Toolchain getJdkToolchainFromConfiguration() try { final ToolchainPrivate[] tcp = getToolchains( "jdk" ); - for ( int i = 0; i < tcp.length; i++ ) + for ( ToolchainPrivate toolchainPrivate : tcp ) { - if ( tcp[i].getType().equals( "jdk" ) /* MNG-5716 */ - && tcp[i].matchesRequirements( jdk.getParameters() ) ) + if ( toolchainPrivate.getType().equals( "jdk" ) /* MNG-5716 */ + && toolchainPrivate.matchesRequirements( jdk.getParameters() ) ) { - return tcp[i]; + return toolchainPrivate; } } } @@ -715,9 +710,9 @@ private ToolchainPrivate[] getToolchains( String type ) buf.append( "\n\nCannot find a suitable 'getToolchainsForType' method. Available methods are:\n" ); Method[] methods = managerClass.getMethods(); - for ( int i = 0; i < methods.length; i++ ) + for ( Method method : methods ) { - buf.append( " " ).append( methods[i] ).append( '\n' ); + buf.append( " " ).append( method ).append( '\n' ); } throw new MojoExecutionException( buf.toString(), e ); } diff --git a/animal-sniffer-maven-plugin/src/main/java/org/codehaus/mojo/animal_sniffer/maven/CheckSignatureMojo.java b/animal-sniffer-maven-plugin/src/main/java/org/codehaus/mojo/animal_sniffer/maven/CheckSignatureMojo.java index 47661b0b..7ef2edca 100644 --- a/animal-sniffer-maven-plugin/src/main/java/org/codehaus/mojo/animal_sniffer/maven/CheckSignatureMojo.java +++ b/animal-sniffer-maven-plugin/src/main/java/org/codehaus/mojo/animal_sniffer/maven/CheckSignatureMojo.java @@ -53,7 +53,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; -import java.util.Iterator; import java.util.List; import java.util.Set; @@ -252,9 +251,8 @@ public void execute() if ( ignores != null ) { - for ( int i = 0; i < ignores.length; i++ ) + for ( String ignore : ignores ) { - String ignore = ignores[i]; if ( ignore == null ) { continue; @@ -314,9 +312,8 @@ public void execute() private static Dependency findMatchingDependency( Signature signature, List dependencies ) { Dependency match = null; - for ( Iterator iterator = dependencies.iterator(); iterator.hasNext(); ) + for ( Dependency d : dependencies ) { - Dependency d = iterator.next(); if ( StringUtils.isBlank( d.getVersion() ) ) { continue; @@ -386,12 +383,11 @@ private void apply( ClassFileVisitor v ) classpathScopes.addAll( Arrays.asList( Artifact.SCOPE_TEST, Artifact.SCOPE_RUNTIME ) ); } - for ( Iterator i = project.getArtifacts().iterator(); i.hasNext(); ) + for ( Artifact artifact : (Iterable) project.getArtifacts() ) { - Artifact artifact = i.next(); - - if ( !artifact.getArtifactHandler().isAddedToClasspath() ) { + if ( !artifact.getArtifactHandler().isAddedToClasspath() ) + { getLog().debug( "Skipping artifact " + BuildSignaturesMojo.artifactId( artifact ) + " as it is not added to the classpath." ); continue; @@ -399,9 +395,11 @@ private void apply( ClassFileVisitor v ) if ( !classpathScopes.contains( artifact.getScope() ) ) { - getLog().debug( "Skipping artifact " + BuildSignaturesMojo.artifactId( artifact ) - + " as it is not on the " - + ( checkTestClasses ? "test" : "compile" ) + " classpath." ); + getLog().debug( + "Skipping artifact " + BuildSignaturesMojo.artifactId( artifact ) + " as it is not on the " + ( + checkTestClasses + ? "test" + : "compile" ) + " classpath." ); continue; } @@ -419,8 +417,8 @@ private void apply( ClassFileVisitor v ) continue; } - getLog().debug( "Adding classes in artifact " + BuildSignaturesMojo.artifactId( artifact ) + - " to the ignores" ); + getLog().debug( + "Adding classes in artifact " + BuildSignaturesMojo.artifactId( artifact ) + " to the ignores" ); v.process( artifact.getFile() ); } } diff --git a/animal-sniffer-maven-plugin/src/main/java/org/codehaus/mojo/animal_sniffer/maven/JdkToolchainConverter.java b/animal-sniffer-maven-plugin/src/main/java/org/codehaus/mojo/animal_sniffer/maven/JdkToolchainConverter.java index b3fa6f4c..77f3ae45 100644 --- a/animal-sniffer-maven-plugin/src/main/java/org/codehaus/mojo/animal_sniffer/maven/JdkToolchainConverter.java +++ b/animal-sniffer-maven-plugin/src/main/java/org/codehaus/mojo/animal_sniffer/maven/JdkToolchainConverter.java @@ -32,7 +32,6 @@ import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup; import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; import org.codehaus.plexus.configuration.PlexusConfiguration; -import org.codehaus.plexus.configuration.PlexusConfigurationException; import java.util.Collections; import java.util.HashMap; @@ -71,11 +70,11 @@ private JdkToolchain fromConfiguration( PlexusConfiguration configuration, Expre throws ComponentConfigurationException { PlexusConfiguration[] params = configuration.getChildren(); - Map parameters = new HashMap(); - for ( int j = 0; j < params.length; j++ ) + Map parameters = new HashMap<>(); + for ( PlexusConfiguration param : params ) { - String name = params[j].getName(); - String val = params[j].getValue(); + String name = param.getName(); + String val = param.getValue(); parameters.put( name, val ); } final JdkToolchain result = new JdkToolchain(); diff --git a/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/ClassFileVisitor.java b/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/ClassFileVisitor.java index b9ac37a3..40b579e8 100644 --- a/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/ClassFileVisitor.java +++ b/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/ClassFileVisitor.java @@ -29,7 +29,6 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.nio.file.FileVisitOption; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; @@ -39,7 +38,6 @@ import java.util.Collections; import java.util.Comparator; import java.util.Enumeration; -import java.util.Iterator; import java.util.SortedSet; import java.util.TreeSet; import java.util.jar.JarEntry; @@ -86,21 +84,16 @@ public void setCheckJars( boolean checkJars ) public void process( File[] files ) throws IOException { - Arrays.sort( files, new Comparator() - { - public int compare( File f1, File f2 ) - { - String n1 = f1.getName(); - String n2 = f2.getName(); - // Ensure that outer classes are visited before inner classes: - int diff = n1.length() - n2.length(); - return diff != 0 ? diff : n1.compareTo( n2 ); - } - + Arrays.sort( files, ( f1, f2 ) -> { + String n1 = f1.getName(); + String n2 = f2.getName(); + // Ensure that outer classes are visited before inner classes: + int diff = n1.length() - n2.length(); + return diff != 0 ? diff : n1.compareTo( n2 ); } ); - for ( int i = 0; i < files.length; i++ ) + for ( File f : files ) { - process( files[i] ); + process( f ); } } @@ -138,7 +131,7 @@ else if ( ( file.getName().endsWith( ".jar" ) || file.getName().endsWith( ".jmod */ public void process( Path path ) throws IOException { - Files.walkFileTree(path, Collections.emptySet(), 10000, new SimpleFileVisitor() { + Files.walkFileTree(path, Collections.emptySet(), 10000, new SimpleFileVisitor() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { @@ -179,14 +172,11 @@ protected void processJarFile( File file ) { try ( JarFile jar = new JarFile( file ) ) { - SortedSet entries = new TreeSet( new Comparator() { - public int compare( JarEntry e1, JarEntry e2 ) - { - String n1 = e1.getName(); - String n2 = e2.getName(); - int diff = n1.length() - n2.length(); - return diff != 0 ? diff : n1.compareTo( n2 ); - } + SortedSet entries = new TreeSet<>( ( e1, e2 ) -> { + String n1 = e1.getName(); + String n2 = e2.getName(); + int diff = n1.length() - n2.length(); + return diff != 0 ? diff : n1.compareTo( n2 ); } ); Enumeration e = jar.entries(); while ( e.hasMoreElements() ) @@ -204,26 +194,21 @@ public int compare( JarEntry e1, JarEntry e2 ) } entries.add( x ); } - Iterator it = entries.iterator(); - while ( it.hasNext() ) { - JarEntry x = it.next(); + for (JarEntry x : entries) + { // Even debug level seems too verbose for: logger.debug( "Processing " + x.getName() + " in " + file ); - InputStream is = jar.getInputStream( x ); - try + try (InputStream is = jar.getInputStream( x )) { process( file.getPath() + ':' + x.getName(), is ); } - finally - { - is.close(); - } } + } catch ( IOException cause ) { - IOException e = new IOException( " failed to process jar " + file.getPath() + " : " + cause.getMessage() ); - e.initCause( cause ); - throw e; + throw new IOException( " failed to process jar " + file.getPath() + " : " + cause.getMessage(), + cause ); + } } @@ -231,15 +216,10 @@ public int compare( JarEntry e1, JarEntry e2 ) protected void processClassFile( File file ) throws IOException { - InputStream in = new FileInputStream( file ); - try + try (InputStream in = new FileInputStream( file )) { process( file.getPath(), in ); } - finally - { - in.close(); - } } /** diff --git a/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/ClassListBuilder.java b/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/ClassListBuilder.java index ff997db5..e895d6cc 100644 --- a/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/ClassListBuilder.java +++ b/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/ClassListBuilder.java @@ -58,7 +58,7 @@ public ClassListBuilder( Set packages, Logger logger ) public ClassListBuilder( Logger logger ) { - this( new HashSet(), logger ); + this( new HashSet<>(), logger ); } protected void process( String name, InputStream image ) @@ -76,21 +76,12 @@ public void visit( int version, int access, String name, String signature, Strin } }, 0 ); } - catch ( ArrayIndexOutOfBoundsException e ) + catch ( ArrayIndexOutOfBoundsException | IllegalArgumentException e ) { logger.error( "Bad class file " + name ); // MANIMALSNIFFER-9 it is a pity that ASM does not throw a nicer error on encountering a malformed // class file. - IOException ioException = new IOException( "Bad class file " + name ); - ioException.initCause( e ); - throw ioException; - } - catch ( IllegalArgumentException e ) - { - logger.error( "Bad class file " + name ); - IOException ioException = new IOException( "Bad class file " + name ); - ioException.initCause( e ); - throw ioException; + throw new IOException( "Bad class file " + name, e ); } } } diff --git a/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/Clazz.java b/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/Clazz.java index 6c747d2c..6216f3fb 100644 --- a/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/Clazz.java +++ b/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/Clazz.java @@ -72,7 +72,7 @@ public Clazz( String name, Set signatures, String superClass, String[] s this.name = name; this.signatures = signatures; this.superClass = superClass; - this.superInterfaces = (String[]) superInterfaces.clone(); + this.superInterfaces = superInterfaces.clone(); } /** @@ -94,7 +94,7 @@ public Clazz( Clazz defA, Clazz defB ) // nothing we can do... this is a breaking change throw new ClassCastException( "Cannot merge class " + defB.name + " as it has changed superclass:" ); } - Set superInterfaces = new HashSet(); + Set superInterfaces = new HashSet<>(); if ( defA.superInterfaces != null ) { superInterfaces.addAll( Arrays.asList( defA.superInterfaces ) ); @@ -103,13 +103,13 @@ public Clazz( Clazz defA, Clazz defB ) { superInterfaces.addAll( Arrays.asList( defB.superInterfaces ) ); } - Set signatures = new HashSet(); + Set signatures = new HashSet<>(); signatures.addAll( defA.signatures ); signatures.addAll( defB.signatures ); this.name = defA.getName(); this.signatures = signatures; this.superClass = defA.superClass; - this.superInterfaces = (String[]) superInterfaces.toArray( new String[superInterfaces.size()] ); + this.superInterfaces = superInterfaces.toArray( new String[0] ); } public String getName() diff --git a/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/Main.java b/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/Main.java index e7ebb201..1665ed6e 100644 --- a/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/Main.java +++ b/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/Main.java @@ -58,7 +58,7 @@ public static void main( String[] args ) Main m = new Main(); String threshold = null; - List files = new ArrayList(); + List files = new ArrayList<>(); for ( int i = 0; i < args.length; i++ ) { if (args[i].equals("-h")) @@ -83,9 +83,9 @@ public static void main( String[] args ) files.add(new File(args[i])); } - for (int i = 0; i < files.size(); i++) + for ( File f : files ) { - m.process((File) files.get(i)); + m.process( f ); } if (threshold!=null && m.maximumVersion.compareTo(threshold)>0) @@ -107,8 +107,11 @@ protected void process( String name, InputStream image ) if (humanReadableName) { - String hn = (String)HUMAN_READABLE_NAME.get(v); - if (hn!=null) v = hn; + String hn = HUMAN_READABLE_NAME.get(v); + if (hn!=null) + { + v = hn; + } } System.out.println( v + " " + name ); @@ -119,7 +122,7 @@ private static int u2( byte u, byte d ) return ( (int) u ) * 256 + d; } - private static final Map HUMAN_READABLE_NAME = new HashMap(); + private static final Map HUMAN_READABLE_NAME = new HashMap<>(); static { diff --git a/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/RegexUtils.java b/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/RegexUtils.java index 3eb59b9a..f507c9da 100644 --- a/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/RegexUtils.java +++ b/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/RegexUtils.java @@ -71,7 +71,7 @@ public static String quote( String s ) } // damn there's at least one \E in the string - StringBuffer sb = new StringBuffer( s.length() + 32 ); + StringBuilder sb = new StringBuilder( s.length() + 32 ); // each escape-escape takes 10 chars... // hope there's less than 4 of them @@ -105,7 +105,7 @@ public static String quote( String s ) */ public static String convertWildcardsToRegex( String wildcardRule, boolean exactMatch ) { - StringBuffer regex = new StringBuffer(); + StringBuilder regex = new StringBuilder(); int index = 0; final int len = wildcardRule.length(); while ( index < len ) diff --git a/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/SignatureBuilder.java b/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/SignatureBuilder.java index 689b25a5..097e4cc1 100644 --- a/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/SignatureBuilder.java +++ b/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/SignatureBuilder.java @@ -67,7 +67,7 @@ public class SignatureBuilder private List excludeClasses; - private final Map classes = new TreeMap(); + private final Map classes = new TreeMap<>(); public static void main( String[] args ) throws IOException @@ -94,7 +94,7 @@ public void addInclude( String className ) { if ( includeClasses == null ) { - includeClasses = new ArrayList(); + includeClasses = new ArrayList<>(); } includeClasses.add( RegexUtils.compileWildcard( className ) ); } @@ -103,21 +103,20 @@ public void addExclude( String className ) { if ( excludeClasses == null ) { - excludeClasses = new ArrayList(); + excludeClasses = new ArrayList<>(); } excludeClasses.add( RegexUtils.compileWildcard( className ) ); } - public SignatureBuilder( InputStream[] in, OutputStream out, Logger logger ) + public SignatureBuilder( InputStream[] ins, OutputStream out, Logger logger ) throws IOException { this.logger = logger; - if ( in != null ) + if ( ins != null ) { - for ( int i = 0; i < in.length; i++ ) + for ( InputStream in : ins ) { - ObjectInputStream ois = new ObjectInputStream( new GZIPInputStream( in[i] ) ); - try + try (ObjectInputStream ois = new ObjectInputStream( new GZIPInputStream( in ) )) { while ( true ) { @@ -131,13 +130,7 @@ public SignatureBuilder( InputStream[] in, OutputStream out, Logger logger ) } catch ( ClassNotFoundException e ) { - final IOException ioException = new IOException( "Could not read base signatures" ); - ioException.initCause( e ); - throw ioException; - } - finally - { - ois.close(); + throw new IOException( "Could not read base signatures", e ); } } } @@ -213,7 +206,7 @@ public SignatureVisitor() { public void visit( int version, int access, String name, String signature, String superName, String[] interfaces ) { - this.clazz = new Clazz( name, new HashSet(), superName, interfaces ); + this.clazz = new Clazz( name, new HashSet<>(), superName, interfaces ); } public void end() diff --git a/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/SignatureChecker.java b/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/SignatureChecker.java index 8c48c9cd..82fc6ebe 100644 --- a/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/SignatureChecker.java +++ b/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/SignatureChecker.java @@ -74,7 +74,7 @@ public class SignatureChecker */ public static final String PREVIOUS_ANNOTATION_FQN = "org.jvnet.animal_sniffer.IgnoreJRERequirement"; - private final Map classes = new HashMap(); + private final Map classes = new HashMap<>(); private final Logger logger; @@ -86,7 +86,7 @@ public class SignatureChecker private final Set ignoredPackages; - private final Set ignoredOuterClassesOrMethods = new HashSet(); + private final Set ignoredOuterClassesOrMethods = new HashSet<>(); private boolean hadError = false; @@ -97,7 +97,7 @@ public class SignatureChecker public static void main( String[] args ) throws Exception { - Set ignoredPackages = new HashSet(); + Set ignoredPackages = new HashSet<>(); ignoredPackages.add( "org.jvnet.animal_sniffer.*" ); ignoredPackages.add( "org.codehaus.mojo.animal_sniffer.*" ); ignoredPackages.add( "org.objectweb.*" ); @@ -108,8 +108,8 @@ public static void main( String[] args ) public SignatureChecker( InputStream in, Set ignoredPackages, Logger logger ) throws IOException { - this.ignoredPackages = new HashSet(); - this.ignoredPackageRules = new LinkedList(); + this.ignoredPackages = new HashSet<>(); + this.ignoredPackageRules = new LinkedList<>(); for(String wildcard : ignoredPackages ) { if ( wildcard.indexOf( '*' ) == -1 && wildcard.indexOf( '?' ) == -1 ) @@ -121,15 +121,13 @@ public SignatureChecker( InputStream in, Set ignoredPackages, Logger log this.ignoredPackageRules.add( newMatchRule( wildcard.replace( '.', '/' ) ) ); } } - this.annotationDescriptors = new HashSet(); + this.annotationDescriptors = new HashSet<>(); this.annotationDescriptors.add( toAnnotationDescriptor( ANNOTATION_FQN ) ); this.annotationDescriptors.add( toAnnotationDescriptor( PREVIOUS_ANNOTATION_FQN ) ); this.logger = logger; - ObjectInputStream ois = null; - try + try (ObjectInputStream ois = new ObjectInputStream( new GZIPInputStream( in ) )) { - ois = new ObjectInputStream( new GZIPInputStream( in ) ); while ( true ) { Clazz c = (Clazz) ois.readObject(); @@ -144,20 +142,6 @@ public SignatureChecker( InputStream in, Set ignoredPackages, Logger log { throw new NoClassDefFoundError( e.getMessage() ); } - finally - { - if ( ois != null ) - { - try - { - ois.close(); - } - catch ( IOException e ) - { - // ignore - } - } - } } /** @since 1.9 */ @@ -203,9 +187,7 @@ protected void process( final String name, InputStream image ) logger.error( "Bad class file " + name ); // MANIMALSNIFFER-9 it is a pity that ASM does not throw a nicer error on encountering a malformed // class file. - IOException ioException = new IOException( "Bad class file " + name ); - ioException.initCause( e ); - throw ioException; + throw new IOException( "Bad class file " + name, e ); } } @@ -296,7 +278,7 @@ private class CheckingVisitor public CheckingVisitor( String name ) { super(Opcodes.ASM7); - this.ignoredPackageCache = new HashSet( 50 * ignoredPackageRules.size() ); + this.ignoredPackageCache = new HashSet<>( 50 * ignoredPackageRules.size() ); this.name = name; } @@ -376,7 +358,7 @@ public MethodVisitor visitMethod( int access, final String name, final String de */ boolean ignoreError = ignoreClass; Label label = null; - Map> exceptions = new HashMap>(); + Map> exceptions = new HashMap<>(); @Override public void visitEnd() { @@ -437,12 +419,7 @@ public void visitTryCatchBlock( Label start, Label end, Label handler, String ty { if ( type != null ) { - Set exceptionTypes = exceptions.get( handler ); - if ( exceptionTypes == null ) - { - exceptionTypes = new HashSet(); - exceptions.put( handler, exceptionTypes ); - } + Set exceptionTypes = exceptions.computeIfAbsent( handler, k -> new HashSet<>() ); // we collect the types for the handler // because we do not have the line number here // and we need a list for a multi catch block diff --git a/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/SignatureMerger.java b/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/SignatureMerger.java index 0478e434..69602cf9 100644 --- a/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/SignatureMerger.java +++ b/animal-sniffer/src/main/java/org/codehaus/mojo/animal_sniffer/SignatureMerger.java @@ -44,7 +44,7 @@ */ public class SignatureMerger { - private final Map classes = new HashMap(); + private final Map classes = new HashMap<>(); private final Logger logger; @@ -98,4 +98,4 @@ public SignatureMerger( InputStream[] in, OutputStream out, Logger logger ) oos.close(); } -} \ No newline at end of file +} diff --git a/pom.xml b/pom.xml index 93ddb710..8ceb205b 100644 --- a/pom.xml +++ b/pom.xml @@ -103,79 +103,23 @@ - - org.apache.maven.plugins - maven-clean-plugin - 3.1.0 - org.apache.maven.plugins maven-checkstyle-plugin 3.1.2 - - org.apache.maven.plugins - maven-compiler-plugin - 3.8.1 - org.apache.maven.plugins maven-surefire-plugin 2.22.2 - - org.apache.maven.plugins - maven-jar-plugin - 3.2.0 - - - org.apache.maven.plugins - maven-resources-plugin - 3.2.0 - - - org.apache.maven.plugins - maven-gpg-plugin - 1.6 - - - org.apache.maven.plugins - maven-source-plugin - 3.2.1 - org.apache.maven.plugins maven-javadoc-plugin - 3.2.0 false - - org.apache.maven.plugins - maven-assembly-plugin - 3.3.0 - - - org.apache.maven.plugins - maven-install-plugin - 2.5.2 - - - org.apache.maven.plugins - maven-site-plugin - 3.9.1 - - - org.apache.maven.plugins - maven-project-info-reports-plugin - 3.1.1 - - - org.apache.maven.plugins - maven-deploy-plugin - 2.8.2 - org.apache.maven.plugins maven-enforcer-plugin @@ -184,7 +128,6 @@ org.apache.maven.plugins maven-invoker-plugin - 3.2.1 src/it ${project.build.directory}/it @@ -237,7 +180,7 @@ org.apache.maven.plugins maven-compiler-plugin - 7 + 8