Skip to content

Commit d3efd7e

Browse files
krzykphilwebb
authored andcommitted
Use try with close
See gh-33987
1 parent 0e68cae commit d3efd7e

File tree

10 files changed

+33
-66
lines changed

10 files changed

+33
-66
lines changed

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/annotation/EndpointDiscovererTests.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 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.
@@ -320,17 +320,14 @@ private void loadWithParent(ApplicationContext parent, Class<?> configuration,
320320
private void load(ApplicationContext parent, Class<?> configuration,
321321
Consumer<AnnotationConfigApplicationContext> consumer) {
322322
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
323-
if (parent != null) {
324-
context.setParent(parent);
325-
}
326-
context.register(configuration);
327-
context.refresh();
328-
try {
323+
try (context) {
324+
if (parent != null) {
325+
context.setParent(parent);
326+
}
327+
context.register(configuration);
328+
context.refresh();
329329
consumer.accept(context);
330330
}
331-
finally {
332-
context.close();
333-
}
334331
}
335332

336333
@Configuration(proxyBeanMethods = false)

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/io/InspectedContent.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 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.
@@ -103,12 +103,9 @@ public static InspectedContent of(Content content, Inspector... inspectors) thro
103103
public static InspectedContent of(IOConsumer<OutputStream> writer, Inspector... inspectors) throws IOException {
104104
Assert.notNull(writer, "Writer must not be null");
105105
InspectingOutputStream outputStream = new InspectingOutputStream(inspectors);
106-
try {
106+
try (outputStream) {
107107
writer.accept(outputStream);
108108
}
109-
finally {
110-
outputStream.close();
111-
}
112109
return new InspectedContent(outputStream.getSize(), outputStream.getContent());
113110
}
114111

spring-boot-project/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/JsonReader.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 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.
@@ -196,7 +196,7 @@ private Object readItemValue(Object value) throws Exception {
196196
}
197197

198198
private JSONObject readJson(InputStream in, Charset charset) throws Exception {
199-
try {
199+
try (in) {
200200
StringBuilder out = new StringBuilder();
201201
InputStreamReader reader = new InputStreamReader(in, charset);
202202
char[] buffer = new char[BUFFER_SIZE];
@@ -206,9 +206,6 @@ private JSONObject readJson(InputStream in, Charset charset) throws Exception {
206206
}
207207
return new JSONObject(out.toString());
208208
}
209-
finally {
210-
in.close();
211-
}
212209
}
213210

214211
}

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 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.
@@ -513,12 +513,9 @@ private static class CrcAndSize {
513513
private long size;
514514

515515
CrcAndSize(InputStream inputStream) throws IOException {
516-
try {
516+
try (inputStream) {
517517
load(inputStream);
518518
}
519-
finally {
520-
inputStream.close();
521-
}
522519
}
523520

524521
private void load(InputStream inputStream) throws IOException {

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 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.
@@ -128,12 +128,9 @@ private void setUpEntry(JarFile jarFile, JarArchiveEntry entry) throws IOExcepti
128128
*/
129129
@Override
130130
public void writeEntry(String entryName, InputStream inputStream) throws IOException {
131-
try {
131+
try (inputStream) {
132132
writeEntry(entryName, new InputStreamEntryWriter(inputStream));
133133
}
134-
finally {
135-
inputStream.close();
136-
}
137134
}
138135

139136
/**

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 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.
@@ -68,14 +68,11 @@ private String loadContent(File file) throws IOException {
6868
}
6969

7070
private String loadContent(InputStream inputStream) throws IOException {
71-
try {
71+
try (inputStream) {
7272
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
7373
copy(inputStream, outputStream);
7474
return outputStream.toString(StandardCharsets.UTF_8);
7575
}
76-
finally {
77-
inputStream.close();
78-
}
7976
}
8077

8178
private void copy(InputStream inputStream, OutputStream outputStream) throws IOException {

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

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 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.
@@ -43,12 +43,9 @@ final class SizeCalculatingEntryWriter implements EntryWriter {
4343

4444
private SizeCalculatingEntryWriter(EntryWriter entryWriter) throws IOException {
4545
SizeCalculatingOutputStream outputStream = new SizeCalculatingOutputStream();
46-
try {
46+
try (outputStream) {
4747
entryWriter.write(outputStream);
4848
}
49-
finally {
50-
outputStream.close();
51-
}
5249
this.content = outputStream.getContent();
5350
this.size = outputStream.getSize();
5451
}
@@ -67,12 +64,9 @@ private InputStream getContentInputStream() throws FileNotFoundException {
6764
}
6865

6966
private void copy(InputStream inputStream, OutputStream outputStream) throws IOException {
70-
try {
67+
try (inputStream) {
7168
StreamUtils.copy(inputStream, outputStream);
7269
}
73-
finally {
74-
inputStream.close();
75-
}
7670
}
7771

7872
@Override

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/StaticResourceJars.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 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.
@@ -131,12 +131,9 @@ private boolean isResourcesJar(File file) {
131131
}
132132

133133
private boolean isResourcesJar(JarFile jar) throws IOException {
134-
try {
134+
try (jar) {
135135
return jar.getName().endsWith(".jar") && (jar.getJarEntry("META-INF/resources") != null);
136136
}
137-
finally {
138-
jar.close();
139-
}
140137
}
141138

142139
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 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.
@@ -578,16 +578,13 @@ private <T> T doWithRetry(Callable<T> action) throws Exception {
578578

579579
protected final void doWithBlockedPort(BlockedPortAction action) throws Exception {
580580
ServerSocket serverSocket = new ServerSocket();
581-
int blockedPort = doWithRetry(() -> {
582-
serverSocket.bind(null);
583-
return serverSocket.getLocalPort();
584-
});
585-
try {
581+
try (serverSocket) {
582+
int blockedPort = doWithRetry(() -> {
583+
serverSocket.bind(null);
584+
return serverSocket.getLocalPort();
585+
});
586586
action.run(blockedPort);
587587
}
588-
finally {
589-
serverSocket.close();
590-
}
591588
}
592589

593590
public interface BlockedPortAction {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java

+5-8
Original file line numberDiff line numberDiff line change
@@ -1470,16 +1470,13 @@ private <T> T doWithRetry(Callable<T> action) throws Exception {
14701470

14711471
protected final void doWithBlockedPort(BlockedPortAction action) throws Exception {
14721472
ServerSocket serverSocket = new ServerSocket();
1473-
int blockedPort = doWithRetry(() -> {
1474-
serverSocket.bind(null);
1475-
return serverSocket.getLocalPort();
1476-
});
1477-
try {
1473+
try (serverSocket) {
1474+
int blockedPort = doWithRetry(() -> {
1475+
serverSocket.bind(null);
1476+
return serverSocket.getLocalPort();
1477+
});
14781478
action.run(blockedPort);
14791479
}
1480-
finally {
1481-
serverSocket.close();
1482-
}
14831480
}
14841481

14851482
private KeyStore loadStore() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {

0 commit comments

Comments
 (0)