From cfe0355b7213fa67ec01d4a49f00e609e08c0ce2 Mon Sep 17 00:00:00 2001 From: rfscholte Date: Sun, 26 Apr 2020 20:22:03 +0200 Subject: [PATCH] Apply try-with-resources --- .../archive/ManifestCreationFinalizer.java | 13 +-- .../AbstractLineAggregatingHandler.java | 31 ++----- .../ComponentsXmlArchiverFileFilter.java | 24 +---- .../SimpleAggregatingDescriptorHandler.java | 40 ++++----- .../assembly/io/DefaultAssemblyReader.java | 45 ++-------- .../assembly/utils/LineEndingsUtils.java | 89 ++++++++----------- 6 files changed, 74 insertions(+), 168 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/assembly/archive/ManifestCreationFinalizer.java b/src/main/java/org/apache/maven/plugins/assembly/archive/ManifestCreationFinalizer.java index dbd76276d..cdda6f75b 100644 --- a/src/main/java/org/apache/maven/plugins/assembly/archive/ManifestCreationFinalizer.java +++ b/src/main/java/org/apache/maven/plugins/assembly/archive/ManifestCreationFinalizer.java @@ -30,7 +30,6 @@ import org.codehaus.plexus.archiver.jar.JarArchiver; import org.codehaus.plexus.archiver.jar.Manifest; import org.codehaus.plexus.archiver.jar.ManifestException; -import org.codehaus.plexus.util.IOUtil; import java.io.File; import java.io.FileInputStream; @@ -82,14 +81,10 @@ public void finalizeArchiveCreation( final Archiver archiver ) if ( manifestFile != null ) { - Reader manifestFileReader = null; - try + try ( Reader manifestFileReader = + new InputStreamReader( new FileInputStream( manifestFile ), StandardCharsets.UTF_8 ) ) { - manifestFileReader = new InputStreamReader( new FileInputStream( manifestFile ), - StandardCharsets.UTF_8 ); manifest = new Manifest( manifestFileReader ); - manifestFileReader.close(); - manifestFileReader = null; } catch ( final FileNotFoundException e ) { @@ -99,10 +94,6 @@ public void finalizeArchiveCreation( final Archiver archiver ) { throw new ArchiverException( "Error processing manifest: " + e.getMessage(), e ); } - finally - { - IOUtil.close( manifestFileReader ); - } } else { diff --git a/src/main/java/org/apache/maven/plugins/assembly/filter/AbstractLineAggregatingHandler.java b/src/main/java/org/apache/maven/plugins/assembly/filter/AbstractLineAggregatingHandler.java index 49a16b9ee..12f101200 100644 --- a/src/main/java/org/apache/maven/plugins/assembly/filter/AbstractLineAggregatingHandler.java +++ b/src/main/java/org/apache/maven/plugins/assembly/filter/AbstractLineAggregatingHandler.java @@ -25,7 +25,6 @@ import org.codehaus.plexus.archiver.ResourceIterator; import org.codehaus.plexus.archiver.UnArchiver; import org.codehaus.plexus.components.io.fileselectors.FileInfo; -import org.codehaus.plexus.util.IOUtil; import javax.annotation.Nonnull; import java.io.BufferedReader; @@ -80,21 +79,20 @@ void addToArchive( final Archiver archiver ) final String name = entry.getKey(); final String fname = new File( name ).getName(); - PrintWriter writer = null; File f; try { f = File.createTempFile( "assembly-" + fname, ".tmp" ); f.deleteOnExit(); - writer = new PrintWriter( new OutputStreamWriter( new FileOutputStream( f ), getEncoding() ) ); - for ( final String line : entry.getValue() ) + try ( PrintWriter writer = + new PrintWriter( new OutputStreamWriter( new FileOutputStream( f ), getEncoding() ) ) ) { - writer.println( line ); + for ( final String line : entry.getValue() ) + { + writer.println( line ); + } } - - writer.close(); - writer = null; } catch ( final IOException e ) { @@ -102,10 +100,6 @@ void addToArchive( final Archiver archiver ) "Error adding aggregated content for: " + fname + " to finalize archive creation. Reason: " + e.getMessage(), e ); } - finally - { - IOUtil.close( writer ); - } excludeOverride = true; archiver.addFile( f, name ); @@ -157,11 +151,9 @@ public boolean isSelected( @Nonnull final FileInfo fileInfo ) void readLines( final FileInfo fileInfo, final List lines ) throws IOException { - BufferedReader reader = null; - try + try ( BufferedReader reader = + new BufferedReader( new InputStreamReader( fileInfo.getContents(), getEncoding() ) ) ) { - reader = new BufferedReader( new InputStreamReader( fileInfo.getContents(), getEncoding() ) ); - for ( String line = reader.readLine(); line != null; line = reader.readLine() ) { if ( !lines.contains( line ) ) @@ -169,13 +161,6 @@ void readLines( final FileInfo fileInfo, final List lines ) lines.add( line ); } } - - reader.close(); - reader = null; - } - finally - { - IOUtil.close( reader ); } } diff --git a/src/main/java/org/apache/maven/plugins/assembly/filter/ComponentsXmlArchiverFileFilter.java b/src/main/java/org/apache/maven/plugins/assembly/filter/ComponentsXmlArchiverFileFilter.java index a5b33b0e6..813da5f34 100644 --- a/src/main/java/org/apache/maven/plugins/assembly/filter/ComponentsXmlArchiverFileFilter.java +++ b/src/main/java/org/apache/maven/plugins/assembly/filter/ComponentsXmlArchiverFileFilter.java @@ -27,7 +27,6 @@ import org.codehaus.plexus.archiver.UnArchiver; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.components.io.fileselectors.FileInfo; -import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.xml.Xpp3Dom; import org.codehaus.plexus.util.xml.Xpp3DomBuilder; import org.codehaus.plexus.util.xml.Xpp3DomWriter; @@ -107,10 +106,9 @@ private void addToArchive( final Archiver archiver ) final File f = File.createTempFile( "maven-assembly-plugin", "tmp" ); f.deleteOnExit(); - Writer fileWriter = null; - try + + try ( Writer fileWriter = WriterFactory.newXmlWriter( new FileOutputStream( f ) ) ) { - fileWriter = WriterFactory.newXmlWriter( new FileOutputStream( f ) ); final Xpp3Dom dom = new Xpp3Dom( "component-set" ); final Xpp3Dom componentDom = new Xpp3Dom( "components" ); dom.addChild( componentDom ); @@ -121,13 +119,6 @@ private void addToArchive( final Archiver archiver ) } Xpp3DomWriter.write( fileWriter, dom ); - - fileWriter.close(); - fileWriter = null; - } - finally - { - IOUtil.close( fileWriter ); } excludeOverride = true; @@ -192,13 +183,9 @@ public boolean isSelected( @Nonnull final FileInfo fileInfo ) if ( ComponentsXmlArchiverFileFilter.COMPONENTS_XML_PATH.equals( entry ) ) { - Reader reader = null; - try + try ( Reader reader = new BufferedReader( ReaderFactory.newXmlReader( fileInfo.getContents() ) ) ) { - reader = new BufferedReader( ReaderFactory.newXmlReader( fileInfo.getContents() ) ); addComponentsXml( reader ); - reader.close(); - reader = null; } catch ( final XmlPullParserException e ) { @@ -207,11 +194,6 @@ public boolean isSelected( @Nonnull final FileInfo fileInfo ) throw error; } - finally - { - IOUtil.close( reader ); - } - return false; } else diff --git a/src/main/java/org/apache/maven/plugins/assembly/filter/SimpleAggregatingDescriptorHandler.java b/src/main/java/org/apache/maven/plugins/assembly/filter/SimpleAggregatingDescriptorHandler.java index ff39122c4..2fa737305 100644 --- a/src/main/java/org/apache/maven/plugins/assembly/filter/SimpleAggregatingDescriptorHandler.java +++ b/src/main/java/org/apache/maven/plugins/assembly/filter/SimpleAggregatingDescriptorHandler.java @@ -32,6 +32,7 @@ import javax.annotation.Nonnull; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; @@ -103,44 +104,43 @@ public void finalizeArchiveCreation( final Archiver archiver ) private File writePropertiesFile() { File f; - - Writer writer = null; try { f = File.createTempFile( "maven-assembly-plugin", "tmp" ); f.deleteOnExit(); - writer = AssemblyFileUtils.isPropertyFile( f ) - ? new OutputStreamWriter( new FileOutputStream( f ), StandardCharsets.ISO_8859_1 ) - : new OutputStreamWriter( new FileOutputStream( f ) ); // Still platform encoding - - writer.write( commentChars + " Aggregated on " + new Date() + " from: " ); - - for ( final String filename : filenames ) + try ( Writer writer = getWriter( f ) ) { - writer.write( "\n" + commentChars + " " + filename ); - } - - writer.write( "\n\n" ); + writer.write( commentChars + " Aggregated on " + new Date() + " from: " ); - writer.write( aggregateWriter.toString() ); + for ( final String filename : filenames ) + { + writer.write( "\n" + commentChars + " " + filename ); + } - writer.close(); - writer = null; + writer.write( "\n\n" ); + writer.write( aggregateWriter.toString() ); + } } catch ( final IOException e ) { throw new ArchiverException( "Error adding aggregated properties to finalize archive creation. Reason: " + e.getMessage(), e ); } - finally - { - IOUtil.close( writer ); - } return f; } + private Writer getWriter( File f ) + throws FileNotFoundException + { + Writer writer; + writer = AssemblyFileUtils.isPropertyFile( f ) + ? new OutputStreamWriter( new FileOutputStream( f ), StandardCharsets.ISO_8859_1 ) + : new OutputStreamWriter( new FileOutputStream( f ) ); // Still platform encoding + return writer; + } + @Override public void finalizeArchiveExtraction( final UnArchiver unarchiver ) { diff --git a/src/main/java/org/apache/maven/plugins/assembly/io/DefaultAssemblyReader.java b/src/main/java/org/apache/maven/plugins/assembly/io/DefaultAssemblyReader.java index 2b695c79b..a638d3c13 100644 --- a/src/main/java/org/apache/maven/plugins/assembly/io/DefaultAssemblyReader.java +++ b/src/main/java/org/apache/maven/plugins/assembly/io/DefaultAssemblyReader.java @@ -19,7 +19,6 @@ * under the License. */ -import org.apache.commons.io.IOUtils; import org.apache.maven.plugins.assembly.AssemblerConfigurationSource; import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException; import org.apache.maven.plugins.assembly.interpolation.AssemblyExpressionEvaluator; @@ -210,13 +209,9 @@ private Assembly addAssemblyForDescriptorReference( final String ref, } } - Reader reader = null; - try + try ( Reader reader = ReaderFactory.newXmlReader( resourceAsStream ) ) { - reader = ReaderFactory.newXmlReader( resourceAsStream ); final Assembly assembly = readAssembly( reader, ref, null, configSource ); - reader.close(); - reader = null; assemblies.add( assembly ); return assembly; } @@ -224,10 +219,6 @@ private Assembly addAssemblyForDescriptorReference( final String ref, { throw new AssemblyReadException( "Problem with descriptor with ID '" + ref + "'", e ); } - finally - { - IOUtils.closeQuietly( reader ); - } } private Assembly addAssemblyFromDescriptorFile( final File descriptor, @@ -249,17 +240,11 @@ private Assembly addAssemblyFromDescriptorFile( final File descriptor, } } - Reader r = null; - try + try ( Reader r = ReaderFactory.newXmlReader( descriptor ) ) { - r = ReaderFactory.newXmlReader( descriptor ); - final Assembly assembly = readAssembly( r, descriptor.getAbsolutePath(), descriptor.getParentFile(), configSource ); - r.close(); - r = null; - assemblies.add( assembly ); return assembly; @@ -268,10 +253,6 @@ private Assembly addAssemblyFromDescriptorFile( final File descriptor, { throw new AssemblyReadException( "Error reading assembly descriptor: " + descriptor, e ); } - finally - { - IOUtil.close( r ); - } } private Assembly addAssemblyFromDescriptor( final String spec, final Locator locator, @@ -296,11 +277,9 @@ private Assembly addAssemblyFromDescriptor( final String spec, final Locator loc } } - Reader r = null; - try + + try ( Reader r = ReaderFactory.newXmlReader( location.getInputStream() ) ) { - r = ReaderFactory.newXmlReader( location.getInputStream() ); - File dir = null; if ( location.getFile() != null ) { @@ -309,9 +288,6 @@ private Assembly addAssemblyFromDescriptor( final String spec, final Locator loc final Assembly assembly = readAssembly( r, spec, dir, configSource ); - r.close(); - r = null; - assemblies.add( assembly ); return assembly; @@ -320,11 +296,6 @@ private Assembly addAssemblyFromDescriptor( final String spec, final Locator loc { throw new AssemblyReadException( "Error reading assembly descriptor: " + spec, e ); } - finally - { - IOUtil.close( r ); - } - } public Assembly readAssembly( Reader reader, final String locationDescription, final File assemblyDir, @@ -443,10 +414,8 @@ protected void mergeComponentsWithMainAssembly( final Assembly assembly, final F } Component component = null; - Reader reader = null; - try + try ( Reader reader = new InputStreamReader( resolvedLocation.getInputStream() ) ) { - reader = new InputStreamReader( resolvedLocation.getInputStream() ); component = new ComponentXpp3Reader( transformer ).read( reader ); } catch ( final IOException | XmlPullParserException e ) @@ -454,10 +423,6 @@ protected void mergeComponentsWithMainAssembly( final Assembly assembly, final F throw new AssemblyReadException( "Error reading component descriptor: " + location + " (resolved to: " + resolvedLocation.getSpecification() + ")", e ); } - finally - { - IOUtil.close( reader ); - } mergeComponentWithAssembly( component, assembly ); } diff --git a/src/main/java/org/apache/maven/plugins/assembly/utils/LineEndingsUtils.java b/src/main/java/org/apache/maven/plugins/assembly/utils/LineEndingsUtils.java index 3ae2eb1f2..e69f4e8c9 100644 --- a/src/main/java/org/apache/maven/plugins/assembly/utils/LineEndingsUtils.java +++ b/src/main/java/org/apache/maven/plugins/assembly/utils/LineEndingsUtils.java @@ -19,12 +19,6 @@ * under the License. */ -import org.apache.maven.plugins.assembly.format.AssemblyFormattingException; -import org.codehaus.plexus.util.IOUtil; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; @@ -36,6 +30,11 @@ import java.io.OutputStreamWriter; import java.io.RandomAccessFile; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import org.apache.maven.plugins.assembly.format.AssemblyFormattingException; + /** * Line Ending class which contains convenience methods to change line endings. */ @@ -62,7 +61,7 @@ private LineEndingsUtils() * @throws IOException . */ public static void convertLineEndings( @Nonnull File source, @Nonnull File dest, LineEndings lineEndings, - Boolean atEndOfFile, String encoding ) + final Boolean atEndOfFile, String encoding ) throws IOException { // MASSEMBLY-637, MASSEMBLY-96 @@ -72,12 +71,10 @@ public static void convertLineEndings( @Nonnull File source, @Nonnull File dest, String eofChars = ""; if ( atEndOfFile == null ) { - RandomAccessFile raf = null; - try + if ( source.length() >= 1 ) { - if ( source.length() >= 1 ) + try ( RandomAccessFile raf = new RandomAccessFile( source, "r" ) ) { - raf = new RandomAccessFile( source, "r" ); raf.seek( source.length() - 1 ); byte last = raf.readByte(); if ( last == '\n' ) @@ -86,46 +83,16 @@ public static void convertLineEndings( @Nonnull File source, @Nonnull File dest, } } } - finally - { - if ( raf != null ) - { - try - { - raf.close(); - } - catch ( IOException ex ) - { - // ignore - } - } - } } else if ( atEndOfFile ) { eofChars = lineEndings.getLineEndingCharacters(); } - BufferedReader in = null; - BufferedWriter out = null; - try + try ( BufferedReader in = getBufferedReader( source, encoding ); + BufferedWriter out = getBufferedWriter( dest, encoding ) ) { - if ( encoding == null ) - { - // platform encoding - in = new BufferedReader( new InputStreamReader( new FileInputStream( source ) ) ); - out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( dest ) ) ); - } - else - { - // MASSEMBLY-371 - in = new BufferedReader( new InputStreamReader( new FileInputStream( source ), encoding ) ); - out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( dest ), encoding ) ); - } - - String line; - - line = in.readLine(); + String line = in.readLine(); while ( line != null ) { out.write( line ); @@ -139,16 +106,34 @@ else if ( atEndOfFile ) out.write( eofChars ); } } + } + } - out.close(); - out = null; - in.close(); - in = null; + private static BufferedReader getBufferedReader( File source, String encoding ) throws IOException + { + if ( encoding == null ) + { + // platform encoding + return new BufferedReader( new InputStreamReader( new FileInputStream( source ) ) ); } - finally + else { - IOUtil.close( in ); - IOUtil.close( out ); + // MASSEMBLY-371 + return new BufferedReader( new InputStreamReader( new FileInputStream( source ), encoding ) ); + } + } + + private static BufferedWriter getBufferedWriter( File dest, String encoding ) throws IOException + { + if ( encoding == null ) + { + // platform encoding + return new BufferedWriter( new OutputStreamWriter( new FileOutputStream( dest ) ) ); + } + else + { + // MASSEMBLY-371 + return new BufferedWriter( new OutputStreamWriter( new FileOutputStream( dest ), encoding ) ); } } @@ -200,12 +185,10 @@ public static LineEndings getLineEnding( @Nullable String lineEnding ) public static String getLineEndingCharacters( @Nullable String lineEnding ) throws AssemblyFormattingException { - String value = lineEnding; if ( lineEnding != null ) { - try { value = LineEndings.valueOf( lineEnding ).getLineEndingCharacters();