Skip to content

Commit af7758c

Browse files
committed
Configure CheckStyle rule for empty catch blocks
This commit configures a new CheckStyle rule that fails for empty "catch" blocks, unless the exception is named "ignored" or "expected". This also fixes the remaining instances missed by the previous commit. Closes gh-35047
1 parent 0d4dfb6 commit af7758c

File tree

8 files changed

+16
-12
lines changed

8 files changed

+16
-12
lines changed

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-
// Implemented interfaces probably expose conflicting method signatures...
457-
// Proceed with original target method.
458-
catch (IllegalArgumentException ignored) {
456+
catch (IllegalArgumentException ex) {
457+
// Implemented interfaces probably expose conflicting method signatures...
458+
// Proceed with original target method.
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 ignored) {
481+
catch (ReflectionWorldException ex) {
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 ignored) {
504+
catch (ReflectionWorldException ex) {
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-core/src/main/java/org/springframework/aot/nativex/substitution/Target_Introspector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private static Class<?> findCustomizerClass(Class<?> type) {
4848
} while (!c.getName().equals("java.lang.Object"));
4949
}
5050
}
51-
catch (Exception exception) {
51+
catch (Exception ignored) {
5252
}
5353
return null;
5454
}

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

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

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

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

spring-web/src/main/java/org/springframework/web/filter/FormContentFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private boolean shouldParse(HttpServletRequest request) {
116116
MediaType mediaType = MediaType.parseMediaType(contentType);
117117
return MediaType.APPLICATION_FORM_URLENCODED.includes(mediaType);
118118
}
119-
catch (IllegalArgumentException ex) {
119+
catch (IllegalArgumentException ignored) {
120120
}
121121
}
122122
return false;

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

src/checkstyle/checkstyle-suppressions.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<suppress files="(^(?!.+[\\/]src[\\/]main[\\/]java[\\/]).*)|(.*framework-docs.*)" checks="JavadocPackage" />
1010

1111
<!-- Global: tests and test fixtures -->
12-
<suppress files="[\\/]src[\\/](test|testFixtures)[\\/](java|java21)[\\/]" checks="AnnotationLocation|AnnotationUseStyle|AtclauseOrder|AvoidNestedBlocks|FinalClass|HideUtilityClassConstructor|InnerTypeLast|JavadocStyle|JavadocType|JavadocVariable|LeftCurly|MultipleVariableDeclarations|NeedBraces|OneTopLevelClass|OuterTypeFilename|RequireThis|SpringCatch|SpringJavadoc|SpringNoThis|SpringDeprecatedCheck"/>
12+
<suppress files="[\\/]src[\\/](test|testFixtures)[\\/](java|java21)[\\/]" checks="AnnotationLocation|AnnotationUseStyle|AtclauseOrder|AvoidNestedBlocks|FinalClass|HideUtilityClassConstructor|InnerTypeLast|JavadocStyle|JavadocType|JavadocVariable|LeftCurly|MultipleVariableDeclarations|NeedBraces|OneTopLevelClass|OuterTypeFilename|RequireThis|SpringCatch|SpringJavadoc|SpringNoThis|SpringDeprecatedCheck|EmptyCatchBlock"/>
1313
<suppress files="[\\/]src[\\/](test|testFixtures)[\\/](java|java21)[\\/]" checks="RegexpSinglelineJava" id="systemOutErrPrint"/>
1414
<suppress files="[\\/]src[\\/](test|testFixtures)[\\/](java|java21)[\\/]" checks="SpringJUnit5" message="should not be public"/>
1515
<suppress files="[\\/]src[\\/]test[\\/](java|java21)[\\/]org[\\/]springframework[\\/].+(Tests|Suite)" checks="IllegalImport" id="bannedJUnitJupiterImports"/>

src/checkstyle/checkstyle.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
</module>
4040
<module name="com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck"/>
4141
<module name="com.puppycrawl.tools.checkstyle.checks.blocks.AvoidNestedBlocksCheck"/>
42+
<module name="com.puppycrawl.tools.checkstyle.checks.blocks.EmptyCatchBlockCheck">
43+
<property name="exceptionVariableName" value="expected|ignore"/>
44+
</module>
4245

4346
<!-- Class Design -->
4447
<module name="com.puppycrawl.tools.checkstyle.checks.design.FinalClassCheck"/>

0 commit comments

Comments
 (0)