Browse Source
This commit rationalizes the use of @Order so that the standard @Priority annotation can be used instead. The handling of both annotations are now defined in OrderUtils. This also updates the link to the JavaEE API so that we refer to JavaEE7 instead of JavaEE6. Issue: SPR-11639pull/461/merge
12 changed files with 211 additions and 61 deletions
@ -0,0 +1,75 @@
@@ -0,0 +1,75 @@
|
||||
/* |
||||
* Copyright 2002-2014 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.annotation; |
||||
|
||||
import java.lang.annotation.Annotation; |
||||
|
||||
import org.springframework.util.ClassUtils; |
||||
|
||||
/** |
||||
* General utility for determining the order of an object based |
||||
* on its type declaration. |
||||
* |
||||
* @author Stephane Nicoll |
||||
* @since 4.1 |
||||
* @see Order |
||||
* @see javax.annotation.Priority |
||||
*/ |
||||
public abstract class OrderUtils { |
||||
|
||||
private static final String PRIORITY_ANNOTATION_CLASS_NAME = "javax.annotation.Priority"; |
||||
|
||||
private static final boolean priorityPresent = |
||||
ClassUtils.isPresent(PRIORITY_ANNOTATION_CLASS_NAME, OrderUtils.class.getClassLoader()); |
||||
|
||||
/** |
||||
* Return the order on the specified {@code type} or the specified |
||||
* default value if none can be found. |
||||
* <p>Take care of {@link Order @Order} and {@code @javax.annotation.Priority}. |
||||
* @param type the type to handle |
||||
* @return the priority value of the default if none can be found |
||||
*/ |
||||
public static Integer getOrder(Class<?> type, Integer defaultOrder) { |
||||
Order order = AnnotationUtils.findAnnotation(type, Order.class); |
||||
if (order != null) { |
||||
return order.value(); |
||||
} |
||||
Integer priorityOrder = getPriorityValue(type); |
||||
if (priorityOrder != null) { |
||||
return priorityOrder; |
||||
} |
||||
return defaultOrder; |
||||
} |
||||
|
||||
/** |
||||
* Return the value of the {@code javax.annotation.Priority} annotation set on the |
||||
* specified type or {@code null} if none is set. |
||||
* @param type the type to handle |
||||
* @return the priority value if the annotation is set, {@code null} otherwise |
||||
*/ |
||||
public static Integer getPriorityValue(Class<?> type) { |
||||
if (priorityPresent) { |
||||
for (Annotation annotation : type.getAnnotations()) { |
||||
if (PRIORITY_ANNOTATION_CLASS_NAME.equals(annotation.annotationType().getName())) { |
||||
return (Integer) AnnotationUtils.getValue(annotation); |
||||
} |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,73 @@
@@ -0,0 +1,73 @@
|
||||
/* |
||||
* Copyright 2002-2014 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.annotation; |
||||
|
||||
import static org.junit.Assert.*; |
||||
|
||||
import javax.annotation.Priority; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
/** |
||||
* |
||||
* @author Stephane Nicoll |
||||
*/ |
||||
public class OrderUtilsTests { |
||||
|
||||
@Test |
||||
public void getSimpleOrder() { |
||||
assertEquals(Integer.valueOf(50), OrderUtils.getOrder(SimpleOrder.class, null)); |
||||
} |
||||
|
||||
@Test |
||||
public void getPriorityOrder() { |
||||
assertEquals(Integer.valueOf(55), OrderUtils.getOrder(SimplePriority.class, null)); |
||||
} |
||||
|
||||
@Test |
||||
public void getOrderWithBoth() { |
||||
assertEquals(Integer.valueOf(50), OrderUtils.getOrder(OrderAndPriority.class, null)); |
||||
} |
||||
|
||||
@Test |
||||
public void getDefaultOrder() { |
||||
assertEquals(Integer.valueOf(33), OrderUtils.getOrder(NoOrder.class, 33)); |
||||
} |
||||
|
||||
@Test |
||||
public void getPriorityValueNoAnnotation() { |
||||
assertNull(OrderUtils.getPriorityValue(SimpleOrder.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void getPriorityValue() { |
||||
assertEquals(Integer.valueOf(55), OrderUtils.getPriorityValue(OrderAndPriority.class)); |
||||
} |
||||
|
||||
@Order(50) |
||||
private static class SimpleOrder {} |
||||
|
||||
@Priority(55) |
||||
private static class SimplePriority {} |
||||
|
||||
@Order(50) |
||||
@Priority(55) |
||||
private static class OrderAndPriority {} |
||||
|
||||
private static class NoOrder {} |
||||
|
||||
} |
||||
Loading…
Reference in new issue