Polish "Use try-with-resources to close resources automatically"

- Apply code formatting
- Use try-with-resources in many other places that were missed in the
  pull request

Closes gh-8045
This commit is contained in:
Andy Wilkinson
2017-05-23 17:24:01 +01:00
parent 3e797c326a
commit d5438c299c
78 changed files with 284 additions and 703 deletions

View File

@@ -187,8 +187,7 @@ abstract class ArchiveCommand extends OptionParsingCommand {
List<MatchedResource> classpathEntries, List<URL> dependencies)
throws FileNotFoundException, IOException, URISyntaxException {
final List<Library> libraries;
JarWriter writer = new JarWriter(file);
try {
try (JarWriter writer = new JarWriter(file)) {
addManifest(writer, compiledClasses);
addCliClasses(writer);
for (Class<?> compiledClass : compiledClasses) {
@@ -196,9 +195,6 @@ abstract class ArchiveCommand extends OptionParsingCommand {
}
libraries = addClasspathEntries(writer, classpathEntries);
}
finally {
writer.close();
}
libraries.addAll(createLibraries(dependencies));
Repackager repackager = new Repackager(file);
repackager.setMainClass(PackagedSpringApplicationLauncher.class.getName());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,7 +31,6 @@ import org.springframework.util.StreamUtils;
* Helper class used to generate the project.
*
* @author Stephane Nicoll
* @since 1.2.0
*/
class ProjectGenerator {
@@ -106,17 +105,13 @@ class ProjectGenerator {
if (!outputFolder.exists()) {
outputFolder.mkdirs();
}
ZipInputStream zipStream = new ZipInputStream(
new ByteArrayInputStream(entity.getContent()));
try {
try (ZipInputStream zipStream = new ZipInputStream(
new ByteArrayInputStream(entity.getContent()))) {
extractFromStream(zipStream, overwrite, outputFolder);
fixExecutableFlag(outputFolder, "mvnw");
fixExecutableFlag(outputFolder, "gradlew");
Log.info("Project extracted to '" + outputFolder.getAbsolutePath() + "'");
}
finally {
zipStream.close();
}
}
private void extractFromStream(ZipInputStream zipStream, boolean overwrite,

View File

@@ -36,7 +36,6 @@ import org.springframework.boot.cli.compiler.GroovyCompilerConfiguration;
*
* @author Dave Syer
* @author Andy Wilkinson
* @since 1.2.0
*/
class GroovyGrabDependencyResolver implements DependencyResolver {
@@ -70,18 +69,14 @@ class GroovyGrabDependencyResolver implements DependencyResolver {
private String createSources(List<String> artifactIdentifiers) throws IOException {
File file = File.createTempFile("SpringCLIDependency", ".groovy");
file.deleteOnExit();
OutputStreamWriter stream = new OutputStreamWriter(new FileOutputStream(file),
"UTF-8");
try {
try (OutputStreamWriter stream = new OutputStreamWriter(
new FileOutputStream(file), "UTF-8")) {
for (String artifactIdentifier : artifactIdentifiers) {
stream.write("@Grab('" + artifactIdentifier + "')");
}
// Dummy class to force compiler to do grab
stream.write("class Installer {}");
}
finally {
stream.close();
}
// Windows paths get tricky unless you work with URI
return file.getAbsoluteFile().toURI().toString();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -38,7 +38,6 @@ import org.springframework.util.SystemPropertyUtils;
* Shared logic for the {@link InstallCommand} and {@link UninstallCommand}.
*
* @author Andy Wilkinson
* @since 1.2.0
*/
class Installer {
@@ -82,13 +81,9 @@ class Installer {
}
private void saveInstallCounts() throws IOException {
FileWriter writer = new FileWriter(getInstalled());
try {
try (FileWriter writer = new FileWriter(getInstalled())) {
this.installCounts.store(writer, null);
}
finally {
writer.close();
}
}
public void install(List<String> artifactIdentifiers) throws Exception {

View File

@@ -98,14 +98,10 @@ public class ExtendedGroovyClassLoader extends GroovyClassLoader {
private Class<?> findSharedClass(String name) {
try {
String path = name.replace('.', '/').concat(".class");
InputStream inputStream = getParent().getResourceAsStream(path);
if (inputStream != null) {
try {
try (InputStream inputStream = getParent().getResourceAsStream(path)) {
if (inputStream != null) {
return defineClass(name, FileCopyUtils.copyToByteArray(inputStream));
}
finally {
inputStream.close();
}
}
return null;
}