From 570bdbd2535180b8ef164f6095bfdc0157a4d7bd Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 11 Jan 2021 12:40:22 +0100 Subject: [PATCH] Avoid unnecessary sorting in base ExceptionHandlerMethodResolvers --- .../invocation/AbstractExceptionHandlerMethodResolver.java | 5 ++++- .../method/annotation/ExceptionHandlerMethodResolver.java | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractExceptionHandlerMethodResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractExceptionHandlerMethodResolver.java index 97c89b8fd6b5..e4e6b6632a25 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractExceptionHandlerMethodResolver.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractExceptionHandlerMethodResolver.java @@ -34,6 +34,7 @@ * * @author Rossen Stoyanchev * @author Juergen Hoeller + * @author Sam Brannen * @since 4.0 */ public abstract class AbstractExceptionHandlerMethodResolver { @@ -138,7 +139,9 @@ private Method getMappedMethod(Class exceptionType) { } } if (!matches.isEmpty()) { - matches.sort(new ExceptionDepthComparator(exceptionType)); + if (matches.size() > 1) { + matches.sort(new ExceptionDepthComparator(exceptionType)); + } return this.mappedMethods.get(matches.get(0)); } else { diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java b/spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java index 697092bc3ae9..3d5995ceae90 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java @@ -39,6 +39,7 @@ * * @author Rossen Stoyanchev * @author Juergen Hoeller + * @author Sam Brannen * @since 3.1 */ public class ExceptionHandlerMethodResolver { @@ -179,7 +180,9 @@ private Method getMappedMethod(Class exceptionType) { } } if (!matches.isEmpty()) { - matches.sort(new ExceptionDepthComparator(exceptionType)); + if (matches.size() > 1) { + matches.sort(new ExceptionDepthComparator(exceptionType)); + } return this.mappedMethods.get(matches.get(0)); } else {