Rossen Stoyanchev
13 years ago
6 changed files with 412 additions and 378 deletions
@ -0,0 +1,153 @@
@@ -0,0 +1,153 @@
|
||||
/* |
||||
* Copyright 2002-2011 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* 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
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.method.annotation; |
||||
|
||||
import java.lang.reflect.Method; |
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
import java.util.concurrent.ConcurrentHashMap; |
||||
|
||||
import org.springframework.core.ExceptionDepthComparator; |
||||
import org.springframework.core.annotation.AnnotationUtils; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.ClassUtils; |
||||
import org.springframework.util.ReflectionUtils.MethodFilter; |
||||
import org.springframework.web.bind.annotation.ExceptionHandler; |
||||
import org.springframework.web.method.HandlerMethodSelector; |
||||
|
||||
/** |
||||
* Given a set of @{@link ExceptionHandler} methods at initialization, finds |
||||
* the best matching method mapped to an exception at runtime. |
||||
* |
||||
* <p>Exception mappings are extracted from the method @{@link ExceptionHandler} |
||||
* annotation or by looking for {@link Throwable} method arguments. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 3.1 |
||||
*/ |
||||
public class ExceptionHandlerMethodResolver { |
||||
|
||||
private static final Method NO_METHOD_FOUND = ClassUtils.getMethodIfAvailable(System.class, "currentTimeMillis"); |
||||
|
||||
private final Map<Class<? extends Throwable>, Method> mappedMethods = |
||||
new ConcurrentHashMap<Class<? extends Throwable>, Method>(); |
||||
|
||||
private final Map<Class<? extends Throwable>, Method> exceptionLookupCache = |
||||
new ConcurrentHashMap<Class<? extends Throwable>, Method>(); |
||||
|
||||
/** |
||||
* A constructor that finds {@link ExceptionHandler} methods in a handler. |
||||
* @param handlerType the handler to inspect for exception handler methods. |
||||
* @throws IllegalStateException |
||||
* If an exception type is mapped to two methods. |
||||
* @throws IllegalArgumentException |
||||
* If an @{@link ExceptionHandler} method is not mapped to any exceptions. |
||||
*/ |
||||
public ExceptionHandlerMethodResolver(Class<?> handlerType) { |
||||
init(HandlerMethodSelector.selectMethods(handlerType, EXCEPTION_HANDLER_METHODS)); |
||||
} |
||||
|
||||
private void init(Set<Method> exceptionHandlerMethods) { |
||||
for (Method method : exceptionHandlerMethods) { |
||||
for (Class<? extends Throwable> exceptionType : detectMappedExceptions(method)) { |
||||
addExceptionMapping(exceptionType, method); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Detect the exceptions an @{@link ExceptionHandler} method is mapped to. |
||||
* If the method @{@link ExceptionHandler} annotation doesn't have any, |
||||
* scan the method signature for all arguments of type {@link Throwable}. |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
private List<Class<? extends Throwable>> detectMappedExceptions(Method method) { |
||||
List<Class<? extends Throwable>> result = new ArrayList<Class<? extends Throwable>>(); |
||||
ExceptionHandler annotation = AnnotationUtils.findAnnotation(method, ExceptionHandler.class); |
||||
if (annotation != null) { |
||||
result.addAll(Arrays.asList(annotation.value())); |
||||
} |
||||
if (result.isEmpty()) { |
||||
for (Class<?> paramType : method.getParameterTypes()) { |
||||
if (Throwable.class.isAssignableFrom(paramType)) { |
||||
result.add((Class<? extends Throwable>) paramType); |
||||
} |
||||
} |
||||
} |
||||
Assert.notEmpty(result, "No exception types mapped to {" + method + "}"); |
||||
return result; |
||||
} |
||||
|
||||
private void addExceptionMapping(Class<? extends Throwable> exceptionType, Method method) { |
||||
Method oldMethod = this.mappedMethods.put(exceptionType, method); |
||||
if (oldMethod != null && !oldMethod.equals(method)) { |
||||
throw new IllegalStateException( |
||||
"Ambiguous @ExceptionHandler method mapped for [" + exceptionType + "]: {" + |
||||
oldMethod + ", " + method + "}."); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Find a method to handle the given exception. If more than one match is |
||||
* found, the best match is selected via {@link ExceptionDepthComparator}. |
||||
* @param exception the exception |
||||
* @return an @{@link ExceptionHandler} method, or {@code null} |
||||
*/ |
||||
public Method resolveMethod(Exception exception) { |
||||
Class<? extends Exception> exceptionType = exception.getClass(); |
||||
Method method = this.exceptionLookupCache.get(exceptionType); |
||||
if (method == null) { |
||||
method = getMappedMethod(exceptionType); |
||||
this.exceptionLookupCache.put(exceptionType, method != null ? method : NO_METHOD_FOUND); |
||||
} |
||||
return method != NO_METHOD_FOUND ? method : null; |
||||
} |
||||
|
||||
/** |
||||
* Return the method mapped to the exception type, or {@code null}. |
||||
*/ |
||||
private Method getMappedMethod(Class<? extends Exception> exceptionType) { |
||||
List<Class<? extends Throwable>> matches = new ArrayList<Class<? extends Throwable>>(); |
||||
for(Class<? extends Throwable> mappedException : this.mappedMethods.keySet()) { |
||||
if (mappedException.isAssignableFrom(exceptionType)) { |
||||
matches.add(mappedException); |
||||
} |
||||
} |
||||
if (!matches.isEmpty()) { |
||||
Collections.sort(matches, new ExceptionDepthComparator(exceptionType)); |
||||
return mappedMethods.get(matches.get(0)); |
||||
} |
||||
else { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* A filter for selecting @{@link ExceptionHandler} methods. |
||||
*/ |
||||
public final static MethodFilter EXCEPTION_HANDLER_METHODS = new MethodFilter() { |
||||
|
||||
public boolean matches(Method method) { |
||||
return AnnotationUtils.findAnnotation(method, ExceptionHandler.class) != null; |
||||
} |
||||
}; |
||||
|
||||
} |
@ -1,163 +0,0 @@
@@ -1,163 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2011 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* 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
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.method.annotation; |
||||
|
||||
import java.lang.reflect.Method; |
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
import java.util.concurrent.ConcurrentHashMap; |
||||
|
||||
import org.springframework.core.ExceptionDepthComparator; |
||||
import org.springframework.core.annotation.AnnotationUtils; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.ClassUtils; |
||||
import org.springframework.web.bind.annotation.ExceptionHandler; |
||||
|
||||
/** |
||||
* Extracts and stores method-to-exception type mappings from a set of {@link ExceptionHandler}-annotated methods. |
||||
* Subsequently {@link #getMethod(Exception)} can be used to matches an {@link Exception} to a method. |
||||
* |
||||
* <p>Method-to-exception type mappings are usually derived from a method's {@link ExceptionHandler} annotation value. |
||||
* The method argument list may also be checked for {@link Throwable} types if that's empty. Exception types can be |
||||
* mapped to one method only. |
||||
* |
||||
* <p>When multiple exception types match a given exception, the best matching exception type is selected by sorting |
||||
* the list of matches with {@link ExceptionDepthComparator}. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 3.1 |
||||
*/ |
||||
public class ExceptionMethodMapping { |
||||
|
||||
protected static final Method NO_METHOD_FOUND = ClassUtils.getMethodIfAvailable(System.class, "currentTimeMillis"); |
||||
|
||||
private final Map<Class<? extends Throwable>, Method> mappedExceptionTypes = |
||||
new HashMap<Class<? extends Throwable>, Method>(); |
||||
|
||||
private final Map<Class<? extends Throwable>, Method> resolvedExceptionTypes = |
||||
new ConcurrentHashMap<Class<? extends Throwable>, Method>(); |
||||
|
||||
/** |
||||
* Creates an {@link ExceptionMethodMapping} instance from a set of {@link ExceptionHandler} methods. |
||||
* <p>While any {@link ExceptionHandler} methods can be provided, it is expected that the exception types |
||||
* handled by any one method do not overlap with the exception types handled by any other method. |
||||
* If two methods map to the same exception type, an exception is raised. |
||||
* @param methods the {@link ExceptionHandler}-annotated methods to add to the mappings |
||||
*/ |
||||
public ExceptionMethodMapping(Set<Method> methods) { |
||||
initExceptionMap(methods); |
||||
} |
||||
|
||||
/** |
||||
* Examines the provided methods and populates mapped exception types. |
||||
*/ |
||||
private void initExceptionMap(Set<Method> methods) { |
||||
for (Method method : methods) { |
||||
for (Class<? extends Throwable> exceptionType : getMappedExceptionTypes(method)) { |
||||
Method prevMethod = mappedExceptionTypes.put(exceptionType, method); |
||||
|
||||
if (prevMethod != null && !prevMethod.equals(method)) { |
||||
throw new IllegalStateException( |
||||
"Ambiguous exception handler mapped for [" + exceptionType + "]: {" + |
||||
prevMethod + ", " + method + "}."); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Derive the list of exception types mapped to the given method in one of the following ways: |
||||
* <ol> |
||||
* <li>The {@link ExceptionHandler} annotation value |
||||
* <li>{@link Throwable} types that appear in the method parameter list |
||||
* </ol> |
||||
* @param method the method to derive mapped exception types for |
||||
* @return the list of exception types the method is mapped to, or an empty list |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
protected List<Class<? extends Throwable>> getMappedExceptionTypes(Method method) { |
||||
ExceptionHandler annotation = AnnotationUtils.findAnnotation(method, ExceptionHandler.class); |
||||
if (annotation.value().length != 0) { |
||||
return Arrays.asList(annotation.value()); |
||||
} |
||||
else { |
||||
List<Class<? extends Throwable>> result = new ArrayList<Class<? extends Throwable>>(); |
||||
for (Class<?> paramType : method.getParameterTypes()) { |
||||
if (Throwable.class.isAssignableFrom(paramType)) { |
||||
result.add((Class<? extends Throwable>) paramType); |
||||
} |
||||
} |
||||
Assert.notEmpty(result, "No exception types mapped to {" + method + "}"); |
||||
return result; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Get the {@link ExceptionHandler} method that matches the type of the provided {@link Exception}. |
||||
* In case of multiple matches, the best match is selected with {@link ExceptionDepthComparator}. |
||||
* @param exception the exception to find a matching {@link ExceptionHandler} method for |
||||
* @return the mapped method, or {@code null} if none |
||||
*/ |
||||
public Method getMethod(Exception exception) { |
||||
Class<? extends Exception> exceptionType = exception.getClass(); |
||||
Method method = resolvedExceptionTypes.get(exceptionType); |
||||
if (method == null) { |
||||
method = resolveExceptionType(exceptionType); |
||||
resolvedExceptionTypes.put(exceptionType, method); |
||||
} |
||||
return (method != NO_METHOD_FOUND) ? method : null; |
||||
} |
||||
|
||||
/** |
||||
* Resolve the given exception type by iterating mapped exception types. |
||||
* Uses {@link #getBestMatchingExceptionType(List, Class)} to select the best match. |
||||
* @param exceptionType the exception type to resolve |
||||
* @return the best matching method, or {@link ExceptionMethodMapping#NO_METHOD_FOUND} |
||||
*/ |
||||
protected final Method resolveExceptionType(Class<? extends Exception> exceptionType) { |
||||
List<Class<? extends Throwable>> matches = new ArrayList<Class<? extends Throwable>>(); |
||||
for(Class<? extends Throwable> mappedExceptionType : mappedExceptionTypes.keySet()) { |
||||
if (mappedExceptionType.isAssignableFrom(exceptionType)) { |
||||
matches.add(mappedExceptionType); |
||||
} |
||||
} |
||||
if (matches.isEmpty()) { |
||||
return NO_METHOD_FOUND; |
||||
} |
||||
else { |
||||
return mappedExceptionTypes.get(getBestMatchingExceptionType(matches, exceptionType)); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Select the best match from the given list of exception types. |
||||
*/ |
||||
protected Class<? extends Throwable> getBestMatchingExceptionType(List<Class<? extends Throwable>> exceptionTypes, |
||||
Class<? extends Exception> exceptionType) { |
||||
Assert.isTrue(exceptionTypes.size() > 0, "No exception types to select from!"); |
||||
if (exceptionTypes.size() > 1) { |
||||
Collections.sort(exceptionTypes, new ExceptionDepthComparator(exceptionType)); |
||||
} |
||||
return exceptionTypes.get(0); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,148 @@
@@ -0,0 +1,148 @@
|
||||
/* |
||||
* Copyright 2002-2011 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* 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
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.method.annotation; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
import static org.junit.Assert.assertNull; |
||||
|
||||
import java.io.FileNotFoundException; |
||||
import java.io.IOException; |
||||
import java.net.BindException; |
||||
import java.net.SocketException; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
|
||||
import org.junit.Test; |
||||
import org.springframework.stereotype.Controller; |
||||
import org.springframework.util.ClassUtils; |
||||
import org.springframework.web.bind.annotation.ExceptionHandler; |
||||
|
||||
/** |
||||
* Test fixture for {@link ExceptionHandlerMethodResolver} tests. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
*/ |
||||
public class ExceptionHandlerMethodResolverTests { |
||||
|
||||
@Test |
||||
public void resolveMethodFromAnnotation() { |
||||
ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(ExceptionController.class); |
||||
IOException exception = new IOException(); |
||||
assertEquals("handleIOException", resolver.resolveMethod(exception).getName()); |
||||
} |
||||
|
||||
@Test |
||||
public void resolveMethodFromArgument() { |
||||
ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(ExceptionController.class); |
||||
IllegalArgumentException exception = new IllegalArgumentException(); |
||||
assertEquals("handleIllegalArgumentException", resolver.resolveMethod(exception).getName()); |
||||
} |
||||
|
||||
@Test |
||||
public void resolveMethodExceptionSubType() { |
||||
ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(ExceptionController.class); |
||||
IOException ioException = new FileNotFoundException(); |
||||
assertEquals("handleIOException", resolver.resolveMethod(ioException).getName()); |
||||
SocketException bindException = new BindException(); |
||||
assertEquals("handleSocketException", resolver.resolveMethod(bindException).getName()); |
||||
} |
||||
|
||||
@Test |
||||
public void resolveMethodBestMatch() { |
||||
ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(ExceptionController.class); |
||||
SocketException exception = new SocketException(); |
||||
assertEquals("handleSocketException", resolver.resolveMethod(exception).getName()); |
||||
} |
||||
|
||||
@Test |
||||
public void resolveMethodNoMatch() { |
||||
ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(ExceptionController.class); |
||||
Exception exception = new Exception(); |
||||
assertNull("1st lookup", resolver.resolveMethod(exception)); |
||||
assertNull("2nd lookup from cache", resolver.resolveMethod(exception)); |
||||
} |
||||
|
||||
@Test |
||||
public void resolveMethodInherited() { |
||||
ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(InheritedController.class); |
||||
IOException exception = new IOException(); |
||||
assertEquals("handleIOException", resolver.resolveMethod(exception).getName()); |
||||
} |
||||
|
||||
@Test(expected = IllegalStateException.class) |
||||
public void ambiguousExceptionMapping() { |
||||
new ExceptionHandlerMethodResolver(AmbiguousController.class); |
||||
} |
||||
|
||||
@Test(expected = IllegalArgumentException.class) |
||||
public void noExceptionMapping() { |
||||
new ExceptionHandlerMethodResolver(NoExceptionController.class); |
||||
} |
||||
|
||||
@Controller |
||||
static class ExceptionController { |
||||
|
||||
public void handle() {} |
||||
|
||||
@ExceptionHandler(IOException.class) |
||||
public void handleIOException() { |
||||
} |
||||
|
||||
@ExceptionHandler(SocketException.class) |
||||
public void handleSocketException() { |
||||
} |
||||
|
||||
@ExceptionHandler |
||||
public void handleIllegalArgumentException(IllegalArgumentException exception) { |
||||
} |
||||
} |
||||
|
||||
@Controller |
||||
static class InheritedController extends ExceptionController { |
||||
|
||||
@Override |
||||
public void handleIOException() { |
||||
} |
||||
} |
||||
|
||||
@Controller |
||||
static class AmbiguousController { |
||||
|
||||
public void handle() {} |
||||
|
||||
@ExceptionHandler({BindException.class, IllegalArgumentException.class}) |
||||
public String handle1(Exception ex, HttpServletRequest request, HttpServletResponse response) |
||||
throws IOException { |
||||
return ClassUtils.getShortName(ex.getClass()); |
||||
} |
||||
|
||||
@ExceptionHandler |
||||
public String handle2(IllegalArgumentException ex) { |
||||
return ClassUtils.getShortName(ex.getClass()); |
||||
} |
||||
} |
||||
|
||||
@Controller |
||||
static class NoExceptionController { |
||||
|
||||
@ExceptionHandler |
||||
public void handle() { |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue