Skip to content

Commit 07e6101

Browse files
author
Vincent Potucek
committed
simplify try with resource
Signed-off-by: Vincent Potucek <[email protected]>
1 parent 4436755 commit 07e6101

File tree

19 files changed

+46
-25
lines changed

19 files changed

+46
-25
lines changed

integration-tests/src/test/java/org/springframework/aop/framework/autoproxy/AdvisorAutoProxyCreatorIntegrationTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ void testRollbackRulesOnMethodPreventRollback() throws Exception {
158158
try {
159159
rb.echoException(new ServletException());
160160
}
161-
catch (ServletException ignored) {
161+
catch (ServletException ex) {
162+
162163
}
163164
assertThat(txMan.commits).as("Transaction counts match").isEqualTo(1);
164165
}
@@ -271,7 +272,9 @@ public void before(Method method, Object[] args, Object target) throws Throwable
271272
TransactionInterceptor.currentTransactionStatus();
272273
throw new RuntimeException("Shouldn't have a transaction");
273274
}
274-
catch (NoTransactionException ignored) { } // this is Ok
275+
catch (NoTransactionException ex) {
276+
// this is Ok
277+
}
275278
}
276279
super.before(method, args, target);
277280
}

spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJExpressionPointcut.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,10 @@ private ShadowMatch getTargetShadowMatch(Method method, Class<?> targetClass) {
453453
ClassUtils.toClassArray(ifcs), targetClass.getClassLoader());
454454
targetMethod = ClassUtils.getMostSpecificMethod(targetMethod, compositeInterface);
455455
}
456-
catch (IllegalArgumentException ignored) { }
457-
// Implemented interfaces probably expose conflicting method signatures...
458-
// Proceed with original target method.
456+
catch (IllegalArgumentException ex) {
457+
// Implemented interfaces probably expose conflicting method signatures...
458+
// Proceed with original target method.
459+
}
459460
}
460461
}
461462
return getShadowMatch(targetMethod, method);
@@ -477,7 +478,7 @@ private ShadowMatch getShadowMatch(Method targetMethod, Method originalMethod) {
477478
try {
478479
shadowMatch = pointcutExpression.matchesMethodExecution(methodToMatch);
479480
}
480-
catch (ReflectionWorldException ignored) {
481+
catch (ReflectionWorldException ex) {
481482
// Failed to introspect target method, probably because it has been loaded
482483
// in a special ClassLoader. Let's try the declaring ClassLoader instead...
483484
try {
@@ -500,7 +501,7 @@ private ShadowMatch getShadowMatch(Method targetMethod, Method originalMethod) {
500501
try {
501502
shadowMatch = pointcutExpression.matchesMethodExecution(methodToMatch);
502503
}
503-
catch (ReflectionWorldException ignored) {
504+
catch (ReflectionWorldException ex) {
504505
// Could neither introspect the target class nor the proxy class ->
505506
// let's try the original method's declaring class before we give up...
506507
try {

spring-context/src/test/java/org/springframework/aop/framework/AbstractAopProxyTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ void serializableTargetAndAdvice() throws Throwable {
233233
try {
234234
p2.echo(new IOException());
235235
}
236-
catch (IOException ignored) {
236+
catch (IOException ex) {
237+
237238
}
238239
assertThat(cta.getCalls()).isEqualTo(2);
239240
}

spring-core/src/main/java/org/springframework/util/FileCopyUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ private static void close(Closeable closeable) {
229229
try {
230230
closeable.close();
231231
}
232-
catch (IOException ignored) {
232+
catch (IOException ex) {
233+
// ignore
233234
}
234235
}
235236

spring-test/src/main/java/org/springframework/mock/http/client/MockClientHttpResponse.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ public void close() {
100100
try {
101101
getBody().close();
102102
}
103-
catch (IOException ignored) {
103+
catch (IOException ex) {
104+
// ignore
104105
}
105106
}
106107

spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpResponse.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ public void close() {
8989
this.httpResponse.close();
9090
}
9191
}
92-
catch (IOException ignored) {
92+
catch (IOException ex) {
93+
// Ignore exception on close...
9394
}
9495
}
9596

spring-web/src/main/java/org/springframework/http/client/ReactorClientHttpResponse.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ public void close() {
108108
StreamUtils.drain(body);
109109
body.close();
110110
}
111-
catch (IOException ignored) {
111+
catch (IOException ex) {
112+
// ignore
112113
}
113114
}
114115

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ protected void writeContent(Resource resource, HttpOutputMessage outputMessage)
153153
in.transferTo(body);
154154
body.flush();
155155
}
156-
catch (Throwable ignored) { } // ignore, see SPR-12999, SPR-13620
156+
catch (Throwable ignored) {
157+
// see SPR-12999, SPR-13620
158+
}
157159
}
158160

159161
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outp
183183
try {
184184
in.close();
185185
}
186-
catch (IOException ignored) { }
186+
catch (IOException ex) {
187+
// ignore
188+
}
187189
}
188190
}
189191

spring-web/src/main/java/org/springframework/web/client/HttpMessageConverterExtractor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ private static byte[] getResponseBody(ClientHttpResponse response) {
152152
try {
153153
return FileCopyUtils.copyToByteArray(response.getBody());
154154
}
155-
catch (IOException ignored) {
155+
catch (IOException ex) {
156+
// ignore
156157
}
157158
return new byte[0];
158159
}

spring-web/src/test/java/org/springframework/web/context/request/RequestContextListenerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void requestContextListenerWithDifferentThread() {
9393
try {
9494
thread.join();
9595
}
96-
catch (InterruptedException ignored) {
96+
catch (InterruptedException ex) {
9797
}
9898
// Still bound to original thread, but at least completed.
9999
assertThat(RequestContextHolder.getRequestAttributes()).isNotNull();

spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/client/MockClientHttpResponse.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ public void close() {
100100
try {
101101
getBody().close();
102102
}
103-
catch (IOException ignored) {
103+
catch (IOException ex) {
104+
// ignore
104105
}
105106
}
106107

spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceHandlerUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ else if (location instanceof UrlResource) {
7676
Assert.isTrue(path.endsWith(FOLDER_SEPARATOR) || path.endsWith(WINDOWS_FOLDER_SEPARATOR),
7777
"Resource location does not end with slash: " + path);
7878
}
79-
catch (IOException ignored) {
79+
catch (IOException ex) {
80+
// ignore
8081
}
8182
}
8283

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ void closeStreamIfNecessary(InputStream body) {
191191
try {
192192
body.close();
193193
}
194-
catch (IOException ignored) {
194+
catch (IOException ex) {
195+
// ignore
195196
}
196197
}
197198

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHandlerUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ else if (location instanceof UrlResource) {
7777
Assert.isTrue(path.endsWith(FOLDER_SEPARATOR) || path.endsWith(WINDOWS_FOLDER_SEPARATOR),
7878
"Resource location does not end with slash: " + path);
7979
}
80-
catch (IOException ignored) {
80+
catch (IOException ex) {
81+
// ignore
8182
}
8283
}
8384

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolverTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,8 @@ static class ResponseStatusTestExceptionResolver {
611611

612612
@ExceptionHandler(SocketTimeoutException.class)
613613
@ResponseStatus(code = HttpStatus.GATEWAY_TIMEOUT, reason = "gateway.timeout")
614-
public void handleException(SocketTimeoutException ignored) {
614+
public void handleException(SocketTimeoutException ex) {
615+
615616
}
616617
}
617618

spring-websocket/src/main/java/org/springframework/web/socket/handler/BinaryWebSocketHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ protected void handleTextMessage(WebSocketSession session, TextMessage message)
4141
try {
4242
session.close(CloseStatus.NOT_ACCEPTABLE.withReason("Text messages not supported"));
4343
}
44-
catch (IOException ignored) {
44+
catch (IOException ex) {
45+
// ignore
4546
}
4647
}
4748

spring-websocket/src/main/java/org/springframework/web/socket/handler/TextWebSocketHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ protected void handleBinaryMessage(WebSocketSession session, BinaryMessage messa
4141
try {
4242
session.close(CloseStatus.NOT_ACCEPTABLE.withReason("Binary messages not supported"));
4343
}
44-
catch (IOException ignored) {
44+
catch (IOException ex) {
45+
// ignore
4546
}
4647
}
4748

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,8 @@ private void sendErrorMessage(WebSocketSession session, Throwable error) {
401401
byte[] bytes = this.stompEncoder.encode(headerAccessor.getMessageHeaders(), EMPTY_PAYLOAD);
402402
// We cannot use try-with-resources here for the WebSocketSession, since we have
403403
// custom handling of the close() method in a finally-block.
404-
try (WebSocketSession _session = session){
405-
_session.sendMessage(new TextMessage(bytes));
404+
try {
405+
session.sendMessage(new TextMessage(bytes));
406406
}
407407
catch (Throwable ex) {
408408
// Could be part of normal workflow (for example, browser tab closed)

0 commit comments

Comments
 (0)