1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .core .annotation ;
18
18
19
- import java .lang .annotation . Annotation ;
19
+ import java .lang .reflect . AnnotatedElement ;
20
20
import java .util .Map ;
21
21
22
+ import org .springframework .core .annotation .MergedAnnotations .SearchStrategy ;
22
23
import org .springframework .lang .Nullable ;
23
- import org .springframework .util .ClassUtils ;
24
24
import org .springframework .util .ConcurrentReferenceHashMap ;
25
25
26
26
/**
33
33
* @see Order
34
34
* @see javax.annotation.Priority
35
35
*/
36
- @ SuppressWarnings ("unchecked" )
37
36
public abstract class OrderUtils {
38
37
39
38
/** Cache marker for a non-annotated Class. */
40
39
private static final Object NOT_ANNOTATED = new Object ();
41
40
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" ;
57
42
58
43
/** 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 );
63
45
64
46
65
47
/**
@@ -99,20 +81,41 @@ public static Integer getOrder(Class<?> type, @Nullable Integer defaultOrder) {
99
81
*/
100
82
@ Nullable
101
83
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 );
103
101
if (cached != null ) {
104
102
return (cached instanceof Integer ? (Integer ) cached : null );
105
103
}
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 );
110
113
}
111
- else {
112
- result = getPriority (type );
114
+ MergedAnnotation <?> priorityAnnotation = annotations .get (JAVAX_PRIORITY_ANNOTATION );
115
+ if (priorityAnnotation .isPresent ()) {
116
+ return priorityAnnotation .getInt (MergedAnnotation .VALUE );
113
117
}
114
- orderCache .put (type , (result != null ? result : NOT_ANNOTATED ));
115
- return result ;
118
+ return null ;
116
119
}
117
120
118
121
/**
@@ -123,20 +126,9 @@ public static Integer getOrder(Class<?> type) {
123
126
*/
124
127
@ Nullable
125
128
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 );
140
132
}
141
133
142
134
}
0 commit comments