Polishing
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@@ -33,25 +33,28 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.method.HandlerMethodSelector;
|
||||
|
||||
/**
|
||||
* Discovers annotated exception handling methods in a given class type, including all
|
||||
* super types, and helps to resolve an Exception to a method that can handle it. The
|
||||
* exception types supported by a given method can also be discovered from the method
|
||||
* signature.
|
||||
* Discovers {@linkplain ExceptionHandler @ExceptionHandler} methods in a given class,
|
||||
* including all of its superclasses, and helps to resolve a given {@link Exception}
|
||||
* to the exception types supported by a given {@link Method}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 3.1
|
||||
*/
|
||||
public class ExceptionHandlerMethodResolver {
|
||||
|
||||
/** A filter for selecting annotated exception handling methods. */
|
||||
public final static MethodFilter EXCEPTION_HANDLER_METHODS = new MethodFilter() {
|
||||
|
||||
/**
|
||||
* A filter for selecting {@code @ExceptionHandler} methods.
|
||||
*/
|
||||
public static final MethodFilter EXCEPTION_HANDLER_METHODS = new MethodFilter() {
|
||||
@Override
|
||||
public boolean matches(Method method) {
|
||||
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");
|
||||
|
||||
|
||||
@@ -116,26 +119,26 @@ public class ExceptionHandlerMethodResolver {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @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) {
|
||||
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).
|
||||
* Find a {@link Method} to handle the given exception type. This can be
|
||||
* useful if an {@link Exception} instance is not available (e.g. for tools).
|
||||
* @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) {
|
||||
Method method = this.exceptionLookupCache.get(exceptionType);
|
||||
if (method == null) {
|
||||
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;
|
||||
}
|
||||
@@ -152,7 +155,7 @@ public class ExceptionHandlerMethodResolver {
|
||||
}
|
||||
if (!matches.isEmpty()) {
|
||||
Collections.sort(matches, new ExceptionDepthComparator(exceptionType));
|
||||
return mappedMethods.get(matches.get(0));
|
||||
return this.mappedMethods.get(matches.get(0));
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user