Browse Source

Polishing

pull/465/head
Juergen Hoeller 12 years ago
parent
commit
4886bf0708
  1. 34
      spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java

34
spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java

@ -33,10 +33,9 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.method.HandlerMethodSelector; import org.springframework.web.method.HandlerMethodSelector;
/** /**
* Discovers {@linkplain ExceptionHandler @ExceptionHandler} methods in a given class * Discovers {@linkplain ExceptionHandler @ExceptionHandler} methods in a given class,
* type, including all super types, and helps to resolve an Exception to the method * including all of its superclasses, and helps to resolve a given {@link Exception}
* its mapped to. Exception mappings are defined through {@code @ExceptionHandler} * to the exception types supported by a given {@link Method}.
* annotation or by looking at the signature of an {@code @ExceptionHandler} method.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 3.1 * @since 3.1
@ -46,12 +45,15 @@ public class ExceptionHandlerMethodResolver {
/** /**
* A filter for selecting {@code @ExceptionHandler} methods. * A filter for selecting {@code @ExceptionHandler} methods.
*/ */
public final static MethodFilter EXCEPTION_HANDLER_METHODS = new MethodFilter() { public static final MethodFilter EXCEPTION_HANDLER_METHODS = new MethodFilter() {
public boolean matches(Method method) { public boolean matches(Method method) {
return (AnnotationUtils.findAnnotation(method, ExceptionHandler.class) != null); return (AnnotationUtils.findAnnotation(method, ExceptionHandler.class) != null);
} }
}; };
/**
* Arbitrary {@link Method} reference, indicating no method found in the cache.
*/
private static final Method NO_METHOD_FOUND = ClassUtils.getMethodIfAvailable(System.class, "currentTimeMillis"); private static final Method NO_METHOD_FOUND = ClassUtils.getMethodIfAvailable(System.class, "currentTimeMillis");
@ -76,8 +78,8 @@ public class ExceptionHandlerMethodResolver {
/** /**
* Extract exception mappings from the {@code @ExceptionHandler} annotation * Extract exception mappings from the {@code @ExceptionHandler} annotation first,
* first and as a fall-back from the method signature. * and then as a fallback from the method signature itself.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private List<Class<? extends Throwable>> detectExceptionMappings(Method method) { private List<Class<? extends Throwable>> detectExceptionMappings(Method method) {
@ -112,36 +114,36 @@ public class ExceptionHandlerMethodResolver {
* Whether the contained type has any exception mappings. * Whether the contained type has any exception mappings.
*/ */
public boolean hasExceptionMappings() { public boolean hasExceptionMappings() {
return (this.mappedMethods.size() > 0); return !this.mappedMethods.isEmpty();
} }
/** /**
* Find a method to handle the given exception. * Find a {@link Method} to handle the given exception.
* Use {@link ExceptionDepthComparator} if more than one match is found. * Use {@link ExceptionDepthComparator} if more than one match is found.
* @param exception the exception * @param exception the exception
* @return a method to handle the exception or {@code null} * @return a Method to handle the exception, or {@code null} if none found
*/ */
public Method resolveMethod(Exception exception) { public Method resolveMethod(Exception exception) {
return resolveMethodByExceptionType(exception.getClass()); return resolveMethodByExceptionType(exception.getClass());
} }
/** /**
* Find a method to handle the given exception type. This can be useful if * Find a {@link Method} to handle the given exception type. This can be
* an Exception instance is not available (example for tools). * useful if an {@link Exception} instance is not available (e.g. for tools).
* @param exceptionType the exception type * @param exceptionType the exception type
* @return a method to handle the exception or {@code null} * @return a Method to handle the exception, or {@code null} if none found
*/ */
public Method resolveMethodByExceptionType(Class<? extends Exception> exceptionType) { public Method resolveMethodByExceptionType(Class<? extends Exception> exceptionType) {
Method method = this.exceptionLookupCache.get(exceptionType); Method method = this.exceptionLookupCache.get(exceptionType);
if (method == null) { if (method == null) {
method = getMappedMethod(exceptionType); method = getMappedMethod(exceptionType);
this.exceptionLookupCache.put(exceptionType, method != null ? method : NO_METHOD_FOUND); this.exceptionLookupCache.put(exceptionType, (method != null ? method : NO_METHOD_FOUND));
} }
return method != NO_METHOD_FOUND ? method : null; return (method != NO_METHOD_FOUND ? method : null);
} }
/** /**
* Return the method mapped to the given exception type or {@code null}. * Return the {@link Method} mapped to the given exception type, or {@code null} if none.
*/ */
private Method getMappedMethod(Class<? extends Exception> exceptionType) { private Method getMappedMethod(Class<? extends Exception> exceptionType) {
List<Class<? extends Throwable>> matches = new ArrayList<Class<? extends Throwable>>(); List<Class<? extends Throwable>> matches = new ArrayList<Class<? extends Throwable>>();

Loading…
Cancel
Save