Add methods for static resolution of @ExceptionHandler

This commit is contained in:
Rossen Stoyanchev 2013-12-09 08:19:17 -05:00
parent 6078c27e60
commit 5f38996cd6
2 changed files with 29 additions and 2 deletions

View File

@ -114,7 +114,16 @@ public class ExceptionHandlerMethodResolver {
* @return a method to handle the exception or {@code null}
*/
public Method resolveMethod(Exception exception) {
Class<? extends Exception> 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<? extends Exception> exceptionType) {
Method method = this.exceptionLookupCache.get(exceptionType);
if (method == null) {
method = getMappedMethod(exceptionType);

View File

@ -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<ControllerAdviceBean, ExceptionHandlerMethodResolver> 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<ControllerAdviceBean, ExceptionHandlerMethodResolver> 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);
}