org.springframework.web.method.annotation
Class ExceptionHandlerMethodResolver

java.lang.Object
  extended by org.springframework.web.method.annotation.ExceptionHandlerMethodResolver

public class ExceptionHandlerMethodResolver
extends java.lang.Object

Given a set of @ExceptionHandler methods at initialization, finds the best matching method mapped to an exception at runtime.

Exception mappings are extracted from the method @ExceptionHandler annotation or by looking for Throwable method arguments.

Since:
3.1
Author:
Rossen Stoyanchev

Field Summary
static ReflectionUtils.MethodFilter EXCEPTION_HANDLER_METHODS
          A filter for selecting @ExceptionHandler methods.
private  java.util.Map<java.lang.Class<? extends java.lang.Throwable>,java.lang.reflect.Method> exceptionLookupCache
           
private  java.util.Map<java.lang.Class<? extends java.lang.Throwable>,java.lang.reflect.Method> mappedMethods
           
private static java.lang.reflect.Method NO_METHOD_FOUND
           
 
Constructor Summary
ExceptionHandlerMethodResolver(java.lang.Class<?> handlerType)
          A constructor that finds ExceptionHandler methods in a handler.
 
Method Summary
private  void addExceptionMapping(java.lang.Class<? extends java.lang.Throwable> exceptionType, java.lang.reflect.Method method)
           
private  java.util.List<java.lang.Class<? extends java.lang.Throwable>> detectMappedExceptions(java.lang.reflect.Method method)
          Detect the exceptions an @ExceptionHandler method is mapped to.
private  java.lang.reflect.Method getMappedMethod(java.lang.Class<? extends java.lang.Exception> exceptionType)
          Return the method mapped to the exception type, or null.
private  void init(java.util.Set<java.lang.reflect.Method> exceptionHandlerMethods)
           
 java.lang.reflect.Method resolveMethod(java.lang.Exception exception)
          Find a method to handle the given exception.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_METHOD_FOUND

private static final java.lang.reflect.Method NO_METHOD_FOUND

mappedMethods

private final java.util.Map<java.lang.Class<? extends java.lang.Throwable>,java.lang.reflect.Method> mappedMethods

exceptionLookupCache

private final java.util.Map<java.lang.Class<? extends java.lang.Throwable>,java.lang.reflect.Method> exceptionLookupCache

EXCEPTION_HANDLER_METHODS

public static final ReflectionUtils.MethodFilter EXCEPTION_HANDLER_METHODS
A filter for selecting @ExceptionHandler methods.

Constructor Detail

ExceptionHandlerMethodResolver

public ExceptionHandlerMethodResolver(java.lang.Class<?> handlerType)
A constructor that finds ExceptionHandler methods in a handler.

Parameters:
handlerType - the handler to inspect for exception handler methods.
Throws:
java.lang.IllegalStateException - If an exception type is mapped to two methods.
java.lang.IllegalArgumentException - If an @ExceptionHandler method is not mapped to any exceptions.
Method Detail

init

private void init(java.util.Set<java.lang.reflect.Method> exceptionHandlerMethods)

detectMappedExceptions

private java.util.List<java.lang.Class<? extends java.lang.Throwable>> detectMappedExceptions(java.lang.reflect.Method method)
Detect the exceptions an @ExceptionHandler method is mapped to. If the method @ExceptionHandler annotation doesn't have any, scan the method signature for all arguments of type Throwable.


addExceptionMapping

private void addExceptionMapping(java.lang.Class<? extends java.lang.Throwable> exceptionType,
                                 java.lang.reflect.Method method)

resolveMethod

public java.lang.reflect.Method resolveMethod(java.lang.Exception exception)
Find a method to handle the given exception. If more than one match is found, the best match is selected via ExceptionDepthComparator.

Parameters:
exception - the exception
Returns:
an @ExceptionHandler method, or null

getMappedMethod

private java.lang.reflect.Method getMappedMethod(java.lang.Class<? extends java.lang.Exception> exceptionType)
Return the method mapped to the exception type, or null.