Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use try with resources to close streams and fix warnings #43

Merged
merged 1 commit into from
Apr 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,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 );
LineReader lineReader = new LineReader( reader );
Expand All @@ -89,7 +87,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 @@ -65,28 +65,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 @@ -109,28 +102,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 @@ -160,16 +145,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 @@ -188,14 +171,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