Migrate AnnotationAwareOrderComparator to MergedAnnotations
Closes gh-22581
This commit is contained in:
parent
7244c9aea1
commit
f065abac93
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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,12 +17,12 @@
|
|||
package org.springframework.core.annotation;
|
||||
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.core.DecoratingProxy;
|
||||
import org.springframework.core.OrderComparator;
|
||||
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
|
@ -61,35 +61,23 @@ public class AnnotationAwareOrderComparator extends OrderComparator {
|
|||
@Override
|
||||
@Nullable
|
||||
protected Integer findOrder(Object obj) {
|
||||
// Check for regular Ordered interface
|
||||
Integer order = super.findOrder(obj);
|
||||
if (order != null) {
|
||||
return order;
|
||||
}
|
||||
return findOrderFromAnnotation(obj);
|
||||
}
|
||||
|
||||
// Check for @Order and @Priority on various kinds of elements
|
||||
if (obj instanceof Class) {
|
||||
return OrderUtils.getOrder((Class<?>) obj);
|
||||
private Integer findOrderFromAnnotation(Object obj) {
|
||||
AnnotatedElement element = obj instanceof AnnotatedElement
|
||||
? (AnnotatedElement) obj
|
||||
: obj.getClass();
|
||||
MergedAnnotations annotations = MergedAnnotations.from(element,
|
||||
SearchStrategy.EXHAUSTIVE);
|
||||
Integer order = OrderUtils.getOrderFromAnnotations(element, annotations);
|
||||
if (order == null && obj instanceof DecoratingProxy) {
|
||||
return findOrderFromAnnotation(((DecoratingProxy) obj).getDecoratedClass());
|
||||
}
|
||||
else if (obj instanceof Method) {
|
||||
Order ann = AnnotationUtils.findAnnotation((Method) obj, Order.class);
|
||||
if (ann != null) {
|
||||
return ann.value();
|
||||
}
|
||||
}
|
||||
else if (obj instanceof AnnotatedElement) {
|
||||
Order ann = AnnotationUtils.getAnnotation((AnnotatedElement) obj, Order.class);
|
||||
if (ann != null) {
|
||||
return ann.value();
|
||||
}
|
||||
}
|
||||
else {
|
||||
order = OrderUtils.getOrder(obj.getClass());
|
||||
if (order == null && obj instanceof DecoratingProxy) {
|
||||
order = OrderUtils.getOrder(((DecoratingProxy) obj).getDecoratedClass());
|
||||
}
|
||||
}
|
||||
|
||||
return order;
|
||||
}
|
||||
|
||||
|
@ -106,8 +94,8 @@ public class AnnotationAwareOrderComparator extends OrderComparator {
|
|||
return OrderUtils.getPriority((Class<?>) obj);
|
||||
}
|
||||
Integer priority = OrderUtils.getPriority(obj.getClass());
|
||||
if (priority == null && obj instanceof DecoratingProxy) {
|
||||
priority = OrderUtils.getPriority(((DecoratingProxy) obj).getDecoratedClass());
|
||||
if (priority == null && obj instanceof DecoratingProxy) {
|
||||
return getPriority(((DecoratingProxy) obj).getDecoratedClass());
|
||||
}
|
||||
return priority;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
|
@ -16,11 +16,11 @@
|
|||
|
||||
package org.springframework.core.annotation;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ConcurrentReferenceHashMap;
|
||||
|
||||
/**
|
||||
|
@ -33,33 +33,15 @@ import org.springframework.util.ConcurrentReferenceHashMap;
|
|||
* @see Order
|
||||
* @see javax.annotation.Priority
|
||||
*/
|
||||
@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;
|
||||
|
||||
static {
|
||||
try {
|
||||
priorityAnnotationType = (Class<? extends Annotation>)
|
||||
ClassUtils.forName("javax.annotation.Priority", OrderUtils.class.getClassLoader());
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
// javax.annotation.Priority not available
|
||||
priorityAnnotationType = null;
|
||||
}
|
||||
}
|
||||
|
||||
private static final String JAVAX_PRIORITY_ANNOTATION = "javax.annotation.Priority";
|
||||
|
||||
/** 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<>();
|
||||
private static final Map<AnnotatedElement, Object> orderCache = new ConcurrentReferenceHashMap<>(64);
|
||||
|
||||
|
||||
/**
|
||||
|
@ -99,22 +81,43 @@ public abstract class OrderUtils {
|
|||
*/
|
||||
@Nullable
|
||||
public static Integer getOrder(Class<?> type) {
|
||||
Object cached = orderCache.get(type);
|
||||
return getOrderFromAnnotations(type, MergedAnnotations.from(type, SearchStrategy.EXHAUSTIVE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the order from the specified annotations collection.
|
||||
* <p>Takes care of {@link Order @Order} and
|
||||
* {@code @javax.annotation.Priority}.
|
||||
* @param element the source element
|
||||
* @param annotations the annotation to consider
|
||||
* @return the order value, or {@code null} if none can be found
|
||||
*/
|
||||
static Integer getOrderFromAnnotations(AnnotatedElement element,
|
||||
MergedAnnotations annotations) {
|
||||
if (!(element instanceof Class)) {
|
||||
return findOrder(annotations);
|
||||
}
|
||||
Object cached = orderCache.get(element);
|
||||
if (cached != null) {
|
||||
return (cached instanceof Integer ? (Integer) cached : null);
|
||||
}
|
||||
Order order = AnnotationUtils.findAnnotation(type, Order.class);
|
||||
Integer result;
|
||||
if (order != null) {
|
||||
result = order.value();
|
||||
}
|
||||
else {
|
||||
result = getPriority(type);
|
||||
}
|
||||
orderCache.put(type, (result != null ? result : NOT_ANNOTATED));
|
||||
Integer result = findOrder(annotations);
|
||||
orderCache.put(element, result != null ? result : NOT_ANNOTATED);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Integer findOrder(MergedAnnotations annotations) {
|
||||
MergedAnnotation<Order> orderAnnotation = annotations.get(Order.class);
|
||||
if (orderAnnotation.isPresent()) {
|
||||
return orderAnnotation.getInt(MergedAnnotation.VALUE);
|
||||
}
|
||||
MergedAnnotation<?> priorityAnnotation = annotations.get(JAVAX_PRIORITY_ANNOTATION);
|
||||
if (priorityAnnotation.isPresent()) {
|
||||
return priorityAnnotation.getInt(MergedAnnotation.VALUE);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value of the {@code javax.annotation.Priority} annotation
|
||||
* declared on the specified type, or {@code null} if none.
|
||||
|
@ -123,20 +126,9 @@ public abstract class OrderUtils {
|
|||
*/
|
||||
@Nullable
|
||||
public static Integer getPriority(Class<?> type) {
|
||||
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) {
|
||||
result = (Integer) AnnotationUtils.getValue(priority);
|
||||
}
|
||||
priorityCache.put(type, (result != null ? result : NOT_ANNOTATED));
|
||||
return result;
|
||||
return MergedAnnotations.from(type, SearchStrategy.EXHAUSTIVE).get(
|
||||
JAVAX_PRIORITY_ANNOTATION).getValue(MergedAnnotation.VALUE,
|
||||
Integer.class).orElse(null);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue