001/*
002 * Copyright 2002-2016 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.annotation.Annotation;
020
021import org.springframework.util.ClassUtils;
022
023/**
024 * General utility for determining the order of an object based on its type declaration.
025 * Handles Spring's {@link Order} annotation as well as {@link javax.annotation.Priority}.
026 *
027 * @author Stephane Nicoll
028 * @author Juergen Hoeller
029 * @since 4.1
030 * @see Order
031 * @see javax.annotation.Priority
032 */
033@SuppressWarnings("unchecked")
034public abstract class OrderUtils {
035
036        private static Class<? extends Annotation> priorityAnnotationType = null;
037
038        static {
039                try {
040                        priorityAnnotationType = (Class<? extends Annotation>)
041                                        ClassUtils.forName("javax.annotation.Priority", OrderUtils.class.getClassLoader());
042                }
043                catch (Throwable ex) {
044                        // javax.annotation.Priority not available, or present but not loadable (on JDK 6)
045                }
046        }
047
048
049        /**
050         * Return the order on the specified {@code type}.
051         * <p>Takes care of {@link Order @Order} and {@code @javax.annotation.Priority}.
052         * @param type the type to handle
053         * @return the order value, or {@code null} if none can be found
054         * @see #getPriority(Class)
055         */
056        public static Integer getOrder(Class<?> type) {
057                return getOrder(type, null);
058        }
059
060        /**
061         * Return the order on the specified {@code type}, or the specified
062         * default value if none can be found.
063         * <p>Takes care of {@link Order @Order} and {@code @javax.annotation.Priority}.
064         * @param type the type to handle
065         * @return the priority value, or the specified default order if none can be found
066         * @see #getPriority(Class)
067         */
068        public static Integer getOrder(Class<?> type, Integer defaultOrder) {
069                Order order = AnnotationUtils.findAnnotation(type, Order.class);
070                if (order != null) {
071                        return order.value();
072                }
073                Integer priorityOrder = getPriority(type);
074                if (priorityOrder != null) {
075                        return priorityOrder;
076                }
077                return defaultOrder;
078        }
079
080        /**
081         * Return the value of the {@code javax.annotation.Priority} annotation
082         * declared on the specified type, or {@code null} if none.
083         * @param type the type to handle
084         * @return the priority value if the annotation is declared, or {@code null} if none
085         */
086        public static Integer getPriority(Class<?> type) {
087                if (priorityAnnotationType != null) {
088                        Annotation priority = AnnotationUtils.findAnnotation(type, priorityAnnotationType);
089                        if (priority != null) {
090                                return (Integer) AnnotationUtils.getValue(priority);
091                        }
092                }
093                return null;
094        }
095
096}