diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/BeanFactoryAspectInstanceFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/BeanFactoryAspectInstanceFactory.java index 855af77216..1ca4bc8a28 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/BeanFactoryAspectInstanceFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/BeanFactoryAspectInstanceFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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. @@ -19,6 +19,7 @@ package org.springframework.aop.aspectj.annotation; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.core.Ordered; +import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.Order; import org.springframework.util.ClassUtils; @@ -109,7 +110,7 @@ public class BeanFactoryAspectInstanceFactory implements MetadataAwareAspectInst if (Ordered.class.isAssignableFrom(type) && this.beanFactory.isSingleton(this.name)) { return ((Ordered) this.beanFactory.getBean(this.name)).getOrder(); } - Order order = type.getAnnotation(Order.class); + Order order = AnnotationUtils.findAnnotation(type, Order.class); if (order != null) { return order.value(); } diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SimpleMetadataAwareAspectInstanceFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SimpleMetadataAwareAspectInstanceFactory.java index 70ff5e6532..8f281a3e3a 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SimpleMetadataAwareAspectInstanceFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SimpleMetadataAwareAspectInstanceFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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. @@ -18,6 +18,7 @@ package org.springframework.aop.aspectj.annotation; import org.springframework.aop.aspectj.SimpleAspectInstanceFactory; import org.springframework.core.Ordered; +import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.Order; /** @@ -59,7 +60,7 @@ public class SimpleMetadataAwareAspectInstanceFactory extends SimpleAspectInstan */ @Override protected int getOrderForAspectClass(Class aspectClass) { - Order order = aspectClass.getAnnotation(Order.class); + Order order = AnnotationUtils.findAnnotation(aspectClass, Order.class); if (order != null) { return order.value(); } diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SingletonMetadataAwareAspectInstanceFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SingletonMetadataAwareAspectInstanceFactory.java index 31f1e18f27..c2025fec6d 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SingletonMetadataAwareAspectInstanceFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SingletonMetadataAwareAspectInstanceFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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. @@ -18,6 +18,7 @@ package org.springframework.aop.aspectj.annotation; import org.springframework.aop.aspectj.SingletonAspectInstanceFactory; import org.springframework.core.Ordered; +import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.Order; /** @@ -60,7 +61,7 @@ public class SingletonMetadataAwareAspectInstanceFactory extends SingletonAspect */ @Override protected int getOrderForAspectClass(Class aspectClass) { - Order order = aspectClass.getAnnotation(Order.class); + Order order = AnnotationUtils.findAnnotation(aspectClass, Order.class); if (order != null) { return order.value(); } diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAwareOrderComparator.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAwareOrderComparator.java index c9d1fe47c5..19ba5fec9a 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAwareOrderComparator.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAwareOrderComparator.java @@ -51,7 +51,7 @@ public class AnnotationAwareOrderComparator extends OrderComparator { } if (obj != null) { Class clazz = (obj instanceof Class ? (Class) obj : obj.getClass()); - Order order = clazz.getAnnotation(Order.class); + Order order = AnnotationUtils.findAnnotation(clazz, Order.class); if (order != null) { return order.value(); } diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java index e177856e82..46847d0823 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java @@ -19,7 +19,6 @@ package org.springframework.core.annotation; import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Method; - import java.util.List; import java.util.Map; import java.util.WeakHashMap; @@ -121,24 +120,24 @@ public abstract class AnnotationUtils { */ public static A findAnnotation(Method method, Class annotationType) { A annotation = getAnnotation(method, annotationType); - Class cl = method.getDeclaringClass(); + Class clazz = method.getDeclaringClass(); if (annotation == null) { - annotation = searchOnInterfaces(method, annotationType, cl.getInterfaces()); + annotation = searchOnInterfaces(method, annotationType, clazz.getInterfaces()); } while (annotation == null) { - cl = cl.getSuperclass(); - if (cl == null || cl == Object.class) { + clazz = clazz.getSuperclass(); + if (clazz == null || clazz.equals(Object.class)) { break; } try { - Method equivalentMethod = cl.getDeclaredMethod(method.getName(), method.getParameterTypes()); + Method equivalentMethod = clazz.getDeclaredMethod(method.getName(), method.getParameterTypes()); annotation = getAnnotation(equivalentMethod, annotationType); } catch (NoSuchMethodException ex) { // No equivalent method found } if (annotation == null) { - annotation = searchOnInterfaces(method, annotationType, cl.getInterfaces()); + annotation = searchOnInterfaces(method, annotationType, clazz.getInterfaces()); } } return annotation; @@ -217,7 +216,7 @@ public abstract class AnnotationUtils { } } Class superClass = clazz.getSuperclass(); - if (superClass == null || superClass == Object.class) { + if (superClass == null || superClass.equals(Object.class)) { return null; } return findAnnotation(superClass, annotationType); @@ -273,25 +272,22 @@ public abstract class AnnotationUtils { * @return the first {@link Class} in the inheritance hierarchy of the specified * {@code clazz} which declares an annotation of at least one of the specified * {@code annotationTypes}, or {@code null} if not found + * @since 3.2.2 * @see Class#isAnnotationPresent(Class) * @see Class#getDeclaredAnnotations() * @see #findAnnotationDeclaringClass(Class, Class) * @see #isAnnotationDeclaredLocally(Class, Class) - * @since 3.2.2 */ - public static Class findAnnotationDeclaringClassForTypes(List> annotationTypes, - Class clazz) { + public static Class findAnnotationDeclaringClassForTypes(List> annotationTypes, Class clazz) { Assert.notEmpty(annotationTypes, "The list of annotation types must not be empty"); if (clazz == null || clazz.equals(Object.class)) { return null; } - for (Class annotationType : annotationTypes) { if (isAnnotationDeclaredLocally(annotationType, clazz)) { return clazz; } } - return findAnnotationDeclaringClassForTypes(annotationTypes, clazz.getSuperclass()); } diff --git a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationAwareOrderComparatorTests.java b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationAwareOrderComparatorTests.java index 47634bb760..df65665564 100644 --- a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationAwareOrderComparatorTests.java +++ b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationAwareOrderComparatorTests.java @@ -45,6 +45,16 @@ public class AnnotationAwareOrderComparatorTests { assertTrue(list.get(1) instanceof B); } + @Test + public void sortInstancesWithSubclass() { + List list = new ArrayList<>(); + list.add(new B()); + list.add(new C()); + AnnotationAwareOrderComparator.sort(list); + assertTrue(list.get(0) instanceof C); + assertTrue(list.get(1) instanceof B); + } + @Test public void sortClasses() { List list = new ArrayList<>(); @@ -55,6 +65,16 @@ public class AnnotationAwareOrderComparatorTests { assertEquals(B.class, list.get(1)); } + @Test + public void sortClassesWithSubclass() { + List list = new ArrayList<>(); + list.add(B.class); + list.add(C.class); + AnnotationAwareOrderComparator.sort(list); + assertEquals(C.class, list.get(0)); + assertEquals(B.class, list.get(1)); + } + @Order(1) private static class A { @@ -64,4 +84,7 @@ public class AnnotationAwareOrderComparatorTests { private static class B { } + private static class C extends A { + } + }