Skip to content

Commit

Permalink
replace "UTF-8" with StandardCharsets.UTF_8 constant use try-with-res…
Browse files Browse the repository at this point in the history
…ources from java 7 where possible remove redundant casts
  • Loading branch information
turbanoff authored and rfscholte committed Oct 31, 2018
1 parent ad61743 commit 0562c51
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 223 deletions.
51 changes: 13 additions & 38 deletions src/main/java/org/apache/maven/plugins/shade/DefaultShader.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
Expand Down Expand Up @@ -159,9 +159,7 @@ private void shadeJars( ShadeRequest shadeRequest, Set<String> resources, List<R

List<Filter> jarFilters = getFilters( jar, shadeRequest.getFilters() );

JarFile jarFile = newJarFile( jar );

try
try ( JarFile jarFile = newJarFile( jar ) )
{

for ( Enumeration<JarEntry> j = jarFile.entries(); j.hasMoreElements(); )
Expand All @@ -183,7 +181,7 @@ private void shadeJars( ShadeRequest shadeRequest, Set<String> resources, List<R
// later
continue;
}

if ( "module-info.class".equals( name ) )
{
getLogger().warn( "Discovered module-info.class. "
Expand All @@ -204,10 +202,6 @@ private void shadeJars( ShadeRequest shadeRequest, Set<String> resources, List<R
}

}
finally
{
jarFile.close();
}
}
}

Expand All @@ -217,10 +211,8 @@ private void shadeSingleJar( ShadeRequest shadeRequest, Set<String> resources,
JarEntry entry, String name )
throws IOException, MojoExecutionException
{
InputStream in = null;
try
try ( InputStream in = jarFile.getInputStream( entry ) )
{
in = jarFile.getInputStream( entry );
String mappedName = remapper.map( name );

int idx = mappedName.lastIndexOf( '/' );
Expand Down Expand Up @@ -262,13 +254,6 @@ else if ( shadeRequest.isShadeSourcesContent() && name.endsWith( ".java" ) )
addResource( resources, jos, mappedName, entry.getTime(), in );
}
}

in.close();
in = null;
}
finally
{
IOUtil.close( in );
}
}

Expand All @@ -281,8 +266,7 @@ private void goThroughAllJarEntriesForManifestTransformer( ShadeRequest shadeReq
{
for ( File jar : shadeRequest.getJars() )
{
JarFile jarFile = newJarFile( jar );
try
try ( JarFile jarFile = newJarFile( jar ) )
{
for ( Enumeration<JarEntry> en = jarFile.entries(); en.hasMoreElements(); )
{
Expand All @@ -291,24 +275,15 @@ private void goThroughAllJarEntriesForManifestTransformer( ShadeRequest shadeReq
if ( manifestTransformer.canTransformResource( resource ) )
{
resources.add( resource );
InputStream inputStream = jarFile.getInputStream( entry );
try
try ( InputStream inputStream = jarFile.getInputStream( entry ) )
{
manifestTransformer.processResource( resource, inputStream,
shadeRequest.getRelocators() );
}
finally
{
inputStream.close();
shadeRequest.getRelocators() );
}
break;
}
}
}
finally
{
jarFile.close();
}
}
if ( manifestTransformer.hasTransformedResource() )
{
Expand All @@ -332,14 +307,14 @@ private void logSummaryOfDuplicates( Multimap<Collection<File>, String> overlapp
{
for ( Collection<File> jarz : overlapping.keySet() )
{
List<String> jarzS = new LinkedList<>();
List<String> jarzS = new ArrayList<>();

for ( File jjar : jarz )
{
jarzS.add( jjar.getName() );
}

List<String> classes = new LinkedList<>();
List<String> classes = new ArrayList<>();

for ( String clazz : overlapping.get( jarz ) )
{
Expand Down Expand Up @@ -483,7 +458,7 @@ public void visitSource( final String source, final String debug )
// Now we put it back on so the class file is written out with the right extension.
jos.putNextEntry( new JarEntry( mappedName + ".class" ) );

IOUtil.copy( renamedClass, jos );
jos.write( renamedClass );
}
catch ( ZipException e )
{
Expand Down Expand Up @@ -532,15 +507,15 @@ private void addJavaSource( Set<String> resources, JarOutputStream jos, String n
{
jos.putNextEntry( new JarEntry( name ) );

String sourceContent = IOUtil.toString( new InputStreamReader( is, "UTF-8" ) );
String sourceContent = IOUtil.toString( new InputStreamReader( is, StandardCharsets.UTF_8 ) );

for ( Relocator relocator : relocators )
{
sourceContent = relocator.applyToSourceContent( sourceContent );
}

final Writer writer = new OutputStreamWriter( jos, "UTF-8" );
IOUtil.copy( sourceContent, writer );
final Writer writer = new OutputStreamWriter( jos, StandardCharsets.UTF_8 );
writer.write( sourceContent );
writer.flush();

resources.add( name );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,17 @@ public MinijarFilter( MavenProject project, Log log, List<SimpleFilter> simpleFi
private ClazzpathUnit addDependencyToClasspath( Clazzpath cp, Artifact dependency )
throws IOException
{
InputStream is = null;
ClazzpathUnit clazzpathUnit = null;
try
try ( InputStream is = new FileInputStream( dependency.getFile() ) )
{
is = new FileInputStream( dependency.getFile() );
clazzpathUnit = cp.addClazzpathUnit( is, dependency.toString() );
is.close();
is = null;
}
catch ( ZipException e )
{
log.warn( dependency.getFile()
+ " could not be unpacked/read for minimization; dependency is probably malformed." );
IOException ioe = new IOException( "Dependency " + dependency.toString() + " in file "
+ dependency.getFile() + " could not be unpacked. File is probably corrupt" );
ioe.initCause( e );
+ dependency.getFile() + " could not be unpacked. File is probably corrupt", e );
throw ioe;
}
catch ( ArrayIndexOutOfBoundsException | IllegalArgumentException e )
Expand All @@ -141,10 +136,6 @@ private ClazzpathUnit addDependencyToClasspath( Clazzpath cp, Artifact dependenc
log.warn( dependency.toString()
+ " could not be analyzed for minimization; dependency is probably malformed." );
}
finally
{
IOUtil.close( is );
}

return clazzpathUnit;
}
Expand Down
20 changes: 5 additions & 15 deletions src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -695,22 +695,12 @@ private void replaceFile( File oldFile, File newFile )
private void copyFiles( File source, File target )
throws IOException
{
InputStream in = null;
OutputStream out = null;
try
{
in = new FileInputStream( source );
out = new FileOutputStream( target );
IOUtil.copy( in, out );
out.close();
out = null;
in.close();
in = null;
}
finally
try ( InputStream in = new FileInputStream( source ) )
{
IOUtil.close( in );
IOUtil.close( out );
try ( OutputStream out = new FileOutputStream( target ) )
{
IOUtil.copy( in, out );
}
}
}

Expand Down
Loading

0 comments on commit 0562c51

Please sign in to comment.