Skip to content

Commit 4f194c2

Browse files
committed
Merge pull request #11624 from igor-suhorukov:master
* pr/11624: Polish "Fix potential resource leaks" Fix potential resource leaks
2 parents 0b22eb9 + d43346d commit 4f194c2

File tree

4 files changed

+25
-17
lines changed
  • spring-boot-project
    • spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure
    • spring-boot-tools
      • spring-boot-loader/src/main/java/org/springframework/boot/loader/jar
      • spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools
      • spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven

4 files changed

+25
-17
lines changed

spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.devtools.autoconfigure;
1818

19+
import java.sql.Connection;
20+
import java.sql.Statement;
1921
import java.util.Arrays;
2022
import java.util.HashSet;
2123
import java.util.Set;
@@ -97,7 +99,11 @@ static final class NonEmbeddedInMemoryDatabaseShutdownExecutor
9799
@Override
98100
public void destroy() throws Exception {
99101
if (dataSourceRequiresShutdown()) {
100-
this.dataSource.getConnection().createStatement().execute("SHUTDOWN");
102+
try (Connection connection = this.dataSource.getConnection()) {
103+
try (Statement statement = connection.createStatement()) {
104+
statement.execute("SHUTDOWN");
105+
}
106+
}
101107
}
102108
}
103109

spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -215,16 +215,16 @@ public void writeLoaderClasses() throws IOException {
215215
@Override
216216
public void writeLoaderClasses(String loaderJarResourceName) throws IOException {
217217
URL loaderJar = getClass().getClassLoader().getResource(loaderJarResourceName);
218-
JarInputStream inputStream = new JarInputStream(
219-
new BufferedInputStream(loaderJar.openStream()));
220-
JarEntry entry;
221-
while ((entry = inputStream.getNextJarEntry()) != null) {
222-
if (entry.getName().endsWith(".class")) {
223-
writeEntry(new JarArchiveEntry(entry),
224-
new InputStreamEntryWriter(inputStream, false));
218+
try (JarInputStream inputStream = new JarInputStream(
219+
new BufferedInputStream(loaderJar.openStream()))) {
220+
JarEntry entry;
221+
while ((entry = inputStream.getNextJarEntry()) != null) {
222+
if (entry.getName().endsWith(".class")) {
223+
writeEntry(new JarArchiveEntry(entry),
224+
new InputStreamEntryWriter(inputStream, false));
225+
}
225226
}
226227
}
227-
inputStream.close();
228228
}
229229

230230
/**

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -157,7 +157,9 @@ public Manifest getManifest() throws IOException {
157157
Manifest manifest = (this.manifest == null ? null : this.manifest.get());
158158
if (manifest == null) {
159159
if (this.type == JarFileType.NESTED_DIRECTORY) {
160-
manifest = new JarFile(this.getRootJarFile()).getManifest();
160+
try (JarFile rootJarFile = new JarFile(this.getRootJarFile())) {
161+
manifest = rootJarFile.getManifest();
162+
}
161163
}
162164
else {
163165
try (InputStream inputStream = getInputStream(MANIFEST_NAME,

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -123,12 +123,12 @@ private boolean hasDevtools() {
123123
return this.hasDevtools;
124124
}
125125

126-
@SuppressWarnings("resource")
127126
private boolean checkForDevtools() {
128127
try {
129128
URL[] urls = getClassPathUrls();
130-
URLClassLoader classLoader = new URLClassLoader(urls);
131-
return (classLoader.findResource(RESTARTER_CLASS_LOCATION) != null);
129+
try (URLClassLoader classLoader = new URLClassLoader(urls)) {
130+
return (classLoader.findResource(RESTARTER_CLASS_LOCATION) != null);
131+
}
132132
}
133133
catch (Exception ex) {
134134
return false;

0 commit comments

Comments
 (0)