1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2018 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.
17
17
package org .springframework .core .annotation ;
18
18
19
19
import java .lang .annotation .Annotation ;
20
+ import java .util .Map ;
20
21
21
22
import org .springframework .lang .Nullable ;
22
23
import org .springframework .util .ClassUtils ;
24
+ import org .springframework .util .ConcurrentReferenceHashMap ;
23
25
24
26
/**
25
27
* General utility for determining the order of an object based on its type declaration.
34
36
@ SuppressWarnings ("unchecked" )
35
37
public abstract class OrderUtils {
36
38
39
+ /** Cache marker for a non-annotated Class */
40
+ private static final Object NOT_ANNOTATED = new Object ();
41
+
42
+
37
43
@ Nullable
38
44
private static Class <? extends Annotation > priorityAnnotationType ;
39
45
@@ -49,6 +55,13 @@ public abstract class OrderUtils {
49
55
}
50
56
51
57
58
+ /** 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 <>();
63
+
64
+
52
65
/**
53
66
* Return the order on the specified {@code type}, or the specified
54
67
* default value if none can be found.
@@ -86,15 +99,20 @@ public static Integer getOrder(Class<?> type, @Nullable Integer defaultOrder) {
86
99
*/
87
100
@ Nullable
88
101
public static Integer getOrder (Class <?> type ) {
102
+ Object cached = orderCache .get (type );
103
+ if (cached != null ) {
104
+ return (cached instanceof Integer ? (Integer ) cached : null );
105
+ }
89
106
Order order = AnnotationUtils .findAnnotation (type , Order .class );
107
+ Integer result ;
90
108
if (order != null ) {
91
- return order .value ();
109
+ result = order .value ();
92
110
}
93
- Integer priorityOrder = getPriority (type );
94
- if (priorityOrder != null ) {
95
- return priorityOrder ;
111
+ else {
112
+ result = getPriority (type );
96
113
}
97
- return null ;
114
+ orderCache .put (type , (result != null ? result : NOT_ANNOTATED ));
115
+ return result ;
98
116
}
99
117
100
118
/**
@@ -105,13 +123,20 @@ public static Integer getOrder(Class<?> type) {
105
123
*/
106
124
@ Nullable
107
125
public static Integer getPriority (Class <?> type ) {
108
- if (priorityAnnotationType != null ) {
109
- Annotation priority = AnnotationUtils .findAnnotation (type , priorityAnnotationType );
110
- if (priority != null ) {
111
- return (Integer ) AnnotationUtils .getValue (priority );
112
- }
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 );
113
137
}
114
- return null ;
138
+ priorityCache .put (type , (result != null ? result : NOT_ANNOTATED ));
139
+ return result ;
115
140
}
116
141
117
142
}
0 commit comments