maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elh...@apache.org
Subject [maven-shade-plugin] branch master updated: use try with resources to close streams and fix warnings (#43)
Date Sun, 12 Apr 2020 11:25:14 GMT
This is an automated email from the ASF dual-hosted git repository.

elharo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-shade-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 7a7ba64  use try with resources to close streams and fix warnings (#43)
7a7ba64 is described below

commit 7a7ba64e24d3ad452282bf56e431093d334e6a6f
Author: Elliotte Rusty Harold <elharo@users.noreply.github.com>
AuthorDate: Sun Apr 12 07:25:05 2020 -0400

    use try with resources to close streams and fix warnings (#43)
---
 .../resource/ServicesResourceTransformer.java      |  4 +--
 .../maven/plugins/shade/DefaultShaderTest.java     | 36 ++++++++++---------
 .../maven/plugins/shade/mojo/ShadeMojoTest.java    | 17 ++++-----
 .../resource/ServiceResourceTransformerTest.java   | 40 +++++-----------------
 .../shade/resource/rule/TransformerTesterRule.java |  1 -
 5 files changed, 38 insertions(+), 60 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java
b/src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java
index bbb55fd..7e5d3a6 100644
--- a/src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java
+++ b/src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java
@@ -71,8 +71,6 @@ public class ServicesResourceTransformer
             serviceEntries.put( resource, out );
         }
 
-        final ServiceStream fout = out;
-
         final String content = IOUtils.toString( is );
         StringReader reader = new StringReader( content );
         BufferedReader lineReader = new BufferedReader( reader );
@@ -87,7 +85,7 @@ public class ServicesResourceTransformer
                     relContent = relocator.applyToSourceContent( relContent );
                 }
             }
-            fout.append( relContent + "\n" );
+            out.append( relContent + "\n" );
         }
 
         if ( this.relocators == null )
diff --git a/src/test/java/org/apache/maven/plugins/shade/DefaultShaderTest.java b/src/test/java/org/apache/maven/plugins/shade/DefaultShaderTest.java
index e736d6a..efeffe5 100644
--- a/src/test/java/org/apache/maven/plugins/shade/DefaultShaderTest.java
+++ b/src/test/java/org/apache/maven/plugins/shade/DefaultShaderTest.java
@@ -157,10 +157,11 @@ public class DefaultShaderTest
 
         s.shade( shadeRequest );
 
-        URLClassLoader cl = new URLClassLoader( new URL[] { file.toURI().toURL() } );
-        Class<?> c = cl.loadClass( "hidden.org.apache.maven.plugins.shade.Lib" );
-        Object o = c.newInstance();
-        assertEquals( "foo.bar/baz", c.getDeclaredField( "CONSTANT" ).get( o ) );
+        try ( URLClassLoader cl = new URLClassLoader( new URL[] { file.toURI().toURL() }
) ) {
+          Class<?> c = cl.loadClass( "hidden.org.apache.maven.plugins.shade.Lib" );
+          Object o = c.newInstance();
+          assertEquals( "foo.bar/baz", c.getDeclaredField( "CONSTANT" ).get( o ) );
+        }
     }
 
     public void testShaderWithCustomShadedPattern()
@@ -211,25 +212,26 @@ public class DefaultShaderTest
 
         s.shade( shadeRequest );
 
-        URLClassLoader cl = new URLClassLoader( new URL[] { file.toURI().toURL() } );
-        Class<?> c = cl.loadClass( "_plexus.util.__StringUtils" );
-        // first, ensure it works:
-        Object o = c.newInstance();
-        assertEquals( "", c.getMethod( "clean", String.class ).invoke( o, (String) null )
);
-
-        // now, check that its source file was rewritten:
-        final String[] source = { null };
-        final ClassReader classReader = new ClassReader( cl.getResourceAsStream( "_plexus/util/__StringUtils.class"
) );
-        classReader.accept( new ClassVisitor( Opcodes.ASM4 )
-        {
+        try ( URLClassLoader cl = new URLClassLoader( new URL[] { file.toURI().toURL() }
) ) {
+          Class<?> c = cl.loadClass( "_plexus.util.__StringUtils" );
+          // first, ensure it works:
+          Object o = c.newInstance();
+          assertEquals( "", c.getMethod( "clean", String.class ).invoke( o, (String) null
) );
+
+          // now, check that its source file was rewritten:
+          final String[] source = { null };
+          final ClassReader classReader = new ClassReader( cl.getResourceAsStream( "_plexus/util/__StringUtils.class"
) );
+          classReader.accept( new ClassVisitor( Opcodes.ASM4 )
+          {
             @Override
             public void visitSource( String arg0, String arg1 )
             {
                 super.visitSource( arg0, arg1 );
                 source[0] = arg0;
             }
-        }, ClassReader.SKIP_CODE );
-        assertEquals( "__StringUtils.java", source[0] );
+          }, ClassReader.SKIP_CODE );
+          assertEquals( "__StringUtils.java", source[0] );
+        }
     }
 
     private void shaderWithPattern( String shadedPattern, File jar, String[] excludes )
diff --git a/src/test/java/org/apache/maven/plugins/shade/mojo/ShadeMojoTest.java b/src/test/java/org/apache/maven/plugins/shade/mojo/ShadeMojoTest.java
index 208bac5..d5020ac 100644
--- a/src/test/java/org/apache/maven/plugins/shade/mojo/ShadeMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/shade/mojo/ShadeMojoTest.java
@@ -99,14 +99,15 @@ public class ShadeMojoTest
 
         s.shade( shadeRequest );
 
