From e806c4eb3d5bd660c169e2b215a4e29b1f43bf35 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 10 Jan 2013 16:55:32 +0100 Subject: [PATCH] AnnotationAwareOrderComparator is able to sort Class objects as well Issue: SPR-10152 --- .../AnnotationAwareOrderComparator.java | 3 +- .../AnnotationAwareOrderComparatorTests.java | 43 ++++++++++++++++--- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAwareOrderComparator.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAwareOrderComparator.java index bf7232e10a..c9d1fe47c5 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAwareOrderComparator.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAwareOrderComparator.java @@ -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(); } diff --git a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationAwareOrderComparatorTests.java b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationAwareOrderComparatorTests.java index 9bdd19f54b..47634bb760 100644 --- a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationAwareOrderComparatorTests.java +++ b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationAwareOrderComparatorTests.java @@ -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 @@ * 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 { public void instanceVariableIsAnAnnotationAwareOrderComparator() { assertThat(AnnotationAwareOrderComparator.INSTANCE, is(instanceOf(AnnotationAwareOrderComparator.class))); } + + @Test + public void sortInstances() { + List 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 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 { + } + }