Skip to content

Commit

Permalink
use try with resources to close streams and fix warnings (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
elharo committed Apr 12, 2020
1 parent c849d73 commit 7a7ba64
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ public void processResource( String resource, InputStream is, final List<Relocat
serviceEntries.put( resource, out );
}

final ServiceStream fout = out;

final String content = IOUtils.toString( is );
StringReader reader = new StringReader( content );
BufferedReader lineReader = new BufferedReader( reader );
Expand All @@ -87,7 +85,7 @@ public void processResource( String resource, InputStream is, final List<Relocat
relContent = relocator.applyToSourceContent( relContent );
}
}
fout.append( relContent + "\n" );
out.append( relContent + "\n" );
}

if ( this.relocators == null )
Expand Down
36 changes: 19 additions & 17 deletions src/test/java/org/apache/maven/plugins/shade/DefaultShaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,11 @@ public void testShaderWithStaticInitializedClass()

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()
Expand Down Expand Up @@ -211,25 +212,26 @@ public void testShaderWithRelocatedClassname()

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 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,15 @@ public void testShaderWithExclusions()

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 ) );
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,21 @@ public void relocatedClasses() throws Exception {
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();
}
}
Expand All @@ -110,28 +103,20 @@ public void concatanationAppliedMultipleTimes() throws Exception {
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();
}
}
Expand Down Expand Up @@ -161,16 +146,14 @@ public void concatenation() throws Exception {
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");
Expand All @@ -189,14 +172,9 @@ else if ("borg.foo.Service".equals( name ))
}
assertTrue( h1 && h2 );
} finally {
IOUtils.closeQuietly( entryStream );
jarFile.close();
}
} finally {
if (jos != null)
{
IOUtils.closeQuietly( jos );
}
tempJar.delete();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

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;
Expand Down

0 comments on commit 7a7ba64

Please sign in to comment.