-        ClassLoader cl = new URLClassLoader( new URL[]{ jarFile.toURI().toURL() } );
-        Class<?> c = cl.loadClass( "org.apache.maven.plugins.shade.Lib" );
-
-        Field field = c.getDeclaredField( "CLASS_REALM_PACKAGE_IMPORT" );
-        assertEquals( "org.codehaus.plexus.util.xml.pull", field.get( null ) );
-
-        Method method = c.getDeclaredMethod( "getClassRealmPackageImport" );
-        assertEquals( "org.codehaus.plexus.util.xml.pull", method.invoke( null ) );
+        try ( URLClassLoader cl = new URLClassLoader( new URL[]{ jarFile.toURI().toURL()
} ) ) {
+            Class<?> c = cl.loadClass( "org.apache.maven.plugins.shade.Lib" );
+    
+            Field field = c.getDeclaredField( "CLASS_REALM_PACKAGE_IMPORT" );
+            assertEquals( "org.codehaus.plexus.util.xml.pull", field.get( null ) );
+    
+            Method method = c.getDeclaredMethod( "getClassRealmPackageImport" );
+            assertEquals( "org.codehaus.plexus.util.xml.pull", method.invoke( null ) );
+        }
     }
 
     /**
diff --git a/src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java
b/src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java
index 3bef2f9..b87252c 100644
--- a/src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java
+++ b/src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java
@@ -66,28 +66,21 @@ public class ServiceResourceTransformerTest {
         File tempJar = File.createTempFile("shade.", ".jar");
         tempJar.deleteOnExit();
         FileOutputStream fos = new FileOutputStream( tempJar );
-        JarOutputStream jos = new JarOutputStream( fos );
-        try {
+        try ( JarOutputStream jos = new JarOutputStream( fos ) ) {
             xformer.modifyOutputStream( jos );
             jos.close();
-            jos = null;
+            
             JarFile jarFile = new JarFile( tempJar );
             JarEntry jarEntry = jarFile.getJarEntry( contentResourceShaded );
             assertNotNull( jarEntry );
-            InputStream entryStream = jarFile.getInputStream( jarEntry );
-            try {
+            try ( InputStream entryStream = jarFile.getInputStream( jarEntry ) ) {
                 String xformedContent = IOUtils.toString( entryStream, "utf-8" );
                 assertEquals( "borg.foo.Service" + System.getProperty( "line.separator" )
                     + "org.foo.exclude.OtherService" + System.getProperty( "line.separator"
), xformedContent );
             } finally {
-                IOUtils.closeQuietly( entryStream );
                 jarFile.close();
             }
         } finally {
-            if (jos != null)
-            {
-                IOUtils.closeQuietly( jos );
-            }
             tempJar.delete();
         }
     }
@@ -110,28 +103,20 @@ public class ServiceResourceTransformerTest {
         File tempJar = File.createTempFile("shade.", ".jar");
         tempJar.deleteOnExit();
         FileOutputStream fos = new FileOutputStream( tempJar );
-        JarOutputStream jos = new JarOutputStream( fos );
-        try {
+        try ( JarOutputStream jos = new JarOutputStream( fos ) ) {
             xformer.modifyOutputStream( jos );
             jos.close();
-            jos = null;
+            
             JarFile jarFile = new JarFile( tempJar );
             JarEntry jarEntry = jarFile.getJarEntry( contentResource );
             assertNotNull( jarEntry );
-            InputStream entryStream = jarFile.getInputStream( jarEntry );
-            try {
+            try ( InputStream entryStream = jarFile.getInputStream( jarEntry ) ) {
                 String xformedContent = IOUtils.toString(entryStream, "utf-8");
                 assertEquals( "org.eclipse1234.osgi.launch.EquinoxFactory" + System.getProperty(
"line.separator" ), xformedContent );
-                
             } finally {
-                IOUtils.closeQuietly( entryStream );
                 jarFile.close();
             }
         } finally {
-            if (jos != null)
-            {
-                IOUtils.closeQuietly( jos );
-            }
             tempJar.delete();
         }
     }
@@ -161,16 +146,14 @@ public class ServiceResourceTransformerTest {
         File tempJar = File.createTempFile("shade.", ".jar");
         tempJar.deleteOnExit();
         FileOutputStream fos = new FileOutputStream( tempJar );
-        JarOutputStream jos = new JarOutputStream( fos );
-        try {
+        try ( JarOutputStream jos = new JarOutputStream( fos ) ) {
             xformer.modifyOutputStream( jos );
             jos.close();
-            jos = null;
+
             JarFile jarFile = new JarFile( tempJar );
             JarEntry jarEntry = jarFile.getJarEntry( contentResource );
             assertNotNull( jarEntry );
-            InputStream entryStream = jarFile.getInputStream( jarEntry );
-            try {
+            try ( InputStream entryStream = jarFile.getInputStream( jarEntry ) ) {
                 String xformedContent = IOUtils.toString(entryStream, "utf-8");
                 // must be two lines, with our two classes.
                 String[] classes = xformedContent.split("\r?\n");
@@ -189,14 +172,9 @@ public class ServiceResourceTransformerTest {
                 }
                 assertTrue( h1 && h2 );
             } finally {
-                IOUtils.closeQuietly( entryStream );
                 jarFile.close();
             }
         } finally {
-            if (jos != null)
-            {
-                IOUtils.closeQuietly( jos );
-            }
             tempJar.delete();
         }
     }
diff --git a/src/test/java/org/apache/maven/plugins/shade/resource/rule/TransformerTesterRule.java
b/src/test/java/org/apache/maven/plugins/shade/resource/rule/TransformerTesterRule.java
index f4f7652..1eaf2b1 100644
--- a/src/test/java/org/apache/maven/plugins/shade/resource/rule/TransformerTesterRule.java
+++ b/src/test/java/org/apache/maven/plugins/shade/resource/rule/TransformerTesterRule.java
@@ -21,7 +21,6 @@ package org.apache.maven.plugins.shade.resource.rule;
 
 import static java.lang.annotation.ElementType.METHOD;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
-import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;


Mime
View raw message