@ExceptionHandler works for inherited method and CGLIB proxies on Portlet controllers as well (SPR-7337)

This commit is contained in:
Juergen Hoeller 2010-07-08 11:45:35 +00:00
parent cb72fe1be2
commit 7e9e8401f7
2 changed files with 26 additions and 5 deletions

View File

@ -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()));

View File

@ -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 {