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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -17,12 +17,12 @@
|
||||||
package org.springframework.core.annotation;
|
package org.springframework.core.annotation;
|
||||||
|
|
||||||
import java.lang.reflect.AnnotatedElement;
|
import java.lang.reflect.AnnotatedElement;
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.core.DecoratingProxy;
|
import org.springframework.core.DecoratingProxy;
|
||||||
import org.springframework.core.OrderComparator;
|
import org.springframework.core.OrderComparator;
|
||||||
|
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -61,35 +61,23 @@ public class AnnotationAwareOrderComparator extends OrderComparator {
|
||||||
@Override
|
@Override
|
||||||
@Nullable
|
@Nullable
|
||||||
protected Integer findOrder(Object obj) {
|
protected Integer findOrder(Object obj) {
|
||||||
// Check for regular Ordered interface
|
|
||||||
Integer order = super.findOrder(obj);
|
Integer order = super.findOrder(obj);
|
||||||
if (order != null) {
|
if (order != null) {
|
||||||
return order;
|
return order;
|
||||||
}
|
}
|
||||||
|
return findOrderFromAnnotation(obj);
|
||||||
|
}
|
||||||
|
|
||||||
// Check for @Order and @Priority on various kinds of elements
|
private Integer findOrderFromAnnotation(Object obj) {
|
||||||
if (obj instanceof Class) {
|
AnnotatedElement element = obj instanceof AnnotatedElement
|
||||||
return OrderUtils.getOrder((Class<?>) obj);
|
? (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;
|
return order;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,8 +94,8 @@ public class AnnotationAwareOrderComparator extends OrderComparator {
|
||||||
return OrderUtils.getPriority((Class<?>) obj);
|
return OrderUtils.getPriority((Class<?>) obj);
|
||||||
}
|
}
|
||||||
Integer priority = OrderUtils.getPriority(obj.getClass());
|
Integer priority = OrderUtils.getPriority(obj.getClass());
|
||||||
if (priority == null && obj instanceof DecoratingProxy) {
|
if (priority == null && obj instanceof DecoratingProxy) {
|
||||||
priority = OrderUtils.getPriority(((DecoratingProxy) obj).getDecoratedClass());
|
return getPriority(((DecoratingProxy) obj).getDecoratedClass());
|
||||||
}
|
}
|
||||||
return priority;
|
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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -16,11 +16,11 @@
|
||||||
|
|
||||||
package org.springframework.core.annotation;
|
package org.springframework.core.annotation;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.reflect.AnnotatedElement;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.ClassUtils;
|
|
||||||
import org.springframework.util.ConcurrentReferenceHashMap;
|
import org.springframework.util.ConcurrentReferenceHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -33,33 +33,15 @@ import org.springframework.util.ConcurrentReferenceHashMap;
|
||||||
* @see Order
|
* @see Order
|
||||||
* @see javax.annotation.Priority
|
* @see javax.annotation.Priority
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public abstract class OrderUtils {
|
public abstract class OrderUtils {
|
||||||
|
|
||||||
/** Cache marker for a non-annotated Class. */
|
/** Cache marker for a non-annotated Class. */
|
||||||
private static final Object NOT_ANNOTATED = new Object();
|
private static final Object NOT_ANNOTATED = new Object();
|
||||||
|
|
||||||
|
private static final String JAVAX_PRIORITY_ANNOTATION = "javax.annotation.Priority";
|
||||||
@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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/** Cache for @Order value (or NOT_ANNOTATED marker) per Class. */
|
/** Cache for @Order value (or NOT_ANNOTATED marker) per Class. */
|
||||||
private static final Map<Class<?>, Object> orderCache = new ConcurrentReferenceHashMap<>(64);
|
private static final Map<AnnotatedElement, Object> orderCache = new ConcurrentReferenceHashMap<>(64);
|
||||||
|
|
||||||
/** Cache for @Priority value (or NOT_ANNOTATED marker) per Class. */
|
|
||||||
private static final Map<Class<?>, Object> priorityCache = new ConcurrentReferenceHashMap<>();
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -99,22 +81,43 @@ public abstract class OrderUtils {
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public static Integer getOrder(Class<?> type) {
|
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) {
|
if (cached != null) {
|
||||||
return (cached instanceof Integer ? (Integer) cached : null);
|
return (cached instanceof Integer ? (Integer) cached : null);
|
||||||
}
|
}
|
||||||
Order order = AnnotationUtils.findAnnotation(type, Order.class);
|
Integer result = findOrder(annotations);
|
||||||
Integer result;
|
orderCache.put(element, result != null ? result : NOT_ANNOTATED);
|
||||||
if (order != null) {
|
|
||||||
result = order.value();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
result = getPriority(type);
|
|
||||||
}
|
|
||||||
orderCache.put(type, (result != null ? result : NOT_ANNOTATED));
|
|
||||||
return result;
|
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
|
* Return the value of the {@code javax.annotation.Priority} annotation
|
||||||
* declared on the specified type, or {@code null} if none.
|
* declared on the specified type, or {@code null} if none.
|
||||||
|
@ -123,20 +126,9 @@ public abstract class OrderUtils {
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public static Integer getPriority(Class<?> type) {
|
public static Integer getPriority(Class<?> type) {
|
||||||
if (priorityAnnotationType == null) {
|
return MergedAnnotations.from(type, SearchStrategy.EXHAUSTIVE).get(
|
||||||
return null;
|
JAVAX_PRIORITY_ANNOTATION).getValue(MergedAnnotation.VALUE,
|
||||||
}
|
Integer.class).orElse(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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue