From 5f38996cd6df9496774adb2fc64a3051d970b226 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 9 Dec 2013 08:19:17 -0500 Subject: [PATCH] Add methods for static resolution of @ExceptionHandler --- .../ExceptionHandlerMethodResolver.java | 11 +++++++++- .../ExceptionHandlerExceptionResolver.java | 20 ++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) 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 535cb01eaca..3c7e7389dd1 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 @@ -114,7 +114,16 @@ public class ExceptionHandlerMethodResolver { * @return a method to handle the exception or {@code null} */ public Method resolveMethod(Exception exception) { - Class exceptionType = exception.getClass(); + return resolveMethodByExceptionType(exception.getClass()); + } + + /** + * Find a method to handle the given exception type. This can be useful if + * an Exception instance is not available (example for tools). + * @param exceptionType the exception type + * @return a method to handle the exception or {@code null} + */ + public Method resolveMethodByExceptionType(Class exceptionType) { Method method = this.exceptionLookupCache.get(exceptionType); if (method == null) { method = getMappedMethod(exceptionType); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java index 2dc94d55c93..e1c3775b94d 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java @@ -205,6 +205,23 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce this.contentNegotiationManager = contentNegotiationManager; } + /** + * Return the configured {@link ContentNegotiationManager}. + */ + public ContentNegotiationManager getContentNegotiationManager() { + return this.contentNegotiationManager; + } + + /** + * Return an unmodifiable Map with the {@link ControllerAdvice @ControllerAdvice} + * beans discovered in the ApplicationContext. The returned map will be empty if + * the method is invoked before the bean has been initialized via + * {@link #afterPropertiesSet()}. + */ + public Map getExceptionHandlerAdviceCache() { + return Collections.unmodifiableMap(this.exceptionHandlerAdviceCache); + } + @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; @@ -365,7 +382,8 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce } for (Entry entry : this.exceptionHandlerAdviceCache.entrySet()) { if(entry.getKey().isApplicableToBeanType(handlerType)) { - Method method = entry.getValue().resolveMethod(exception); + ExceptionHandlerMethodResolver resolver = entry.getValue(); + Method method = resolver.resolveMethod(exception); if (method != null) { return new ServletInvocableHandlerMethod(entry.getKey().resolveBean(), method); }