001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.core.annotation;
018
019import java.lang.reflect.AnnotatedElement;
020import java.util.Arrays;
021import java.util.List;
022
023import org.springframework.core.DecoratingProxy;
024import org.springframework.core.OrderComparator;
025import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
026import org.springframework.lang.Nullable;
027
028/**
029 * {@code AnnotationAwareOrderComparator} is an extension of
030 * {@link OrderComparator} that supports Spring's
031 * {@link org.springframework.core.Ordered} interface as well as the
032 * {@link Order @Order} and {@link javax.annotation.Priority @Priority}
033 * annotations, with an order value provided by an {@code Ordered}
034 * instance overriding a statically defined annotation value (if any).
035 *
036 * <p>Consult the Javadoc for {@link OrderComparator} for details on the
037 * sort semantics for non-ordered objects.
038 *
039 * @author Juergen Hoeller
040 * @author Oliver Gierke
041 * @author Stephane Nicoll
042 * @since 2.0.1
043 * @see org.springframework.core.Ordered
044 * @see org.springframework.core.annotation.Order
045 * @see javax.annotation.Priority
046 */
047public class AnnotationAwareOrderComparator extends OrderComparator {
048
049        /**
050         * Shared default instance of {@code AnnotationAwareOrderComparator}.
051         */
052        public static final AnnotationAwareOrderComparator INSTANCE = new AnnotationAwareOrderComparator();
053
054
055        /**
056         * This implementation checks for {@link Order @Order} or
057         * {@link javax.annotation.Priority @Priority} on various kinds of
058         * elements, in addition to the {@link org.springframework.core.Ordered}
059         * check in the superclass.
060         */
061        @Override
062        @Nullable
063        protected Integer findOrder(Object obj) {
064                Integer order = super.findOrder(obj);
065                if (order != null) {
066                        return order;
067                }
068                return findOrderFromAnnotation(obj);
069        }
070
071        @Nullable
072        private Integer findOrderFromAnnotation(Object obj) {
073                AnnotatedElement element = (obj instanceof AnnotatedElement ? (AnnotatedElement) obj : obj.getClass());
074                MergedAnnotations annotations = MergedAnnotations.from(element, SearchStrategy.TYPE_HIERARCHY);
075                Integer order = OrderUtils.getOrderFromAnnotations(element, annotations);
076                if (order == null && obj instanceof DecoratingProxy) {
077                        return findOrderFromAnnotation(((DecoratingProxy) obj).getDecoratedClass());
078                }
079                return order;
080        }
081
082        /**
083         * This implementation retrieves an @{@link javax.annotation.Priority}
084         * value, allowing for additional semantics over the regular @{@link Order}
085         * annotation: typically, selecting one object over another in case of
086         * multiple matches but only one object to be returned.
087         */
088        @Override
089        @Nullable
090        public Integer getPriority(Object obj) {
091                if (obj instanceof Class) {
092                        return OrderUtils.getPriority((Class<?>) obj);
093                }
094                Integer priority = OrderUtils.getPriority(obj.getClass());
095                if (priority == null  && obj instanceof DecoratingProxy) {
096                        return getPriority(((DecoratingProxy) obj).getDecoratedClass());
097                }
098                return priority;
099        }
100
101
102        /**
103         * Sort the given list with a default {@link AnnotationAwareOrderComparator}.
104         * <p>Optimized to skip sorting for lists with size 0 or 1,
105         * in order to avoid unnecessary array extraction.
106         * @param list the List to sort
107         * @see java.util.List#sort(java.util.Comparator)
108         */
109        public static void sort(List<?> list) {
110                if (list.size() > 1) {
111                        list.sort(INSTANCE);
112                }
113        }
114
115        /**
116         * Sort the given array with a default AnnotationAwareOrderComparator.
117         * <p>Optimized to skip sorting for lists with size 0 or 1,
118         * in order to avoid unnecessary array extraction.
119         * @param array the array to sort
120         * @see java.util.Arrays#sort(Object[], java.util.Comparator)
121         */
122        public static void sort(Object[] array) {
123                if (array.length > 1) {
124                        Arrays.sort(array, INSTANCE);
125                }
126        }
127
128        /**
129         * Sort the given array or List with a default AnnotationAwareOrderComparator,
130         * if necessary. Simply skips sorting when given any other value.
131         * <p>Optimized to skip sorting for lists with size 0 or 1,
132         * in order to avoid unnecessary array extraction.
133         * @param value the array or List to sort
134         * @see java.util.Arrays#sort(Object[], java.util.Comparator)
135         */
136        public static void sortIfNecessary(Object value) {
137                if (value instanceof Object[]) {
138                        sort((Object[]) value);
139                }
140                else if (value instanceof List) {
141                        sort((List<?>) value);
142                }
143        }
144
145}