Skip to content

Commit 7a7ba64

Browse files
authored
use try with resources to close streams and fix warnings (#43)
1 parent c849d73 commit 7a7ba64

File tree

5 files changed

+38
-60
lines changed

5 files changed

+38
-60
lines changed

src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ public void processResource( String resource, InputStream is, final List<Relocat
7171
serviceEntries.put( resource, out );
7272
}
7373

74-
final ServiceStream fout = out;
75-
7674
final String content = IOUtils.toString( is );
7775
StringReader reader = new StringReader( content );
7876
BufferedReader lineReader = new BufferedReader( reader );
@@ -87,7 +85,7 @@ public void processResource( String resource, InputStream is, final List<Relocat
8785
relContent = relocator.applyToSourceContent( relContent );
8886
}
8987
}
90-
fout.append( relContent + "\n" );
88+
out.append( relContent + "\n" );
9189
}
9290

9391
if ( this.relocators == null )

src/test/java/org/apache/maven/plugins/shade/DefaultShaderTest.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,11 @@ public void testShaderWithStaticInitializedClass()
157157

158158
s.shade( shadeRequest );
159159

160-
URLClassLoader cl = new URLClassLoader( new URL[] { file.toURI().toURL() } );
161-
Class<?> c = cl.loadClass( "hidden.org.apache.maven.plugins.shade.Lib" );
162-
Object o = c.newInstance();
163-
assertEquals( "foo.bar/baz", c.getDeclaredField( "CONSTANT" ).get( o ) );
160+
try ( URLClassLoader cl = new URLClassLoader( new URL[] { file.toURI().toURL() } ) ) {
161+
Class<?> c = cl.loadClass( "hidden.org.apache.maven.plugins.shade.Lib" );
162+
Object o = c.newInstance();
163+
assertEquals( "foo.bar/baz", c.getDeclaredField( "CONSTANT" ).get( o ) );
164+
}
164165
}
165166

166167
public void testShaderWithCustomShadedPattern()
@@ -211,25 +212,26 @@ public void testShaderWithRelocatedClassname()
211212

212213
s.shade( shadeRequest );
213214

214-
URLClassLoader cl = new URLClassLoader( new URL[] { file.toURI().toURL() } );
215-
Class<?> c = cl.loadClass( "_plexus.util.__StringUtils" );
216-
// first, ensure it works:
217-
Object o = c.newInstance();
218-
assertEquals( "", c.getMethod( "clean", String.class ).invoke( o, (String) null ) );
219-
220-
// now, check that its source file was rewritten:
221-
final String[] source = { null };
222-
final ClassReader classReader = new ClassReader( cl.getResourceAsStream( "_plexus/util/__StringUtils.class" ) );
223-
classReader.accept( new ClassVisitor( Opcodes.ASM4 )
224-
{
215+
try ( URLClassLoader cl = new URLClassLoader( new URL[] { file.toURI().toURL() } ) ) {
216+
Class<?> c = cl.loadClass( "_plexus.util.__StringUtils" );
217+
// first, ensure it works:
218+
Object o = c.newInstance();
219+
assertEquals( "", c.getMethod( "clean", String.class ).invoke( o, (String) null ) );
220+
221+
// now, check that its source file was rewritten:
222+
final String[] source = { null };
223+
final ClassReader classReader = new ClassReader( cl.getResourceAsStream( "_plexus/util/__StringUtils.class" ) );
224+
classReader.accept( new ClassVisitor( Opcodes.ASM4 )
225+
{
225226
@Override
226227
public void visitSource( String arg0, String arg1 )
227228
{
228229
super.visitSource( arg0, arg1 );
229230
source[0] = arg0;
230231
}
231-
}, ClassReader.SKIP_CODE );
232-
assertEquals( "__StringUtils.java", source[0] );
232+
}, ClassReader.SKIP_CODE );
233+
assertEquals( "__StringUtils.java", source[0] );
234+
}
233235
}
234236

235237
private void shaderWithPattern( String shadedPattern, File jar, String[] excludes )

src/test/java/org/apache/maven/plugins/shade/mojo/ShadeMojoTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,15 @@ public void testShaderWithExclusions()
9999

100100
s.shade( shadeRequest );
101101

102-
ClassLoader cl = new URLClassLoader( new URL[]{ jarFile.toURI().toURL() } );
103-
Class<?> c = cl.loadClass( "org.apache.maven.plugins.shade.Lib" );
104-
105-
Field field = c.getDeclaredField( "CLASS_REALM_PACKAGE_IMPORT" );
106-
assertEquals( "org.codehaus.plexus.util.xml.pull", field.get( null ) );
107-
108-
Method method = c.getDeclaredMethod( "getClassRealmPackageImport" );
109-
assertEquals( "org.codehaus.plexus.util.xml.pull", method.invoke( null ) );
102+
try ( URLClassLoader cl = new URLClassLoader( new URL[]{ jarFile.toURI().toURL() } ) ) {
103+
Class<?> c = cl.loadClass( "org.apache.maven.plugins.shade.Lib" );
104+
105+
Field field = c.getDeclaredField( "CLASS_REALM_PACKAGE_IMPORT" );
106+
assertEquals( "org.codehaus.plexus.util.xml.pull", field.get( null ) );
107+
108+
Method method = c.getDeclaredMethod( "getClassRealmPackageImport" );
109+
assertEquals( "org.codehaus.plexus.util.xml.pull", method.invoke( null ) );
110+
}
110111
}
111112

112113
/**

src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -66,28 +66,21 @@ public void relocatedClasses() throws Exception {
6666
File tempJar = File.createTempFile("shade.", ".jar");
6767
tempJar.deleteOnExit();
6868
FileOutputStream fos = new FileOutputStream( tempJar );
69-
JarOutputStream jos = new JarOutputStream( fos );
70-
try {
69+
try ( JarOutputStream jos = new JarOutputStream( fos ) ) {
7170
xformer.modifyOutputStream( jos );
7271
jos.close();
73-
jos = null;
72+
7473
JarFile jarFile = new JarFile( tempJar );
7574
JarEntry jarEntry = jarFile.getJarEntry( contentResourceShaded );
7675
assertNotNull( jarEntry );
77-
InputStream entryStream = jarFile.getInputStream( jarEntry );
78-
try {
76+
try ( InputStream entryStream = jarFile.getInputStream( jarEntry ) ) {
7977
String xformedContent = IOUtils.toString( entryStream, "utf-8" );
8078
assertEquals( "borg.foo.Service" + System.getProperty( "line.separator" )
8179
+ "org.foo.exclude.OtherService" + System.getProperty( "line.separator" ), xformedContent );
8280
} finally {
83-
IOUtils.closeQuietly( entryStream );
8481
jarFile.close();
8582
}
8683
} finally {
87-
if (jos != null)
88-
{
89-
IOUtils.closeQuietly( jos );
90-
}
9184
tempJar.delete();
9285
}
9386
}
@@ -110,28 +103,20 @@ public void concatanationAppliedMultipleTimes() throws Exception {
110103
File tempJar = File.createTempFile("shade.", ".jar");
111104
tempJar.deleteOnExit();
112105
FileOutputStream fos = new FileOutputStream( tempJar );
113-
JarOutputStream jos = new JarOutputStream( fos );
114-
try {
106+
try ( JarOutputStream jos = new JarOutputStream( fos ) ) {
115107
xformer.modifyOutputStream( jos );
116108
jos.close();
117-
jos = null;
109+
118110
JarFile jarFile = new JarFile( tempJar );
119111
JarEntry jarEntry = jarFile.getJarEntry( contentResource );
120112
assertNotNull( jarEntry );
121-
InputStream entryStream = jarFile.getInputStream( jarEntry );
122-
try {
113+
try ( InputStream entryStream = jarFile.getInputStream( jarEntry ) ) {
123114
String xformedContent = IOUtils.toString(entryStream, "utf-8");
124115
assertEquals( "org.eclipse1234.osgi.launch.EquinoxFactory" + System.getProperty( "line.separator" ), xformedContent );
125-
126116
} finally {
127-
IOUtils.closeQuietly( entryStream );
128117
jarFile.close();
129118
}
130119
} finally {
131-
if (jos != null)
132-
{
133-
IOUtils.closeQuietly( jos );
134-
}
135120
tempJar.delete();
136121
}
137122
}
@@ -161,16 +146,14 @@ public void concatenation() throws Exception {
161146
File tempJar = File.createTempFile("shade.", ".jar");
162147
tempJar.deleteOnExit();
163148
FileOutputStream fos = new FileOutputStream( tempJar );
164-
JarOutputStream jos = new JarOutputStream( fos );
165-
try {
149+
try ( JarOutputStream jos = new JarOutputStream( fos ) ) {
166150
xformer.modifyOutputStream( jos );
167151
jos.close();
168-
jos = null;
152+
169153
JarFile jarFile = new JarFile( tempJar );
170154
JarEntry jarEntry = jarFile.getJarEntry( contentResource );
171155
assertNotNull( jarEntry );
172-
InputStream entryStream = jarFile.getInputStream( jarEntry );
173-
try {
156+
try ( InputStream entryStream = jarFile.getInputStream( jarEntry ) ) {
174157
String xformedContent = IOUtils.toString(entryStream, "utf-8");
175158
// must be two lines, with our two classes.
176159
String[] classes = xformedContent.split("\r?\n");
@@ -189,14 +172,9 @@ else if ("borg.foo.Service".equals( name ))
189172
}
190173
assertTrue( h1 && h2 );
191174
} finally {
192-
IOUtils.closeQuietly( entryStream );
193175
jarFile.close();
194176
}
195177
} finally {
196-
if (jos != null)
197-
{
198-
IOUtils.closeQuietly( jos );
199-
}
200178
tempJar.delete();
201179
}
202180
}

src/test/java/org/apache/maven/plugins/shade/resource/rule/TransformerTesterRule.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import static java.lang.annotation.ElementType.METHOD;
2323
import static java.lang.annotation.RetentionPolicy.RUNTIME;
24-
import static org.junit.Assert.assertEquals;
2524
import static org.junit.Assert.assertNotNull;
2625
import static org.junit.Assert.assertTrue;
2726
import static org.junit.Assert.fail;

0 commit comments

Comments
 (0)