@ExceptionHandler works for inherited method and CGLIB proxies on Portlet controllers as well (SPR-7337)
This commit is contained in:
parent
cb72fe1be2
commit
7e9e8401f7
|
|
@ -44,6 +44,7 @@ import javax.portlet.WindowState;
|
||||||
import org.springframework.core.ExceptionDepthComparator;
|
import org.springframework.core.ExceptionDepthComparator;
|
||||||
import org.springframework.core.GenericTypeResolver;
|
import org.springframework.core.GenericTypeResolver;
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
|
import org.springframework.core.annotation.AnnotationUtils;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
@ -136,10 +137,11 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Method oldMappedMethod = resolverMethods.get(handledException);
|
Method oldMappedMethod = resolverMethods.get(handledException);
|
||||||
throw new IllegalStateException(
|
if (!oldMappedMethod.equals(method)) {
|
||||||
"Ambiguous exception handler mapped for " + handledException + "]: {" +
|
throw new IllegalStateException(
|
||||||
oldMappedMethod + ", " + method + "}.");
|
"Ambiguous exception handler mapped for " + handledException + "]: {" +
|
||||||
|
oldMappedMethod + ", " + method + "}.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -160,7 +162,7 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
protected List<Class<? extends Throwable>> getHandledExceptions(Method method) {
|
protected List<Class<? extends Throwable>> getHandledExceptions(Method method) {
|
||||||
List<Class<? extends Throwable>> result = new ArrayList<Class<? extends Throwable>>();
|
List<Class<? extends Throwable>> result = new ArrayList<Class<? extends Throwable>>();
|
||||||
ExceptionHandler exceptionHandler = method.getAnnotation(ExceptionHandler.class);
|
ExceptionHandler exceptionHandler = AnnotationUtils.findAnnotation(method, ExceptionHandler.class);
|
||||||
if (exceptionHandler != null) {
|
if (exceptionHandler != null) {
|
||||||
if (!ObjectUtils.isEmpty(exceptionHandler.value())) {
|
if (!ObjectUtils.isEmpty(exceptionHandler.value())) {
|
||||||
result.addAll(Arrays.asList(exceptionHandler.value()));
|
result.addAll(Arrays.asList(exceptionHandler.value()));
|
||||||
|
|
|
||||||
|
|
@ -90,6 +90,15 @@ public class AnnotationMethodHandlerExceptionResolverTests {
|
||||||
assertEquals("Invalid view name returned", "Y:BindException", mav.getViewName());
|
assertEquals("Invalid view name returned", "Y:BindException", mav.getViewName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void inherited() {
|
||||||
|
IOException ex = new IOException();
|
||||||
|
InheritedController controller = new InheritedController();
|
||||||
|
ModelAndView mav = exceptionResolver.resolveException(request, response, controller, ex);
|
||||||
|
assertNotNull("No ModelAndView returned", mav);
|
||||||
|
assertEquals("Invalid view name returned", "GenericError", mav.getViewName());
|
||||||
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalStateException.class)
|
@Test(expected = IllegalStateException.class)
|
||||||
public void ambiguous() {
|
public void ambiguous() {
|
||||||
IllegalArgumentException ex = new IllegalArgumentException();
|
IllegalArgumentException ex = new IllegalArgumentException();
|
||||||
|
|
@ -119,6 +128,16 @@ public class AnnotationMethodHandlerExceptionResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
private static class InheritedController extends SimpleController {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String handleIOException(IOException ex, PortletRequest request) {
|
||||||
|
return "GenericError";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
private static class AmbiguousController {
|
private static class AmbiguousController {
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue