Made findMethodAnnotationOnBridgeMethod compatible with Java 8

This commit is contained in:
Juergen Hoeller 2014-01-05 02:30:04 +01:00
parent 8c8eead667
commit 1a0a381559
1 changed files with 37 additions and 53 deletions

View File

@ -16,15 +16,11 @@
package org.springframework.core.annotation; package org.springframework.core.annotation;
import static org.junit.Assert.*;
import static org.springframework.core.annotation.AnnotationUtils.*;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.annotation.Inherited; import java.lang.annotation.Inherited;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -33,6 +29,9 @@ import org.junit.Test;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import static org.junit.Assert.*;
import static org.springframework.core.annotation.AnnotationUtils.*;
/** /**
* Unit tests for {@link AnnotationUtils}. * Unit tests for {@link AnnotationUtils}.
* *
@ -44,54 +43,49 @@ import org.springframework.stereotype.Component;
public class AnnotationUtilsTests { public class AnnotationUtilsTests {
@Test @Test
public void testFindMethodAnnotationOnLeaf() throws SecurityException, NoSuchMethodException { public void findMethodAnnotationOnLeaf() throws Exception {
Method m = Leaf.class.getMethod("annotatedOnLeaf", (Class[]) null);
final Method m = Leaf.class.getMethod("annotatedOnLeaf", (Class[]) null);
assertNotNull(m.getAnnotation(Order.class)); assertNotNull(m.getAnnotation(Order.class));
assertNotNull(getAnnotation(m, Order.class)); assertNotNull(getAnnotation(m, Order.class));
assertNotNull(findAnnotation(m, Order.class)); assertNotNull(findAnnotation(m, Order.class));
} }
@Test @Test
public void testFindMethodAnnotationOnRoot() throws SecurityException, NoSuchMethodException { public void findMethodAnnotationOnRoot() throws Exception {
Method m = Leaf.class.getMethod("annotatedOnRoot", (Class[]) null);
final Method m = Leaf.class.getMethod("annotatedOnRoot", (Class[]) null);
assertNotNull(m.getAnnotation(Order.class)); assertNotNull(m.getAnnotation(Order.class));
assertNotNull(getAnnotation(m, Order.class)); assertNotNull(getAnnotation(m, Order.class));
assertNotNull(findAnnotation(m, Order.class)); assertNotNull(findAnnotation(m, Order.class));
} }
@Test @Test
public void testFindMethodAnnotationOnRootButOverridden() throws SecurityException, NoSuchMethodException { public void findMethodAnnotationOnRootButOverridden() throws Exception {
Method m = Leaf.class.getMethod("overrideWithoutNewAnnotation", (Class[]) null);
final Method m = Leaf.class.getMethod("overrideWithoutNewAnnotation", (Class[]) null);
assertNull(m.getAnnotation(Order.class)); assertNull(m.getAnnotation(Order.class));
assertNull(getAnnotation(m, Order.class)); assertNull(getAnnotation(m, Order.class));
assertNotNull(findAnnotation(m, Order.class)); assertNotNull(findAnnotation(m, Order.class));
} }
@Test @Test
public void testFindMethodAnnotationNotAnnotated() throws SecurityException, NoSuchMethodException { public void findMethodAnnotationNotAnnotated() throws Exception {
Method m = Leaf.class.getMethod("notAnnotated", (Class[]) null);
final Method m = Leaf.class.getMethod("notAnnotated", (Class[]) null);
assertNull(findAnnotation(m, Order.class)); assertNull(findAnnotation(m, Order.class));
} }
@Test @Test
public void testFindMethodAnnotationOnBridgeMethod() throws Exception { public void findMethodAnnotationOnBridgeMethod() throws Exception {
Method m = SimpleFoo.class.getMethod("something", Object.class);
final Method m = SimpleFoo.class.getMethod("something", Object.class);
assertTrue(m.isBridge()); assertTrue(m.isBridge());
assertNull(m.getAnnotation(Order.class)); assertNull(m.getAnnotation(Order.class));
assertNull(getAnnotation(m, Order.class)); assertNull(getAnnotation(m, Order.class));
assertNotNull(findAnnotation(m, Order.class)); assertNotNull(findAnnotation(m, Order.class));
assertNull(m.getAnnotation(Transactional.class)); // TODO: actually found on OpenJDK 8 b99! assertNull(m.getAnnotation(Transactional.class));
assertNotNull(getAnnotation(m, Transactional.class)); assertNotNull(getAnnotation(m, Transactional.class));
assertNotNull(findAnnotation(m, Transactional.class)); assertNotNull(findAnnotation(m, Transactional.class));
} }
// TODO consider whether we want this to handle annotations on interfaces // TODO consider whether we want this to handle annotations on interfaces
// public void testFindMethodAnnotationFromInterfaceImplementedByRoot() // public void findMethodAnnotationFromInterfaceImplementedByRoot()
// throws Exception { // throws Exception {
// Method m = Leaf.class.getMethod("fromInterfaceImplementedByRoot", // Method m = Leaf.class.getMethod("fromInterfaceImplementedByRoot",
// (Class[]) null); // (Class[]) null);
@ -101,7 +95,6 @@ public class AnnotationUtilsTests {
@Test @Test
public void testFindAnnotationDeclaringClass() throws Exception { public void testFindAnnotationDeclaringClass() throws Exception {
// no class-level annotation // no class-level annotation
assertNull(findAnnotationDeclaringClass(Transactional.class, NonAnnotatedInterface.class)); assertNull(findAnnotationDeclaringClass(Transactional.class, NonAnnotatedInterface.class));
assertNull(findAnnotationDeclaringClass(Transactional.class, NonAnnotatedClass.class)); assertNull(findAnnotationDeclaringClass(Transactional.class, NonAnnotatedClass.class));
@ -128,7 +121,6 @@ public class AnnotationUtilsTests {
@Test @Test
public void findAnnotationDeclaringClassForTypesWithSingleCandidateType() { public void findAnnotationDeclaringClassForTypesWithSingleCandidateType() {
// no class-level annotation // no class-level annotation
List<Class<? extends Annotation>> transactionalCandidateList = Arrays.<Class<? extends Annotation>> asList(Transactional.class); List<Class<? extends Annotation>> transactionalCandidateList = Arrays.<Class<? extends Annotation>> asList(Transactional.class);
assertNull(findAnnotationDeclaringClassForTypes(transactionalCandidateList, NonAnnotatedInterface.class)); assertNull(findAnnotationDeclaringClassForTypes(transactionalCandidateList, NonAnnotatedInterface.class));
@ -137,8 +129,7 @@ public class AnnotationUtilsTests {
// inherited class-level annotation; note: @Transactional is inherited // inherited class-level annotation; note: @Transactional is inherited
assertEquals(InheritedAnnotationInterface.class, assertEquals(InheritedAnnotationInterface.class,
findAnnotationDeclaringClassForTypes(transactionalCandidateList, InheritedAnnotationInterface.class)); findAnnotationDeclaringClassForTypes(transactionalCandidateList, InheritedAnnotationInterface.class));
assertNull(findAnnotationDeclaringClassForTypes(transactionalCandidateList, assertNull(findAnnotationDeclaringClassForTypes(transactionalCandidateList, SubInheritedAnnotationInterface.class));
SubInheritedAnnotationInterface.class));
assertEquals(InheritedAnnotationClass.class, assertEquals(InheritedAnnotationClass.class,
findAnnotationDeclaringClassForTypes(transactionalCandidateList, InheritedAnnotationClass.class)); findAnnotationDeclaringClassForTypes(transactionalCandidateList, InheritedAnnotationClass.class));
assertEquals(InheritedAnnotationClass.class, assertEquals(InheritedAnnotationClass.class,
@ -158,9 +149,7 @@ public class AnnotationUtilsTests {
@Test @Test
public void findAnnotationDeclaringClassForTypesWithMultipleCandidateTypes() { public void findAnnotationDeclaringClassForTypesWithMultipleCandidateTypes() {
List<Class<? extends Annotation>> candidates = Arrays.<Class<? extends Annotation>> asList(Transactional.class, Order.class);
List<Class<? extends Annotation>> candidates = Arrays.<Class<? extends Annotation>> asList(Transactional.class,
Order.class);
// no class-level annotation // no class-level annotation
assertNull(findAnnotationDeclaringClassForTypes(candidates, NonAnnotatedInterface.class)); assertNull(findAnnotationDeclaringClassForTypes(candidates, NonAnnotatedInterface.class));
@ -196,7 +185,6 @@ public class AnnotationUtilsTests {
@Test @Test
public void testIsAnnotationDeclaredLocally() throws Exception { public void testIsAnnotationDeclaredLocally() throws Exception {
// no class-level annotation // no class-level annotation
assertFalse(isAnnotationDeclaredLocally(Transactional.class, NonAnnotatedInterface.class)); assertFalse(isAnnotationDeclaredLocally(Transactional.class, NonAnnotatedInterface.class));
assertFalse(isAnnotationDeclaredLocally(Transactional.class, NonAnnotatedClass.class)); assertFalse(isAnnotationDeclaredLocally(Transactional.class, NonAnnotatedClass.class));
@ -216,7 +204,6 @@ public class AnnotationUtilsTests {
@Test @Test
public void testIsAnnotationInherited() throws Exception { public void testIsAnnotationInherited() throws Exception {
// no class-level annotation // no class-level annotation
assertFalse(isAnnotationInherited(Transactional.class, NonAnnotatedInterface.class)); assertFalse(isAnnotationInherited(Transactional.class, NonAnnotatedInterface.class));
assertFalse(isAnnotationInherited(Transactional.class, NonAnnotatedClass.class)); assertFalse(isAnnotationInherited(Transactional.class, NonAnnotatedClass.class));
@ -238,51 +225,48 @@ public class AnnotationUtilsTests {
} }
@Test @Test
public void testGetValueFromAnnotation() throws Exception { public void getValueFromAnnotation() throws Exception {
Method method = SimpleFoo.class.getMethod("something", Object.class);
final Method method = SimpleFoo.class.getMethod("something", Object.class); Order order = findAnnotation(method, Order.class);
final Order order = findAnnotation(method, Order.class);
assertEquals(1, AnnotationUtils.getValue(order, AnnotationUtils.VALUE)); assertEquals(1, AnnotationUtils.getValue(order, AnnotationUtils.VALUE));
assertEquals(1, AnnotationUtils.getValue(order)); assertEquals(1, AnnotationUtils.getValue(order));
} }
@Test @Test
public void testGetDefaultValueFromAnnotation() throws Exception { public void getValueFromNonPublicAnnotation() throws Exception {
Method method = SimpleFoo.class.getMethod("something", Object.class);
final Method method = SimpleFoo.class.getMethod("something", Object.class); Order order = findAnnotation(method, Order.class);
final Order order = findAnnotation(method, Order.class);
assertEquals(Ordered.LOWEST_PRECEDENCE, AnnotationUtils.getDefaultValue(order, AnnotationUtils.VALUE)); assertEquals(Ordered.LOWEST_PRECEDENCE, AnnotationUtils.getDefaultValue(order, AnnotationUtils.VALUE));
assertEquals(Ordered.LOWEST_PRECEDENCE, AnnotationUtils.getDefaultValue(order)); assertEquals(Ordered.LOWEST_PRECEDENCE, AnnotationUtils.getDefaultValue(order));
} }
@Test @Test
public void testGetDefaultValueFromAnnotationType() throws Exception { public void getDefaultValueFromAnnotation() throws Exception {
Method method = SimpleFoo.class.getMethod("something", Object.class);
Order order = findAnnotation(method, Order.class);
assertEquals(Ordered.LOWEST_PRECEDENCE, AnnotationUtils.getDefaultValue(Order.class, AnnotationUtils.VALUE)); assertEquals(Ordered.LOWEST_PRECEDENCE, AnnotationUtils.getDefaultValue(order, AnnotationUtils.VALUE));
assertEquals(Ordered.LOWEST_PRECEDENCE, AnnotationUtils.getDefaultValue(Order.class)); assertEquals(Ordered.LOWEST_PRECEDENCE, AnnotationUtils.getDefaultValue(order));
} }
@Test @Test
public void testFindAnnotationFromInterface() throws Exception { public void getDefaultValueFromNonPublicAnnotation() throws Exception {
Method method = ImplementsInterfaceWithAnnotatedMethod.class.getMethod("foo"); Method method = ImplementsInterfaceWithAnnotatedMethod.class.getMethod("foo");
Order order = findAnnotation(method, Order.class); Order order = findAnnotation(method, Order.class);
assertNotNull(order); assertNotNull(order);
} }
@Test @Test
public void testFindAnnotationFromInterfaceOnSuper() throws Exception { public void findAnnotationFromInterfaceOnSuper() throws Exception {
Method method = SubOfImplementsInterfaceWithAnnotatedMethod.class.getMethod("foo"); Method method = SubOfImplementsInterfaceWithAnnotatedMethod.class.getMethod("foo");
Order order = findAnnotation(method, Order.class); Order order = findAnnotation(method, Order.class);
assertNotNull(order); assertNotNull(order);
} }
@Test @Test
public void testFindAnnotationFromInterfaceWhenSuperDoesNotImplementMethod() throws Exception { public void findAnnotationFromInterfaceWhenSuperDoesNotImplementMethod() throws Exception {
Method method = SubOfAbstractImplementsInterfaceWithAnnotatedMethod.class.getMethod("foo"); Method method = SubOfAbstractImplementsInterfaceWithAnnotatedMethod.class.getMethod("foo");
Order order = findAnnotation(method, Order.class); Order order = findAnnotation(method, Order.class);
assertNotNull(order); assertNotNull(order);
@ -356,6 +340,12 @@ public class AnnotationUtilsTests {
} }
} }
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@interface Transactional {
}
public static abstract class Foo<T> { public static abstract class Foo<T> {
@Order(1) @Order(1)
@ -449,9 +439,3 @@ public class AnnotationUtilsTests {
} }
} }
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@interface Transactional {
}