private void detectAnnotationExceptionMappings(Method method, List<Class<? extends Throwable>> result) { ExceptionHandler ann = AnnotatedElementUtils.findMergedAnnotation(method, ExceptionHandler.class); Assert.state(ann != null, "No ExceptionHandler annotation"); result.addAll(Arrays.asList(ann.value())); }
private void detectAnnotationExceptionMappings(Method method, List<Class<? extends Throwable>> result) { ExceptionHandler ann = AnnotatedElementUtils.findMergedAnnotation(method, ExceptionHandler.class); Assert.state(ann != null, "No ExceptionHandler annotation"); result.addAll(Arrays.asList(ann.value())); }
@Test public void supportsAllDefaultHandlerExceptionResolverExceptionTypes() throws Exception { Class<ResponseEntityExceptionHandler> clazz = ResponseEntityExceptionHandler.class; Method handleExceptionMethod = clazz.getMethod("handleException", Exception.class, WebRequest.class); ExceptionHandler annotation = handleExceptionMethod.getAnnotation(ExceptionHandler.class); List<Class<?>> exceptionTypes = Arrays.asList(annotation.value()); for (Method method : DefaultHandlerExceptionResolver.class.getDeclaredMethods()) { Class<?>[] paramTypes = method.getParameterTypes(); if (method.getName().startsWith("handle") && (paramTypes.length == 4)) { String name = paramTypes[0].getSimpleName(); assertTrue("@ExceptionHandler is missing " + name, exceptionTypes.contains(paramTypes[0])); } } }
protected Map<Class<? extends Throwable>, ResponseStatus> generateExceptionMapping(Set<Class<?>> classes) { Map<Class<? extends Throwable>, ResponseStatus> result = new HashMap<Class<? extends Throwable>, ResponseStatus>(); log.debug(String.format("Looking for classes with @ControllerAdvice annotation")); for (Class clazz: classes) { ControllerAdvice advice = findAnnotation(clazz, ControllerAdvice.class); if (advice == null) { continue; } log.debug(String.format("%s is annotated as @ControllerAdvice", clazz.getName())); for (Method method: clazz.getMethods()) { ExceptionHandler handler = findAnnotation(method, ExceptionHandler.class); if (handler == null) { log.debug(String.format("@ExceptionHandler is missing on %s method, skipping", method)); continue; } ResponseStatus responseStatus = findAnnotation(method, ResponseStatus.class); if (responseStatus == null) { log.debug(String.format("@ResponseStatus is missing on %s method, skipping", method)); continue; } Class[] exceptionClasses = handler.value(); for (Class exceptionClass: exceptionClasses) { log.debug(String.format("%s will be mapped to %s", exceptionClass, responseStatus)); result.put(exceptionClass, responseStatus); } } } return result; }
protected void detectAnnotationExceptionMappings(Method method, List<Class<? extends Throwable>> result) { ExceptionHandler ann = AnnotationUtils.findAnnotation(method, ExceptionHandler.class); Assert.state(ann != null, "No ExceptionHandler annotation"); result.addAll(Arrays.asList(ann.value())); }
private void detectAnnotationExceptionMappings(Method method, List<Class<? extends Throwable>> result) { ExceptionHandler ann = AnnotatedElementUtils.findMergedAnnotation(method, ExceptionHandler.class); Assert.state(ann != null, "No ExceptionHandler annotation"); result.addAll(Arrays.asList(ann.value())); }
/** * Returns all the exception classes handled by the given method. * <p>The default implementation looks for exceptions in the annotation, * or - if that annotation element is empty - any exceptions listed in the method parameters if the method * is annotated with {@code @ExceptionHandler}. * @param method the method * @return the handled exceptions */ @SuppressWarnings("unchecked") protected List<Class<? extends Throwable>> getHandledExceptions(Method method) { List<Class<? extends Throwable>> result = new ArrayList<Class<? extends Throwable>>(); ExceptionHandler exceptionHandler = AnnotationUtils.findAnnotation(method, ExceptionHandler.class); if (exceptionHandler != null) { if (!ObjectUtils.isEmpty(exceptionHandler.value())) { result.addAll(Arrays.asList(exceptionHandler.value())); } else { for (Class<?> param : method.getParameterTypes()) { if (Throwable.class.isAssignableFrom(param)) { result.add((Class<? extends Throwable>) param); } } } } return result; }
/** * Returns all the exception classes handled by the given method. * <p>Default implementation looks for exceptions in the {@linkplain ExceptionHandler#value() annotation}, * or - if that annotation element is empty - any exceptions listed in the method parameters if the * method is annotated with {@code @ExceptionHandler}. * @param method the method * @return the handled exceptions */ @SuppressWarnings("unchecked") protected List<Class<? extends Throwable>> getHandledExceptions(Method method) { List<Class<? extends Throwable>> result = new ArrayList<Class<? extends Throwable>>(); ExceptionHandler exceptionHandler = AnnotationUtils.findAnnotation(method, ExceptionHandler.class); if (exceptionHandler != null) { if (!ObjectUtils.isEmpty(exceptionHandler.value())) { result.addAll(Arrays.asList(exceptionHandler.value())); } else { for (Class<?> param : method.getParameterTypes()) { if (Throwable.class.isAssignableFrom(param)) { result.add((Class<? extends Throwable>) param); } } } } return result; }
/** * Returns all the exception classes handled by the given method. * <p>Default implementation looks for exceptions in the {@linkplain ExceptionHandler#value() annotation}, * or - if that annotation element is empty - any exceptions listed in the method parameters if the * method is annotated with {@code @ExceptionHandler}. * @param method the method * @return the handled exceptions */ @SuppressWarnings("unchecked") protected List<Class<? extends Throwable>> getHandledExceptions(Method method) { List<Class<? extends Throwable>> result = new ArrayList<Class<? extends Throwable>>(); ExceptionHandler exceptionHandler = AnnotationUtils.findAnnotation(method, ExceptionHandler.class); if (exceptionHandler != null) { if (!ObjectUtils.isEmpty(exceptionHandler.value())) { result.addAll(Arrays.asList(exceptionHandler.value())); } else { for (Class<?> param : method.getParameterTypes()) { if (Throwable.class.isAssignableFrom(param)) { result.add((Class<? extends Throwable>) param); } } } } return result; }