25
25
* Handles Spring's {@link Order} annotation as well as {@link javax.annotation.Priority}.
26
26
*
27
27
* @author Stephane Nicoll
28
+ * @author Juergen Hoeller
28
29
* @since 4.1
29
30
* @see Order
30
31
* @see javax.annotation.Priority
31
32
*/
33
+ @ SuppressWarnings ("unchecked" )
32
34
public abstract class OrderUtils {
33
35
34
- private static final String PRIORITY_ANNOTATION_CLASS_NAME = "javax.annotation.Priority" ;
36
+ private static Class <? extends Annotation > priorityAnnotationType = null ;
35
37
36
- private static final boolean priorityPresent =
37
- ClassUtils .isPresent (PRIORITY_ANNOTATION_CLASS_NAME , OrderUtils .class .getClassLoader ());
38
+ static {
39
+ try {
40
+ priorityAnnotationType = (Class <? extends Annotation >)
41
+ ClassUtils .forName ("javax.annotation.Priority" , OrderUtils .class .getClassLoader ());
42
+ }
43
+ catch (ClassNotFoundException ex ) {
44
+ // javax.annotation.Priority not available
45
+ }
46
+ }
38
47
39
48
40
49
/**
@@ -52,7 +61,7 @@ public static Integer getOrder(Class<?> type) {
52
61
* default value if none can be found.
53
62
* <p>Take care of {@link Order @Order} and {@code @javax.annotation.Priority}.
54
63
* @param type the type to handle
55
- * @return the priority value of the default if none can be found
64
+ * @return the priority value, or the specified default order if none can be found
56
65
*/
57
66
public static Integer getOrder (Class <?> type , Integer defaultOrder ) {
58
67
Order order = AnnotationUtils .findAnnotation (type , Order .class );
@@ -67,17 +76,16 @@ public static Integer getOrder(Class<?> type, Integer defaultOrder) {
67
76
}
68
77
69
78
/**
70
- * Return the value of the {@code javax.annotation.Priority} annotation set
71
- * on the specified type or {@code null} if none is set .
79
+ * Return the value of the {@code javax.annotation.Priority} annotation
80
+ * declared on the specified type, or {@code null} if none.
72
81
* @param type the type to handle
73
- * @return the priority value if the annotation is set, {@code null} otherwise
82
+ * @return the priority value if the annotation is declared, or {@code null} if none
74
83
*/
75
84
public static Integer getPriority (Class <?> type ) {
76
- if (priorityPresent ) {
77
- for (Annotation annotation : type .getAnnotations ()) {
78
- if (PRIORITY_ANNOTATION_CLASS_NAME .equals (annotation .annotationType ().getName ())) {
79
- return (Integer ) AnnotationUtils .getValue (annotation );
80
- }
85
+ if (priorityAnnotationType != null ) {
86
+ Annotation priority = AnnotationUtils .findAnnotation (type , priorityAnnotationType );
87
+ if (priority != null ) {
88
+ return (Integer ) AnnotationUtils .getValue (priority );
81
89
}
82
90
}
83
91
return null ;
0 commit comments