Browse Source

AnnotationAwareOrderComparator is able to sort Class objects as well

Issue: SPR-10152
pull/214/merge
Juergen Hoeller 12 years ago committed by unknown
parent
commit
e806c4eb3d
  1. 3
      spring-core/src/main/java/org/springframework/core/annotation/AnnotationAwareOrderComparator.java
  2. 43
      spring-core/src/test/java/org/springframework/core/annotation/AnnotationAwareOrderComparatorTests.java

3
spring-core/src/main/java/org/springframework/core/annotation/AnnotationAwareOrderComparator.java

@ -50,7 +50,8 @@ public class AnnotationAwareOrderComparator extends OrderComparator { @@ -50,7 +50,8 @@ public class AnnotationAwareOrderComparator extends OrderComparator {
return ((Ordered) obj).getOrder();
}
if (obj != null) {
Order order = obj.getClass().getAnnotation(Order.class);
Class<?> clazz = (obj instanceof Class ? (Class) obj : obj.getClass());
Order order = clazz.getAnnotation(Order.class);
if (order != null) {
return order.value();
}

43
spring-core/src/test/java/org/springframework/core/annotation/AnnotationAwareOrderComparatorTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
@ -13,16 +13,19 @@ @@ -13,16 +13,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.annotation;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
/**
* Unit tests for {@link AnnotationAwareOrderComparator}.
*
* @author Juergen Hoeller
* @author Oliver Gierke
*/
public class AnnotationAwareOrderComparatorTests {
@ -31,4 +34,34 @@ public class AnnotationAwareOrderComparatorTests { @@ -31,4 +34,34 @@ public class AnnotationAwareOrderComparatorTests {
public void instanceVariableIsAnAnnotationAwareOrderComparator() {
assertThat(AnnotationAwareOrderComparator.INSTANCE, is(instanceOf(AnnotationAwareOrderComparator.class)));
}
@Test
public void sortInstances() {
List<Object> list = new ArrayList<>();
list.add(new B());
list.add(new A());
AnnotationAwareOrderComparator.sort(list);
assertTrue(list.get(0) instanceof A);
assertTrue(list.get(1) instanceof B);
}
@Test
public void sortClasses() {
List<Object> list = new ArrayList<>();
list.add(B.class);
list.add(A.class);
AnnotationAwareOrderComparator.sort(list);
assertEquals(A.class, list.get(0));
assertEquals(B.class, list.get(1));
}
@Order(1)
private static class A {
}
@Order(2)
private static class B {
}
}

Loading…
Cancel
Save