From d3efd7e091258ebb8ab7363f95c7986ffce2a785 Mon Sep 17 00:00:00 2001 From: Krzysztof Krason Date: Thu, 26 Jan 2023 18:29:35 -0800 Subject: [PATCH] Use try with close See gh-33987 --- .../annotation/EndpointDiscovererTests.java | 17 +++++++---------- .../buildpack/platform/io/InspectedContent.java | 7 ++----- .../boot/configurationmetadata/JsonReader.java | 7 ++----- .../tasks/bundling/BootZipCopyAction.java | 7 ++----- .../boot/loader/tools/AbstractJarWriter.java | 7 ++----- .../boot/loader/tools/DefaultLaunchScript.java | 7 ++----- .../tools/SizeCalculatingEntryWriter.java | 12 +++--------- .../web/servlet/server/StaticResourceJars.java | 7 ++----- .../AbstractReactiveWebServerFactoryTests.java | 15 ++++++--------- .../AbstractServletWebServerFactoryTests.java | 13 +++++-------- 10 files changed, 33 insertions(+), 66 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/annotation/EndpointDiscovererTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/annotation/EndpointDiscovererTests.java index fde226e419..0cb7ed8cab 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/annotation/EndpointDiscovererTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/annotation/EndpointDiscovererTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 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. @@ -320,17 +320,14 @@ class EndpointDiscovererTests { private void load(ApplicationContext parent, Class configuration, Consumer consumer) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - if (parent != null) { - context.setParent(parent); - } - context.register(configuration); - context.refresh(); - try { + try (context) { + if (parent != null) { + context.setParent(parent); + } + context.register(configuration); + context.refresh(); consumer.accept(context); } - finally { - context.close(); - } } @Configuration(proxyBeanMethods = false) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/io/InspectedContent.java b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/io/InspectedContent.java index 14dd9398a6..1f42a68d3f 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/io/InspectedContent.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/io/InspectedContent.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 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. @@ -103,12 +103,9 @@ public class InspectedContent implements Content { public static InspectedContent of(IOConsumer writer, Inspector... inspectors) throws IOException { Assert.notNull(writer, "Writer must not be null"); InspectingOutputStream outputStream = new InspectingOutputStream(inspectors); - try { + try (outputStream) { writer.accept(outputStream); } - finally { - outputStream.close(); - } return new InspectedContent(outputStream.getSize(), outputStream.getContent()); } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/JsonReader.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/JsonReader.java index 1971074ae5..0e0b92c9f5 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/JsonReader.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/JsonReader.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 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. @@ -196,7 +196,7 @@ class JsonReader { } private JSONObject readJson(InputStream in, Charset charset) throws Exception { - try { + try (in) { StringBuilder out = new StringBuilder(); InputStreamReader reader = new InputStreamReader(in, charset); char[] buffer = new char[BUFFER_SIZE]; @@ -206,9 +206,6 @@ class JsonReader { } return new JSONObject(out.toString()); } - finally { - in.close(); - } } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java index 533d85f3ee..7f9ece9275 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 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. @@ -513,12 +513,9 @@ class BootZipCopyAction implements CopyAction { private long size; CrcAndSize(InputStream inputStream) throws IOException { - try { + try (inputStream) { load(inputStream); } - finally { - inputStream.close(); - } } private void load(InputStream inputStream) throws IOException { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/AbstractJarWriter.java b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/AbstractJarWriter.java index 340c9e1f9f..ffbdf5ec73 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/AbstractJarWriter.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/AbstractJarWriter.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 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. @@ -128,12 +128,9 @@ public abstract class AbstractJarWriter implements LoaderClassesWriter { */ @Override public void writeEntry(String entryName, InputStream inputStream) throws IOException { - try { + try (inputStream) { writeEntry(entryName, new InputStreamEntryWriter(inputStream)); } - finally { - inputStream.close(); - } } /** diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/DefaultLaunchScript.java b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/DefaultLaunchScript.java index aaa5b6cecc..ea914f1930 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/DefaultLaunchScript.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/DefaultLaunchScript.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 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. @@ -68,14 +68,11 @@ public class DefaultLaunchScript implements LaunchScript { } private String loadContent(InputStream inputStream) throws IOException { - try { + try (inputStream) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); copy(inputStream, outputStream); return outputStream.toString(StandardCharsets.UTF_8); } - finally { - inputStream.close(); - } } private void copy(InputStream inputStream, OutputStream outputStream) throws IOException { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/SizeCalculatingEntryWriter.java b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/SizeCalculatingEntryWriter.java index 36d13c65b2..22544bbc3e 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/SizeCalculatingEntryWriter.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/SizeCalculatingEntryWriter.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 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. @@ -43,12 +43,9 @@ final class SizeCalculatingEntryWriter implements EntryWriter { private SizeCalculatingEntryWriter(EntryWriter entryWriter) throws IOException { SizeCalculatingOutputStream outputStream = new SizeCalculatingOutputStream(); - try { + try (outputStream) { entryWriter.write(outputStream); } - finally { - outputStream.close(); - } this.content = outputStream.getContent(); this.size = outputStream.getSize(); } @@ -67,12 +64,9 @@ final class SizeCalculatingEntryWriter implements EntryWriter { } private void copy(InputStream inputStream, OutputStream outputStream) throws IOException { - try { + try (inputStream) { StreamUtils.copy(inputStream, outputStream); } - finally { - inputStream.close(); - } } @Override diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/StaticResourceJars.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/StaticResourceJars.java index a9c49f0470..280363ed0d 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/StaticResourceJars.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/StaticResourceJars.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 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. @@ -131,12 +131,9 @@ class StaticResourceJars { } private boolean isResourcesJar(JarFile jar) throws IOException { - try { + try (jar) { return jar.getName().endsWith(".jar") && (jar.getJarEntry("META-INF/resources") != null); } - finally { - jar.close(); - } } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java index 6ed039b760..1dab6a94f4 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 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. @@ -578,16 +578,13 @@ public abstract class AbstractReactiveWebServerFactoryTests { protected final void doWithBlockedPort(BlockedPortAction action) throws Exception { ServerSocket serverSocket = new ServerSocket(); - int blockedPort = doWithRetry(() -> { - serverSocket.bind(null); - return serverSocket.getLocalPort(); - }); - try { + try (serverSocket) { + int blockedPort = doWithRetry(() -> { + serverSocket.bind(null); + return serverSocket.getLocalPort(); + }); action.run(blockedPort); } - finally { - serverSocket.close(); - } } public interface BlockedPortAction { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java index c1f90c6958..d2ef917e9a 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java @@ -1470,16 +1470,13 @@ public abstract class AbstractServletWebServerFactoryTests { protected final void doWithBlockedPort(BlockedPortAction action) throws Exception { ServerSocket serverSocket = new ServerSocket(); - int blockedPort = doWithRetry(() -> { - serverSocket.bind(null); - return serverSocket.getLocalPort(); - }); - try { + try (serverSocket) { + int blockedPort = doWithRetry(() -> { + serverSocket.bind(null); + return serverSocket.getLocalPort(); + }); action.run(blockedPort); } - finally { - serverSocket.close(); - } } private KeyStore loadStore() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {