Overhaul AnnotatedElementUtils

- Methods which search for a specific annotation now properly ensure
   that the sought annotation was actually found.

 - Both the "get" and the "find" search algorithms no longer needlessly
   traverse meta-annotation hierarchies twice.

 - Both the "get" and the "find" search algorithms now properly
   increment the metaDepth when recursively searching within the
   meta-annotation hierarchy.

 - Redesigned getMetaAnnotationTypes() so that it doesn't needlessly
   search irrelevant annotations.

 - Documented and tested hasMetaAnnotationTypes().

 - Documented isAnnotated().

Issue: SPR-11514
This commit is contained in:
Sam Brannen
2015-05-05 23:35:00 +02:00
parent 8853107f76
commit ba84458c65
4 changed files with 135 additions and 59 deletions
@@ -62,11 +62,33 @@ public class AnnotatedElementUtilsTests {
@Test
public void getMetaAnnotationTypesOnClassWithMetaDepth2() {
Set<String> names = getMetaAnnotationTypes(ComposedTransactionalComponentClass.class,
ComposedTransactionalComponent.class);
Set<String> names = getMetaAnnotationTypes(ComposedTransactionalComponentClass.class, ComposedTransactionalComponent.class);
assertEquals(names(TransactionalComponent.class, Transactional.class, Component.class, Retention.class, Documented.class, Target.class, Inherited.class), names);
}
@Test
public void hasMetaAnnotationTypesOnNonAnnotatedClass() {
assertFalse(hasMetaAnnotationTypes(NonAnnotatedClass.class, Transactional.class.getName()));
}
@Test
public void hasMetaAnnotationTypesOnClassWithMetaDepth0() {
assertFalse(hasMetaAnnotationTypes(TransactionalComponentClass.class, TransactionalComponent.class.getName()));
}
@Test
public void hasMetaAnnotationTypesOnClassWithMetaDepth1() {
assertTrue(hasMetaAnnotationTypes(TransactionalComponentClass.class, Transactional.class.getName()));
assertTrue(hasMetaAnnotationTypes(TransactionalComponentClass.class, Component.class.getName()));
}
@Test
public void hasMetaAnnotationTypesOnClassWithMetaDepth2() {
assertTrue(hasMetaAnnotationTypes(ComposedTransactionalComponentClass.class, Transactional.class.getName()));
assertTrue(hasMetaAnnotationTypes(ComposedTransactionalComponentClass.class, Component.class.getName()));
assertFalse(hasMetaAnnotationTypes(ComposedTransactionalComponentClass.class, ComposedTransactionalComponent.class.getName()));
}
@Test
public void getAllAnnotationAttributesOnClassWithLocalAnnotation() {
MultiValueMap<String, Object> attributes = getAllAnnotationAttributes(TxConfig.class,
@@ -277,6 +299,14 @@ public class AnnotatedElementUtilsTests {
assertNotNull("Should find @Order on StringGenericParameter.getFor() bridge method", attributes);
}
/** @since 4.2 */
@Test
public void findAnnotationAttributesOnClassWithMetaAndLocalTxConfig() {
AnnotationAttributes attributes = findAnnotationAttributes(MetaAndLocalTxConfigClass.class, Transactional.class);
assertNotNull("Should find @Transactional on MetaAndLocalTxConfigClass", attributes);
assertEquals("TX qualifier for MetaAndLocalTxConfigClass.", "localTxMgr", attributes.getString("qualifier"));
}
// -------------------------------------------------------------------------
@@ -315,6 +345,8 @@ public class AnnotatedElementUtilsTests {
String value() default "";
String qualifier() default "transactionManager";
boolean readOnly() default false;
}
@@ -333,6 +365,13 @@ public class AnnotatedElementUtilsTests {
@interface Composed2 {
}
@Transactional
@Retention(RetentionPolicy.RUNTIME)
@interface TxComposedWithOverride {
String qualifier() default "txMgr";
}
@Transactional("TxComposed1")
@Retention(RetentionPolicy.RUNTIME)
@interface TxComposed1 {
@@ -354,6 +393,14 @@ public class AnnotatedElementUtilsTests {
@interface ComposedTransactionalComponent {
}
@TxComposedWithOverride
// Override default "txMgr" from @TxComposedWithOverride with "localTxMgr"
@Transactional(qualifier = "localTxMgr")
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MetaAndLocalTxConfig {
}
// -------------------------------------------------------------------------
static class NonAnnotatedClass {
@@ -389,6 +436,10 @@ public class AnnotatedElementUtilsTests {
static class SubSubClassWithInheritedComposedAnnotation extends SubClassWithInheritedComposedAnnotation {
}
@MetaAndLocalTxConfig
static class MetaAndLocalTxConfigClass {
}
@Transactional("TxConfig")
static class TxConfig {
}