diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/AnnotationExceptionHandlerMethodResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/AnnotationExceptionHandlerMethodResolver.java index a0800bc02b..bdd5467056 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/AnnotationExceptionHandlerMethodResolver.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/AnnotationExceptionHandlerMethodResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -24,7 +24,7 @@ import java.util.List; import java.util.Map; import org.springframework.core.MethodIntrospector; -import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.messaging.handler.annotation.MessageExceptionHandler; import org.springframework.messaging.handler.invocation.AbstractExceptionHandlerMethodResolver; @@ -35,6 +35,7 @@ import org.springframework.messaging.handler.invocation.AbstractExceptionHandler * or from the method signature as a fallback option. * * @author Rossen Stoyanchev + * @author Juergen Hoeller * @since 4.0 */ public class AnnotationExceptionHandlerMethodResolver extends AbstractExceptionHandlerMethodResolver { @@ -50,7 +51,7 @@ public class AnnotationExceptionHandlerMethodResolver extends AbstractExceptionH private static Map, Method> initExceptionMappings(Class handlerType) { Map methods = MethodIntrospector.selectMethods(handlerType, (MethodIntrospector.MetadataLookup) method -> - AnnotationUtils.findAnnotation(method, MessageExceptionHandler.class)); + AnnotatedElementUtils.findMergedAnnotation(method, MessageExceptionHandler.class)); Map, Method> result = new HashMap<>(); for (Map.Entry entry : methods.entrySet()) { diff --git a/spring-tx/src/main/java/org/springframework/transaction/event/TransactionalEventListenerFactory.java b/spring-tx/src/main/java/org/springframework/transaction/event/TransactionalEventListenerFactory.java index 19d5ab82f8..57aae2b134 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/event/TransactionalEventListenerFactory.java +++ b/spring-tx/src/main/java/org/springframework/transaction/event/TransactionalEventListenerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2018 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. @@ -21,11 +21,11 @@ import java.lang.reflect.Method; import org.springframework.context.ApplicationListener; import org.springframework.context.event.EventListenerFactory; import org.springframework.core.Ordered; -import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.core.annotation.AnnotatedElementUtils; /** * {@link EventListenerFactory} implementation that handles {@link TransactionalEventListener} - * annotated method. + * annotated methods. * * @author Stephane Nicoll * @since 4.2 @@ -47,7 +47,7 @@ public class TransactionalEventListenerFactory implements EventListenerFactory, @Override public boolean supportsMethod(Method method) { - return (AnnotationUtils.findAnnotation(method, TransactionalEventListener.class) != null); + return AnnotatedElementUtils.hasAnnotation(method, TransactionalEventListener.class); } @Override diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java b/spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java index 564b2e55e8..3eb93f88e1 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java @@ -25,7 +25,7 @@ import java.util.Map; import org.springframework.core.ExceptionDepthComparator; import org.springframework.core.MethodIntrospector; -import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ConcurrentReferenceHashMap; @@ -47,7 +47,7 @@ public class ExceptionHandlerMethodResolver { * A filter for selecting {@code @ExceptionHandler} methods. */ public static final MethodFilter EXCEPTION_HANDLER_METHODS = method -> - (AnnotationUtils.findAnnotation(method, ExceptionHandler.class) != null); + AnnotatedElementUtils.hasAnnotation(method, ExceptionHandler.class); private final Map, Method> mappedMethods = new HashMap<>(16); @@ -89,8 +89,8 @@ public class ExceptionHandlerMethodResolver { return result; } - protected void detectAnnotationExceptionMappings(Method method, List> result) { - ExceptionHandler ann = AnnotationUtils.findAnnotation(method, ExceptionHandler.class); + private void detectAnnotationExceptionMappings(Method method, List> result) { + ExceptionHandler ann = AnnotatedElementUtils.findMergedAnnotation(method, ExceptionHandler.class); Assert.state(ann != null, "No ExceptionHandler annotation"); result.addAll(Arrays.asList(ann.value())); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ControllerMethodResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ControllerMethodResolver.java index 2c33943a08..39c766945a 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ControllerMethodResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ControllerMethodResolver.java @@ -32,13 +32,14 @@ import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.MethodIntrospector; import org.springframework.core.ReactiveAdapterRegistry; +import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.annotation.AnnotationAwareOrderComparator; -import org.springframework.core.annotation.AnnotationUtils; import org.springframework.http.codec.HttpMessageReader; import org.springframework.lang.Nullable; import org.springframework.util.Assert; -import org.springframework.util.ReflectionUtils; +import org.springframework.util.ReflectionUtils.MethodFilter; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; @@ -50,8 +51,6 @@ import org.springframework.web.reactive.result.method.InvocableHandlerMethod; import org.springframework.web.reactive.result.method.SyncHandlerMethodArgumentResolver; import org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod; -import static org.springframework.core.MethodIntrospector.selectMethods; - /** * Package-private class to assist {@link RequestMappingHandlerAdapter} with * resolving, initializing, and caching annotated methods declared in @@ -69,8 +68,21 @@ import static org.springframework.core.MethodIntrospector.selectMethods; */ class ControllerMethodResolver { - private static Log logger = LogFactory.getLog(ControllerMethodResolver.class); + /** + * MethodFilter that matches {@link InitBinder @InitBinder} methods. + */ + public static final MethodFilter INIT_BINDER_METHODS = method -> + AnnotatedElementUtils.hasAnnotation(method, InitBinder.class); + /** + * MethodFilter that matches {@link ModelAttribute @ModelAttribute} methods. + */ + public static final MethodFilter MODEL_ATTRIBUTE_METHODS = method -> + (!AnnotatedElementUtils.hasAnnotation(method, RequestMapping.class) && + AnnotatedElementUtils.hasAnnotation(method, ModelAttribute.class)); + + + private static Log logger = LogFactory.getLog(ControllerMethodResolver.class); private final List initBinderResolvers; @@ -212,11 +224,11 @@ class ControllerMethodResolver { for (ControllerAdviceBean bean : beans) { Class beanType = bean.getBeanType(); if (beanType != null) { - Set attrMethods = selectMethods(beanType, ATTRIBUTE_METHODS); + Set attrMethods = MethodIntrospector.selectMethods(beanType, MODEL_ATTRIBUTE_METHODS); if (!attrMethods.isEmpty()) { this.modelAttributeAdviceCache.put(bean, attrMethods); } - Set binderMethods = selectMethods(beanType, BINDER_METHODS); + Set binderMethods = MethodIntrospector.selectMethods(beanType, INIT_BINDER_METHODS); if (!binderMethods.isEmpty()) { this.initBinderAdviceCache.put(bean, binderMethods); } @@ -270,7 +282,8 @@ class ControllerMethodResolver { }); this.initBinderMethodCache - .computeIfAbsent(handlerType, aClass -> selectMethods(handlerType, BINDER_METHODS)) + .computeIfAbsent(handlerType, + clazz -> MethodIntrospector.selectMethods(handlerType, INIT_BINDER_METHODS)) .forEach(method -> { Object bean = handlerMethod.getBean(); result.add(getInitBinderMethod(bean, method)); @@ -302,7 +315,8 @@ class ControllerMethodResolver { }); this.modelAttributeMethodCache - .computeIfAbsent(handlerType, aClass -> selectMethods(handlerType, ATTRIBUTE_METHODS)) + .computeIfAbsent(handlerType, + clazz -> MethodIntrospector.selectMethods(handlerType, MODEL_ATTRIBUTE_METHODS)) .forEach(method -> { Object bean = handlerMethod.getBean(); result.add(createAttributeMethod(bean, method)); @@ -373,14 +387,4 @@ class ControllerMethodResolver { return result; } - - /** Filter for {@link InitBinder @InitBinder} methods. */ - private static final ReflectionUtils.MethodFilter BINDER_METHODS = method -> - AnnotationUtils.findAnnotation(method, InitBinder.class) != null; - - /** Filter for {@link ModelAttribute @ModelAttribute} methods. */ - private static final ReflectionUtils.MethodFilter ATTRIBUTE_METHODS = method -> - (AnnotationUtils.findAnnotation(method, RequestMapping.class) == null) && - (AnnotationUtils.findAnnotation(method, ModelAttribute.class) != null); - } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java index 4247fa08d8..84da9dcdd1 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java @@ -36,8 +36,8 @@ import org.springframework.core.DefaultParameterNameDiscoverer; import org.springframework.core.MethodIntrospector; import org.springframework.core.ParameterNameDiscoverer; import org.springframework.core.ReactiveAdapterRegistry; +import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.annotation.AnnotationAwareOrderComparator; -import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.task.AsyncTaskExecutor; import org.springframework.core.task.SimpleAsyncTaskExecutor; import org.springframework.http.converter.ByteArrayHttpMessageConverter; @@ -114,6 +114,20 @@ import org.springframework.web.util.WebUtils; public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter implements BeanFactoryAware, InitializingBean { + /** + * MethodFilter that matches {@link InitBinder @InitBinder} methods. + */ + public static final MethodFilter INIT_BINDER_METHODS = method -> + AnnotatedElementUtils.hasAnnotation(method, InitBinder.class); + + /** + * MethodFilter that matches {@link ModelAttribute @ModelAttribute} methods. + */ + public static final MethodFilter MODEL_ATTRIBUTE_METHODS = method -> + (!AnnotatedElementUtils.hasAnnotation(method, RequestMapping.class) && + AnnotatedElementUtils.hasAnnotation(method, ModelAttribute.class)); + + @Nullable private List customArgumentResolvers; @@ -1005,18 +1019,4 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter return mav; } - - /** - * MethodFilter that matches {@link InitBinder @InitBinder} methods. - */ - public static final MethodFilter INIT_BINDER_METHODS = method -> - AnnotationUtils.findAnnotation(method, InitBinder.class) != null; - - /** - * MethodFilter that matches {@link ModelAttribute @ModelAttribute} methods. - */ - public static final MethodFilter MODEL_ATTRIBUTE_METHODS = method -> - ((AnnotationUtils.findAnnotation(method, RequestMapping.class) == null) && - (AnnotationUtils.findAnnotation(method, ModelAttribute.class) != null)); - }