Skip to content

Commit 814d24e

Browse files
committed
Consistent detection of Order annotation in superclasses and interfaces
Issue: SPR-10514
1 parent 16548d2 commit 814d24e

File tree

6 files changed

+42
-20
lines changed

6 files changed

+42
-20
lines changed

spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/BeanFactoryAspectInstanceFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import org.springframework.beans.factory.BeanFactory;
2020
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
2121
import org.springframework.core.Ordered;
22+
import org.springframework.core.annotation.AnnotationUtils;
2223
import org.springframework.core.annotation.Order;
2324
import org.springframework.util.ClassUtils;
2425

@@ -109,7 +110,7 @@ public int getOrder() {
109110
if (Ordered.class.isAssignableFrom(type) && this.beanFactory.isSingleton(this.name)) {
110111
return ((Ordered) this.beanFactory.getBean(this.name)).getOrder();
111112
}
112-
Order order = type.getAnnotation(Order.class);
113+
Order order = AnnotationUtils.findAnnotation(type, Order.class);
113114
if (order != null) {
114115
return order.value();
115116
}

spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SimpleMetadataAwareAspectInstanceFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import org.springframework.aop.aspectj.SimpleAspectInstanceFactory;
2020
import org.springframework.core.Ordered;
21+
import org.springframework.core.annotation.AnnotationUtils;
2122
import org.springframework.core.annotation.Order;
2223

2324
/**
@@ -59,7 +60,7 @@ public final AspectMetadata getAspectMetadata() {
5960
*/
6061
@Override
6162
protected int getOrderForAspectClass(Class<?> aspectClass) {
62-
Order order = aspectClass.getAnnotation(Order.class);
63+
Order order = AnnotationUtils.findAnnotation(aspectClass, Order.class);
6364
if (order != null) {
6465
return order.value();
6566
}

spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SingletonMetadataAwareAspectInstanceFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import org.springframework.aop.aspectj.SingletonAspectInstanceFactory;
2020
import org.springframework.core.Ordered;
21+
import org.springframework.core.annotation.AnnotationUtils;
2122
import org.springframework.core.annotation.Order;
2223

2324
/**
@@ -60,7 +61,7 @@ public final AspectMetadata getAspectMetadata() {
6061
*/
6162
@Override
6263
protected int getOrderForAspectClass(Class<?> aspectClass) {
63-
Order order = aspectClass.getAnnotation(Order.class);
64+
Order order = AnnotationUtils.findAnnotation(aspectClass, Order.class);
6465
if (order != null) {
6566
return order.value();
6667
}

spring-core/src/main/java/org/springframework/core/annotation/AnnotationAwareOrderComparator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protected int getOrder(Object obj) {
5151
}
5252
if (obj != null) {
5353
Class<?> clazz = (obj instanceof Class ? (Class) obj : obj.getClass());
54-
Order order = clazz.getAnnotation(Order.class);
54+
Order order = AnnotationUtils.findAnnotation(clazz, Order.class);
5555
if (order != null) {
5656
return order.value();
5757
}

spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.lang.annotation.Annotation;
2020
import java.lang.reflect.AnnotatedElement;
2121
import java.lang.reflect.Method;
22-
2322
import java.util.List;
2423
import java.util.Map;
2524
import java.util.WeakHashMap;
@@ -121,24 +120,24 @@ public static <A extends Annotation> A getAnnotation(Method method, Class<A> ann
121120
*/
122121
public static <A extends Annotation> A findAnnotation(Method method, Class<A> annotationType) {
123122
A annotation = getAnnotation(method, annotationType);
124-
Class<?> cl = method.getDeclaringClass();
123+
Class<?> clazz = method.getDeclaringClass();
125124
if (annotation == null) {
126-
annotation = searchOnInterfaces(method, annotationType, cl.getInterfaces());
125+
annotation = searchOnInterfaces(method, annotationType, clazz.getInterfaces());
127126
}
128127
while (annotation == null) {
129-
cl = cl.getSuperclass();
130-
if (cl == null || cl == Object.class) {
128+
clazz = clazz.getSuperclass();
129+
if (clazz == null || clazz.equals(Object.class)) {
131130
break;
132131
}
133132
try {
134-
Method equivalentMethod = cl.getDeclaredMethod(method.getName(), method.getParameterTypes());
133+
Method equivalentMethod = clazz.getDeclaredMethod(method.getName(), method.getParameterTypes());
135134
annotation = getAnnotation(equivalentMethod, annotationType);
136135
}
137136
catch (NoSuchMethodException ex) {
138137
// No equivalent method found
139138
}
140139
if (annotation == null) {
141-
annotation = searchOnInterfaces(method, annotationType, cl.getInterfaces());
140+
annotation = searchOnInterfaces(method, annotationType, clazz.getInterfaces());
142141
}
143142
}
144143
return annotation;
@@ -217,7 +216,7 @@ public static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> a
217216
}
218217
}
219218
Class<?> superClass = clazz.getSuperclass();
220-
if (superClass == null || superClass == Object.class) {
219+
if (superClass == null || superClass.equals(Object.class)) {
221220
return null;
222221
}
223222
return findAnnotation(superClass, annotationType);
@@ -273,25 +272,22 @@ public static Class<?> findAnnotationDeclaringClass(Class<? extends Annotation>
273272
* @return the first {@link Class} in the inheritance hierarchy of the specified
274273
* {@code clazz} which declares an annotation of at least one of the specified
275274
* {@code annotationTypes}, or {@code null} if not found
275+
* @since 3.2.2
276276
* @see Class#isAnnotationPresent(Class)
277277
* @see Class#getDeclaredAnnotations()
278278
* @see #findAnnotationDeclaringClass(Class, Class)
279279
* @see #isAnnotationDeclaredLocally(Class, Class)
280-
* @since 3.2.2
281280
*/
282-
public static Class<?> findAnnotationDeclaringClassForTypes(List<Class<? extends Annotation>> annotationTypes,
283-
Class<?> clazz) {
281+
public static Class<?> findAnnotationDeclaringClassForTypes(List<Class<? extends Annotation>> annotationTypes, Class<?> clazz) {
284282
Assert.notEmpty(annotationTypes, "The list of annotation types must not be empty");
285283
if (clazz == null || clazz.equals(Object.class)) {
286284
return null;
287285
}
288-
289286
for (Class<? extends Annotation> annotationType : annotationTypes) {
290287
if (isAnnotationDeclaredLocally(annotationType, clazz)) {
291288
return clazz;
292289
}
293290
}
294-
295291
return findAnnotationDeclaringClassForTypes(annotationTypes, clazz.getSuperclass());
296292
}
297293

spring-core/src/test/java/org/springframework/core/annotation/AnnotationAwareOrderComparatorTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ public void sortInstances() {
4545
assertTrue(list.get(1) instanceof B);
4646
}
4747

48+
@Test
49+
public void sortInstancesWithSubclass() {
50+
List<Object> list = new ArrayList<>();
51+
list.add(new B());
52+
list.add(new C());
53+
AnnotationAwareOrderComparator.sort(list);
54+
assertTrue(list.get(0) instanceof C);
55+
assertTrue(list.get(1) instanceof B);
56+
}
57+
4858
@Test
4959
public void sortClasses() {
5060
List<Object> list = new ArrayList<>();
@@ -55,6 +65,16 @@ public void sortClasses() {
5565
assertEquals(B.class, list.get(1));
5666
}
5767

68+
@Test
69+
public void sortClassesWithSubclass() {
70+
List<Object> list = new ArrayList<>();
71+
list.add(B.class);
72+
list.add(C.class);
73+
AnnotationAwareOrderComparator.sort(list);
74+
assertEquals(C.class, list.get(0));
75+
assertEquals(B.class, list.get(1));
76+
}
77+
5878

5979
@Order(1)
6080
private static class A {
@@ -64,4 +84,7 @@ private static class A {
6484
private static class B {
6585
}
6686

87+
private static class C extends A {
88+
}
89+
6790
}

0 commit comments

Comments
 (0)