Browse Source

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
pull/27896/head
Marten Deinum 3 years ago committed by Sam Brannen
parent
commit
e1200f34e7
  1. 5
      spring-context-indexer/src/main/java/org/springframework/context/index/processor/MetadataStore.java
  2. 2
      spring-core/src/main/java/org/springframework/core/LocalVariableTableParameterNameDiscoverer.java
  3. 2
      spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.java
  4. 2
      spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java
  5. 2
      spring-web/src/main/java/org/springframework/http/converter/ResourceRegionHttpMessageConverter.java
  6. 2
      spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java

5
spring-context-indexer/src/main/java/org/springframework/context/index/processor/MetadataStore.java

@ -62,12 +62,9 @@ class MetadataStore { @@ -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 {

2
spring-core/src/main/java/org/springframework/core/LocalVariableTableParameterNameDiscoverer.java

@ -101,6 +101,8 @@ public class LocalVariableTableParameterNameDiscoverer implements ParameterNameD @@ -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<Executable, String[]> map = new ConcurrentHashMap<>(32);

2
spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.java

@ -169,6 +169,8 @@ public class BufferedImageHttpMessageConverter implements HttpMessageConverter<B @@ -169,6 +169,8 @@ public class BufferedImageHttpMessageConverter implements HttpMessageConverter<B
ImageInputStream imageInputStream = null;
ImageReader imageReader = null;
// We cannot use try-with-resources here as, potential, exception upon closing
// would still bubble up the stack
try {
imageInputStream = createImageInputStream(inputMessage.getBody());
MediaType contentType = inputMessage.getHeaders().getContentType();

2
spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java

@ -131,6 +131,8 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R @@ -131,6 +131,8 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
protected void writeContent(Resource resource, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
// We cannot use try-with-resources here as, potential, exception upon closing
// would still bubble up the stack
try {
InputStream in = resource.getInputStream();
try {

2
spring-web/src/main/java/org/springframework/http/converter/ResourceRegionHttpMessageConverter.java

@ -155,6 +155,8 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa @@ -155,6 +155,8 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa
responseHeaders.setContentLength(rangeLength);
InputStream in = region.getResource().getInputStream();
// We cannot use try-with-resources here as, potential, exception upon closing
// would still bubble up the stack
try {
StreamUtils.copyRange(in, outputMessage.getBody(), start, end);
}

2
spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java

@ -374,6 +374,8 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE @@ -374,6 +374,8 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE
headerAccessor.setMessage(error.getMessage());
byte[] bytes = this.stompEncoder.encode(headerAccessor.getMessageHeaders(), EMPTY_PAYLOAD);
// We cannot use try-with-resources here as, potential, exception upon closing
// would still bubble up the stack
try {
session.sendMessage(new TextMessage(bytes));
}

Loading…
Cancel
Save