Search in sources :

Example 1 with MessageExceptionHandler

use of org.springframework.messaging.handler.annotation.MessageExceptionHandler in project spring-framework by spring-projects.

the class AnnotationExceptionHandlerMethodResolver method initExceptionMappings.

private static Map<Class<? extends Throwable>, Method> initExceptionMappings(Class<?> handlerType) {
    Map<Method, MessageExceptionHandler> methods = MethodIntrospector.selectMethods(handlerType, new MethodIntrospector.MetadataLookup<MessageExceptionHandler>() {

        @Override
        public MessageExceptionHandler inspect(Method method) {
            return AnnotationUtils.findAnnotation(method, MessageExceptionHandler.class);
        }
    });
    Map<Class<? extends Throwable>, Method> result = new HashMap<>();
    for (Map.Entry<Method, MessageExceptionHandler> entry : methods.entrySet()) {
        Method method = entry.getKey();
        List<Class<? extends Throwable>> exceptionTypes = new ArrayList<>();
        exceptionTypes.addAll(Arrays.asList(entry.getValue().value()));
        if (exceptionTypes.isEmpty()) {
            exceptionTypes.addAll(getExceptionsFromMethodSignature(method));
        }
        for (Class<? extends Throwable> exceptionType : exceptionTypes) {
            Method oldMethod = result.put(exceptionType, method);
            if (oldMethod != null && !oldMethod.equals(method)) {
                throw new IllegalStateException("Ambiguous @ExceptionHandler method mapped for [" + exceptionType + "]: {" + oldMethod + ", " + method + "}");
            }
        }
    }
    return result;
}
Also used : MethodIntrospector(org.springframework.core.MethodIntrospector) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MessageExceptionHandler(org.springframework.messaging.handler.annotation.MessageExceptionHandler) Method(java.lang.reflect.Method) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MethodIntrospector (org.springframework.core.MethodIntrospector)1 MessageExceptionHandler (org.springframework.messaging.handler.annotation.MessageExceptionHandler)1