|
|
|
@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
|
|
|
|
|
/* |
|
|
|
|
* Copyright 2002-2017 the original author or authors. |
|
|
|
|
* Copyright 2002-2018 the original author or authors. |
|
|
|
|
* |
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -17,9 +17,11 @@
@@ -17,9 +17,11 @@
|
|
|
|
|
package org.springframework.core.annotation; |
|
|
|
|
|
|
|
|
|
import java.lang.annotation.Annotation; |
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
import org.springframework.lang.Nullable; |
|
|
|
|
import org.springframework.util.ClassUtils; |
|
|
|
|
import org.springframework.util.ConcurrentReferenceHashMap; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* General utility for determining the order of an object based on its type declaration. |
|
|
|
@ -34,6 +36,10 @@ import org.springframework.util.ClassUtils;
@@ -34,6 +36,10 @@ import org.springframework.util.ClassUtils;
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
public abstract class OrderUtils { |
|
|
|
|
|
|
|
|
|
/** Cache marker for a non-annotated Class */ |
|
|
|
|
private static final Object NOT_ANNOTATED = new Object(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
|
private static Class<? extends Annotation> priorityAnnotationType; |
|
|
|
|
|
|
|
|
@ -49,6 +55,13 @@ public abstract class OrderUtils {
@@ -49,6 +55,13 @@ public abstract class OrderUtils {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Cache for @Order value (or NOT_ANNOTATED marker) per Class */ |
|
|
|
|
private static final Map<Class<?>, Object> orderCache = new ConcurrentReferenceHashMap<>(64); |
|
|
|
|
|
|
|
|
|
/** Cache for @Priority value (or NOT_ANNOTATED marker) per Class */ |
|
|
|
|
private static final Map<Class<?>, Object> priorityCache = new ConcurrentReferenceHashMap<>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Return the order on the specified {@code type}, or the specified |
|
|
|
|
* default value if none can be found. |
|
|
|
@ -86,15 +99,20 @@ public abstract class OrderUtils {
@@ -86,15 +99,20 @@ public abstract class OrderUtils {
|
|
|
|
|
*/ |
|
|
|
|
@Nullable |
|
|
|
|
public static Integer getOrder(Class<?> type) { |
|
|
|
|
Object cached = orderCache.get(type); |
|
|
|
|
if (cached != null) { |
|
|
|
|
return (cached instanceof Integer ? (Integer) cached : null); |
|
|
|
|
} |
|
|
|
|
Order order = AnnotationUtils.findAnnotation(type, Order.class); |
|
|
|
|
Integer result; |
|
|
|
|
if (order != null) { |
|
|
|
|
return order.value(); |
|
|
|
|
result = order.value(); |
|
|
|
|
} |
|
|
|
|
Integer priorityOrder = getPriority(type); |
|
|
|
|
if (priorityOrder != null) { |
|
|
|
|
return priorityOrder; |
|
|
|
|
else { |
|
|
|
|
result = getPriority(type); |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
orderCache.put(type, (result != null ? result : NOT_ANNOTATED)); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -105,13 +123,20 @@ public abstract class OrderUtils {
@@ -105,13 +123,20 @@ public abstract class OrderUtils {
|
|
|
|
|
*/ |
|
|
|
|
@Nullable |
|
|
|
|
public static Integer getPriority(Class<?> type) { |
|
|
|
|
if (priorityAnnotationType != null) { |
|
|
|
|
if (priorityAnnotationType == null) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
Object cached = priorityCache.get(type); |
|
|
|
|
if (cached != null) { |
|
|
|
|
return (cached instanceof Integer ? (Integer) cached : null); |
|
|
|
|
} |
|
|
|
|
Annotation priority = AnnotationUtils.findAnnotation(type, priorityAnnotationType); |
|
|
|
|
Integer result = null; |
|
|
|
|
if (priority != null) { |
|
|
|
|
return (Integer) AnnotationUtils.getValue(priority); |
|
|
|
|
} |
|
|
|
|
result = (Integer) AnnotationUtils.getValue(priority); |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
priorityCache.put(type, (result != null ? result : NOT_ANNOTATED)); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|