AnnotationAwareOrderComparator is able to sort Class objects as well

Issue: SPR-10152
This commit is contained in:
Juergen Hoeller
2013-01-10 16:55:32 +01:00
committed by unknown
parent dae4485155
commit e806c4eb3d
2 changed files with 40 additions and 6 deletions

View File

@@ -50,7 +50,8 @@ public class AnnotationAwareOrderComparator extends OrderComparator {
return ((Ordered) obj).getOrder(); return ((Ordered) obj).getOrder();
} }
if (obj != null) { 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) { if (order != null) {
return order.value(); return order.value();
} }

View File

@@ -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"); * 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.
@@ -13,16 +13,19 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.core.annotation; package org.springframework.core.annotation;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.Test;
/** /**
* Unit tests for {@link AnnotationAwareOrderComparator}. * @author Juergen Hoeller
*
* @author Oliver Gierke * @author Oliver Gierke
*/ */
public class AnnotationAwareOrderComparatorTests { public class AnnotationAwareOrderComparatorTests {
@@ -31,4 +34,34 @@ public class AnnotationAwareOrderComparatorTests {
public void instanceVariableIsAnAnnotationAwareOrderComparator() { public void instanceVariableIsAnAnnotationAwareOrderComparator() {
assertThat(AnnotationAwareOrderComparator.INSTANCE, is(instanceOf(AnnotationAwareOrderComparator.class))); 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 {
}
} }