Skip to content

Commit 0d4dfb6

Browse files
Vincent Potucekbclozel
authored andcommitted
Rename exception variables in empty catch blocks
The Spring codebase sometimes ignores exceptions in catch blocks on purpose. This is often called out by an inline comment. We should make this more obvious by renaming the exception argument in the catch block to declare whether the exception is "ignored" or "expected". See gh-35047 Signed-off-by: Vincent Potucek <[email protected]> [[email protected]: rework commit message] Signed-off-by: Brian Clozel <[email protected]>
1 parent cd3ac44 commit 0d4dfb6

File tree

46 files changed

+66
-108
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+66
-108
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ void testRollbackRulesOnMethodPreventRollback() throws Exception {
158158
try {
159159
rb.echoException(new ServletException());
160160
}
161-
catch (ServletException ex) {
162-
161+
catch (ServletException ignored) {
163162
}
164163
assertThat(txMan.commits).as("Transaction counts match").isEqualTo(1);
165164
}
@@ -272,7 +271,7 @@ public void before(Method method, Object[] args, Object target) throws Throwable
272271
TransactionInterceptor.currentTransactionStatus();
273272
throw new RuntimeException("Shouldn't have a transaction");
274273
}
275-
catch (NoTransactionException ex) {
274+
catch (NoTransactionException ignored) {
276275
// this is Ok
277276
}
278277
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,9 @@ private ShadowMatch getTargetShadowMatch(Method method, Class<?> targetClass) {
453453
ClassUtils.toClassArray(ifcs), targetClass.getClassLoader());
454454
targetMethod = ClassUtils.getMostSpecificMethod(targetMethod, compositeInterface);
455455
}
456-
catch (IllegalArgumentException ex) {
457-
// Implemented interfaces probably expose conflicting method signatures...
458-
// Proceed with original target method.
456+
// Implemented interfaces probably expose conflicting method signatures...
457+
// Proceed with original target method.
458+
catch (IllegalArgumentException ignored) {
459459
}
460460
}
461461
}
@@ -478,7 +478,7 @@ private ShadowMatch getShadowMatch(Method targetMethod, Method originalMethod) {
478478
try {
479479
shadowMatch = pointcutExpression.matchesMethodExecution(methodToMatch);
480480
}
481-
catch (ReflectionWorldException ex) {
481+
catch (ReflectionWorldException ignored) {
482482
// Failed to introspect target method, probably because it has been loaded
483483
// in a special ClassLoader. Let's try the declaring ClassLoader instead...
484484
try {
@@ -501,7 +501,7 @@ private ShadowMatch getShadowMatch(Method targetMethod, Method originalMethod) {
501501
try {
502502
shadowMatch = pointcutExpression.matchesMethodExecution(methodToMatch);
503503
}
504-
catch (ReflectionWorldException ex) {
504+
catch (ReflectionWorldException ignored) {
505505
// Could neither introspect the target class nor the proxy class ->
506506
// let's try the original method's declaring class before we give up...
507507
try {

spring-beans/src/main/java/org/springframework/beans/factory/annotation/BeanFactoryAnnotationUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ public static boolean isQualifierMatch(
208208
}
209209
}
210210
}
211-
catch (NoSuchBeanDefinitionException ex) {
212-
// Ignore - can't compare qualifiers for a manually registered singleton object
211+
catch (NoSuchBeanDefinitionException ignored) {
212+
// can't compare qualifiers for a manually registered singleton object
213213
}
214214
}
215215
return false;

spring-context/src/main/java/org/springframework/context/support/PostProcessorRegistrationDelegate.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,7 @@ private void resolveTypeStringValue(TypedStringValue typedStringValue) {
523523
try {
524524
typedStringValue.resolveTargetType(this.beanFactory.getBeanClassLoader());
525525
}
526-
catch (ClassNotFoundException ex) {
527-
// ignore
526+
catch (ClassNotFoundException ignored) {
528527
}
529528
}
530529

spring-context/src/main/java/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ public void afterPropertiesSet() {
266266
Method eclMethod = configuration.getClass().getMethod("externalClassLoader", ClassLoader.class);
267267
ReflectionUtils.invokeMethod(eclMethod, configuration, this.applicationContext.getClassLoader());
268268
}
269-
catch (NoSuchMethodException ex) {
270-
// Ignore - no Hibernate Validator 5.2+ or similar provider
269+
catch (NoSuchMethodException ignored) {
270+
// no Hibernate Validator 5.2+ or similar provider
271271
}
272272
}
273273

@@ -417,8 +417,8 @@ public <T> T unwrap(@Nullable Class<T> type) {
417417
try {
418418
return super.unwrap(type);
419419
}
420-
catch (ValidationException ex) {
421-
// Ignore - we'll try ValidatorFactory unwrapping next
420+
catch (ValidationException ignored) {
421+
// we'll try ValidatorFactory unwrapping next
422422
}
423423
}
424424
if (this.validatorFactory != null) {

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

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

spring-core-test/src/main/java/org/springframework/core/test/tools/CompileWithForkedClassLoaderClassLoader.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ protected Class<?> findClass(String name) throws ClassNotFoundException {
8484
try (stream) {
8585
return stream.readAllBytes();
8686
}
87-
catch (IOException ex) {
88-
// ignore
87+
catch (IOException ignored) {
8988
}
9089
}
9190
return null;

spring-core/src/main/java/org/springframework/cglib/proxy/BridgeMethodResolver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ public BridgeMethodResolver(Map declToBridge, ClassLoader classLoader) {
7373
} finally {
7474
is.close();
7575
}
76-
} catch (IOException ignored) {}
76+
} catch (IOException ignored) {
77+
}
7778
}
7879
return resolved;
7980
}

spring-core/src/main/java/org/springframework/cglib/proxy/MethodProxy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public static MethodProxy create(Class c1, Class c2, String desc, String name1,
6262
try {
6363
proxy.init();
6464
}
65-
catch (CodeGenerationException ex) {
66-
// Ignore - to be retried when actually needed later on (possibly not at all)
65+
catch (CodeGenerationException ignored) {
66+
// to be retried when actually needed later on (possibly not at all)
6767
}
6868
}
6969
// SPRING PATCH END

spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,7 @@ private static class KotlinDelegate {
438438
return constructor;
439439
}
440440
}
441-
catch (UnsupportedOperationException ex) {
442-
// ignore
441+
catch (UnsupportedOperationException ignored) {
443442
}
444443
return null;
445444
}

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

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

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/DefaultStompSession.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,7 @@ private void resetConnection() {
526526
try {
527527
conn.close();
528528
}
529-
catch (Throwable ex) {
530-
// ignore
529+
catch (Throwable ignored) {
531530
}
532531
}
533532
}

spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,8 +1000,7 @@ private String getHost(String elementNamespace, DataHandler dataHandler) {
10001000
URI uri = ResourceUtils.toURI(elementNamespace);
10011001
return uri.getHost();
10021002
}
1003-
catch (URISyntaxException ex) {
1004-
// ignore
1003+
catch (URISyntaxException ignored) {
10051004
}
10061005
return dataHandler.getName();
10071006
}

spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/R2dbcTransactionManagerTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,8 +710,7 @@ public Mono<Void> afterCompletion(int status) {
710710
try {
711711
return Mono.fromRunnable(() -> doAfterCompletion(status));
712712
}
713-
catch (Throwable ex) {
714-
// ignore
713+
catch (Throwable ignored) {
715714
}
716715

717716
return Mono.empty();

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

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

spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,8 +1129,7 @@ private long parseDateHeader(String name, String value) {
11291129
try {
11301130
return simpleDateFormat.parse(value).getTime();
11311131
}
1132-
catch (ParseException ex) {
1133-
// ignore
1132+
catch (ParseException ignored) {
11341133
}
11351134
}
11361135
throw new IllegalArgumentException("Cannot parse date value '" + value + "' for '" + name + "' header");

spring-web/src/main/java/org/springframework/http/HttpHeaders.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,8 +1223,7 @@ public void setHost(@Nullable InetSocketAddress host) {
12231223
try {
12241224
port = Integer.parseInt(portString);
12251225
}
1226-
catch (NumberFormatException ex) {
1227-
// ignore
1226+
catch (NumberFormatException ignored) {
12281227
}
12291228
}
12301229

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

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

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

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

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ else if (expiresAttribute != null) {
119119
ZonedDateTime expiresDate = ZonedDateTime.parse(expiresAttribute, DateTimeFormatter.RFC_1123_DATE_TIME);
120120
return Duration.between(ZonedDateTime.now(expiresDate.getZone()), expiresDate).toSeconds();
121121
}
122-
catch (DateTimeParseException ex) {
123-
// ignore
122+
catch (DateTimeParseException ignored) {
124123
}
125124
}
126125
return -1;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,20 +158,20 @@ protected void writeContent(Resource resource, HttpOutputMessage outputMessage)
158158
in.transferTo(out);
159159
out.flush();
160160
}
161-
catch (NullPointerException ex) {
162-
// ignore, see SPR-13620
161+
catch (NullPointerException ignored) {
162+
// see SPR-13620
163163
}
164164
finally {
165165
try {
166166
in.close();
167167
}
168-
catch (Throwable ex) {
169-
// ignore, see SPR-12999
168+
catch (Throwable ignored) {
169+
// see SPR-12999
170170
}
171171
}
172172
}
173-
catch (FileNotFoundException ex) {
174-
// ignore, see SPR-12999
173+
catch (FileNotFoundException ignored) {
174+
// see SPR-12999
175175
}
176176
}
177177

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,7 @@ protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outp
183183
try {
184184
in.close();
185185
}
186-
catch (IOException ex) {
187-
// ignore
186+
catch (IOException ignored) {
188187
}
189188
}
190189
}
@@ -244,8 +243,7 @@ private void writeResourceRegionCollection(Collection<ResourceRegion> resourceRe
244243
in.close();
245244
}
246245
}
247-
catch (IOException ex) {
248-
// ignore
246+
catch (IOException ignored) {
249247
}
250248
}
251249

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,7 @@ private StreamSource readStreamSource(InputStream body) throws IOException {
270270
transform(t, new StreamResult(os));
271271
return os.count;
272272
}
273-
catch (TransformerException ex) {
274-
// ignore
273+
catch (TransformerException ignored) {
275274
}
276275
}
277276
return null;

spring-web/src/main/java/org/springframework/http/server/reactive/AbstractListenerWriteProcessor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,7 @@ public <T> void onNext(AbstractListenerWriteProcessor<T> processor, T data) {
439439
// ignore
440440
}
441441
@Override
442-
public <T> void onError(AbstractListenerWriteProcessor<T> processor, Throwable ex) {
443-
// ignore
442+
public <T> void onError(AbstractListenerWriteProcessor<T> processor, Throwable ignored) {
444443
}
445444
@Override
446445
public <T> void onComplete(AbstractListenerWriteProcessor<T> processor) {

spring-web/src/main/java/org/springframework/http/server/reactive/ServletHttpHandlerAdapter.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,26 +309,23 @@ private static void delegateTimeout(AsyncListener listener, AsyncEvent event) {
309309
try {
310310
listener.onTimeout(event);
311311
}
312-
catch (Exception ex) {
313-
// Ignore
312+
catch (Exception ignored) {
314313
}
315314
}
316315

317316
private static void delegateError(AsyncListener listener, AsyncEvent event) {
318317
try {
319318
listener.onError(event);
320319
}
321-
catch (Exception ex) {
322-
// Ignore
320+
catch (Exception ignored) {
323321
}
324322
}
325323

326324
private static void delegateComplete(AsyncListener listener, AsyncEvent event) {
327325
try {
328326
listener.onComplete(event);
329327
}
330-
catch (Exception ex) {
331-
// Ignore
328+
catch (Exception ignored) {
332329
}
333330
}
334331

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

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

spring-web/src/main/java/org/springframework/web/context/request/async/CallableInterceptorChain.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ private void cancelTask() {
113113
try {
114114
future.cancel(true);
115115
}
116-
catch (Throwable ex) {
117-
// Ignore
116+
catch (Throwable ignored) {
118117
}
119118
}
120119
}

spring-web/src/main/java/org/springframework/web/context/request/async/StandardServletAsyncWebRequest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,7 @@ private int tryObtainLock() {
235235
break;
236236
}
237237
}
238-
catch (InterruptedException ex) {
239-
// ignore
238+
catch (InterruptedException ignored) {
240239
}
241240
}
242241

spring-web/src/main/java/org/springframework/web/context/support/ServletContextResource.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ public boolean isReadable() {
122122
try {
123123
is.close();
124124
}
125-
catch (IOException ex) {
126-
// ignore
125+
catch (IOException ignored) {
127126
}
128127
return true;
129128
}

spring-web/src/main/java/org/springframework/web/service/registry/HttpServiceProxyRegistryFactoryBean.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ private static void addGroupAdapter(
151151
Class<?> clazz = ClassUtils.forName(className, HttpServiceGroupAdapter.class.getClassLoader());
152152
groupAdapters.put(clientType, (HttpServiceGroupAdapter<?>) BeanUtils.instantiateClass(clazz));
153153
}
154-
catch (ClassNotFoundException ex) {
155-
// ignore
154+
catch (ClassNotFoundException ignored) {
156155
}
157156
}
158157
}

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 ex) {
96+
catch (InterruptedException ignored) {
9797
}
9898
// Still bound to original thread, but at least completed.
9999
assertThat(RequestContextHolder.getRequestAttributes()).isNotNull();

0 commit comments

Comments
 (0)