From e1200f34e735769fcc9bac86059d56312460e132 Mon Sep 17 00:00:00 2001 From: Marten Deinum Date: Thu, 16 Dec 2021 07:10:12 +0100 Subject: [PATCH] Use try-with-resources for AutoClosables where feasible Where unfeasible, this commit adds inline comments to explain why try-with-resources must not be used in certain scenarios. The purpose of the comments is to avoid accidental conversion to try-with-resources at a later date. Closes gh-27823 --- .../context/index/processor/MetadataStore.java | 5 +---- .../core/LocalVariableTableParameterNameDiscoverer.java | 2 ++ .../http/converter/BufferedImageHttpMessageConverter.java | 2 ++ .../http/converter/ResourceHttpMessageConverter.java | 2 ++ .../http/converter/ResourceRegionHttpMessageConverter.java | 2 ++ .../web/socket/messaging/StompSubProtocolHandler.java | 2 ++ 6 files changed, 11 insertions(+), 4 deletions(-) diff --git a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/MetadataStore.java b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/MetadataStore.java index c00f682b77..8593fc7508 100644 --- a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/MetadataStore.java +++ b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/MetadataStore.java @@ -62,12 +62,9 @@ class MetadataStore { private CandidateComponentsMetadata readMetadata(InputStream in) throws IOException { - try { + try (in){ return PropertiesMarshaller.read(in); } - finally { - in.close(); - } } private FileObject getMetadataResource() throws IOException { diff --git a/spring-core/src/main/java/org/springframework/core/LocalVariableTableParameterNameDiscoverer.java b/spring-core/src/main/java/org/springframework/core/LocalVariableTableParameterNameDiscoverer.java index 6498a35f56..70a2a6adf6 100644 --- a/spring-core/src/main/java/org/springframework/core/LocalVariableTableParameterNameDiscoverer.java +++ b/spring-core/src/main/java/org/springframework/core/LocalVariableTableParameterNameDiscoverer.java @@ -101,6 +101,8 @@ public class LocalVariableTableParameterNameDiscoverer implements ParameterNameD } return NO_DEBUG_INFO_MAP; } + // We cannot use try-with-resources here as, potential, exception upon closing + // would still bubble up the stack try { ClassReader classReader = new ClassReader(is); Map map = new ConcurrentHashMap<>(32); diff --git a/spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.java index cdfa5781c9..7a020f25c0 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.java @@ -169,6 +169,8 @@ public class BufferedImageHttpMessageConverter implements HttpMessageConverter