Skip to content

Commit 20bb7ac

Browse files
committed
Migrate AnnotationAwareOrderComparator to MergedAnnotations
Closes gh-22581
1 parent f0768e5 commit 20bb7ac

File tree

2 files changed

+53
-73
lines changed

2 files changed

+53
-73
lines changed

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

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -17,12 +17,12 @@
1717
package org.springframework.core.annotation;
1818

1919
import java.lang.reflect.AnnotatedElement;
20-
import java.lang.reflect.Method;
2120
import java.util.Arrays;
2221
import java.util.List;
2322

2423
import org.springframework.core.DecoratingProxy;
2524
import org.springframework.core.OrderComparator;
25+
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
2626
import org.springframework.lang.Nullable;
2727

2828
/**
@@ -61,35 +61,23 @@ public class AnnotationAwareOrderComparator extends OrderComparator {
6161
@Override
6262
@Nullable
6363
protected Integer findOrder(Object obj) {
64-
// Check for regular Ordered interface
6564
Integer order = super.findOrder(obj);
6665
if (order != null) {
6766
return order;
6867
}
68+
return findOrderFromAnnotation(obj);
69+
}
6970

70-
// Check for @Order and @Priority on various kinds of elements
71-
if (obj instanceof Class) {
72-
return OrderUtils.getOrder((Class<?>) obj);
73-
}
74-
else if (obj instanceof Method) {
75-
Order ann = AnnotationUtils.findAnnotation((Method) obj, Order.class);
76-
if (ann != null) {
77-
return ann.value();
78-
}
71+
private Integer findOrderFromAnnotation(Object obj) {
72+
AnnotatedElement element = obj instanceof AnnotatedElement
73+
? (AnnotatedElement) obj
74+
: obj.getClass();
75+
MergedAnnotations annotations = MergedAnnotations.from(element,
76+
SearchStrategy.EXHAUSTIVE);
77+
Integer order = OrderUtils.getOrderFromAnnotations(element, annotations);
78+
if (order == null && obj instanceof DecoratingProxy) {
79+
return findOrderFromAnnotation(((DecoratingProxy) obj).getDecoratedClass());
7980
}
80-
else if (obj instanceof AnnotatedElement) {
81-
Order ann = AnnotationUtils.getAnnotation((AnnotatedElement) obj, Order.class);
82-
if (ann != null) {
83-
return ann.value();
84-
}
85-
}
86-
else {
87-
order = OrderUtils.getOrder(obj.getClass());
88-
if (order == null && obj instanceof DecoratingProxy) {
89-
order = OrderUtils.getOrder(((DecoratingProxy) obj).getDecoratedClass());
90-
}
91-
}
92-
9381
return order;
9482
}
9583

@@ -106,8 +94,8 @@ public Integer getPriority(Object obj) {
10694
return OrderUtils.getPriority((Class<?>) obj);
10795
}
10896
Integer priority = OrderUtils.getPriority(obj.getClass());
109-
if (priority == null && obj instanceof DecoratingProxy) {
110-
priority = OrderUtils.getPriority(((DecoratingProxy) obj).getDecoratedClass());
97+
if (priority == null && obj instanceof DecoratingProxy) {
98+
return getPriority(((DecoratingProxy) obj).getDecoratedClass());
11199
}
112100
return priority;
113101
}

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

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -16,11 +16,11 @@
1616

1717
package org.springframework.core.annotation;
1818

19-
import java.lang.annotation.Annotation;
19+
import java.lang.reflect.AnnotatedElement;
2020
import java.util.Map;
2121

22+
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
2223
import org.springframework.lang.Nullable;
23-
import org.springframework.util.ClassUtils;
2424
import org.springframework.util.ConcurrentReferenceHashMap;
2525

2626
/**
@@ -33,33 +33,15 @@
3333
* @see Order
3434
* @see javax.annotation.Priority
3535
*/
36-
@SuppressWarnings("unchecked")
3736
public abstract class OrderUtils {
3837

3938
/** Cache marker for a non-annotated Class. */
4039
private static final Object NOT_ANNOTATED = new Object();
4140

42-
43-
@Nullable
44-
private static Class<? extends Annotation> priorityAnnotationType;
45-
46-
static {
47-
try {
48-
priorityAnnotationType = (Class<? extends Annotation>)
49-
ClassUtils.forName("javax.annotation.Priority", OrderUtils.class.getClassLoader());
50-
}
51-
catch (Throwable ex) {
52-
// javax.annotation.Priority not available
53-
priorityAnnotationType = null;
54-
}
55-
}
56-
41+
private static final String JAVAX_PRIORITY_ANNOTATION = "javax.annotation.Priority";
5742

5843
/** Cache for @Order value (or NOT_ANNOTATED marker) per Class. */
59-
private static final Map<Class<?>, Object> orderCache = new ConcurrentReferenceHashMap<>(64);
60-
61-
/** Cache for @Priority value (or NOT_ANNOTATED marker) per Class. */
62-
private static final Map<Class<?>, Object> priorityCache = new ConcurrentReferenceHashMap<>();
44+
private static final Map<AnnotatedElement, Object> orderCache = new ConcurrentReferenceHashMap<>(64);
6345

6446

6547
/**
@@ -99,20 +81,41 @@ public static Integer getOrder(Class<?> type, @Nullable Integer defaultOrder) {
9981
*/
10082
@Nullable
10183
public static Integer getOrder(Class<?> type) {
102-
Object cached = orderCache.get(type);
84+
return getOrderFromAnnotations(type, MergedAnnotations.from(type, SearchStrategy.EXHAUSTIVE));
85+
}
86+
87+
/**
88+
* Return the order from the specified annotations collection.
89+
* <p>Takes care of {@link Order @Order} and
90+
* {@code @javax.annotation.Priority}.
91+
* @param element the source element
92+
* @param annotations the annotation to consider
93+
* @return the order value, or {@code null} if none can be found
94+
*/
95+
static Integer getOrderFromAnnotations(AnnotatedElement element,
96+
MergedAnnotations annotations) {
97+
if (!(element instanceof Class)) {
98+
return findOrder(annotations);
99+
}
100+
Object cached = orderCache.get(element);
103101
if (cached != null) {
104102
return (cached instanceof Integer ? (Integer) cached : null);
105103
}
106-
Order order = AnnotationUtils.findAnnotation(type, Order.class);
107-
Integer result;
108-
if (order != null) {
109-
result = order.value();
104+
Integer result = findOrder(annotations);
105+
orderCache.put(element, result != null ? result : NOT_ANNOTATED);
106+
return result;
107+
}
108+
109+
private static Integer findOrder(MergedAnnotations annotations) {
110+
MergedAnnotation<Order> orderAnnotation = annotations.get(Order.class);
111+
if (orderAnnotation.isPresent()) {
112+
return orderAnnotation.getInt(MergedAnnotation.VALUE);
110113
}
111-
else {
112-
result = getPriority(type);
114+
MergedAnnotation<?> priorityAnnotation = annotations.get(JAVAX_PRIORITY_ANNOTATION);
115+
if (priorityAnnotation.isPresent()) {
116+
return priorityAnnotation.getInt(MergedAnnotation.VALUE);
113117
}
114-
orderCache.put(type, (result != null ? result : NOT_ANNOTATED));
115-
return result;
118+
return null;
116119
}
117120

118121
/**
@@ -123,20 +126,9 @@ public static Integer getOrder(Class<?> type) {
123126
*/
124127
@Nullable
125128
public static Integer getPriority(Class<?> type) {
126-
if (priorityAnnotationType == null) {
127-
return null;
128-
}
129-
Object cached = priorityCache.get(type);
130-
if (cached != null) {
131-
return (cached instanceof Integer ? (Integer) cached : null);
132-
}
133-
Annotation priority = AnnotationUtils.findAnnotation(type, priorityAnnotationType);
134-
Integer result = null;
135-
if (priority != null) {
136-
result = (Integer) AnnotationUtils.getValue(priority);
137-
}
138-
priorityCache.put(type, (result != null ? result : NOT_ANNOTATED));
139-
return result;
129+
return MergedAnnotations.from(type, SearchStrategy.EXHAUSTIVE).get(
130+
JAVAX_PRIORITY_ANNOTATION).getValue(MergedAnnotation.VALUE,
131+
Integer.class).orElse(null);
140132
}
141133

142134
}

0 commit comments

Comments
 (0)