Skip to content

Commit

Permalink
Support arbitrary levels of meta-annotations in TypeDescriptor
Browse files Browse the repository at this point in the history
Prior to this commit, the `getAnnotation()` method in `TypeDescriptor`
only supported a single level of meta-annotations. In other words, the
annotation hierarchy would not be exhaustively searched.

This commit provides support for arbitrary levels of meta-annotations
in `TypeDescriptor` by delegating to `AnnotationUtils.findAnnotation()`
within `TypeDescriptor.getAnnotation()`.

Issue: SPR-12793
  • Loading branch information
sbrannen committed May 8, 2015
1 parent 5f03c97 commit 04d6afe
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
Expand Up @@ -27,6 +27,7 @@

import org.springframework.core.MethodParameter;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.lang.UsesJava8;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
Expand Down Expand Up @@ -237,6 +238,8 @@ public Annotation[] getAnnotations() {

/**
* Determine if this type descriptor has the specified annotation.
* <p>As of Spring Framework 4.2, this method supports arbitrary levels
* of meta-annotations.
* @param annotationType the annotation type
* @return <tt>true</tt> if the annotation is present
*/
Expand All @@ -245,19 +248,27 @@ public boolean hasAnnotation(Class<? extends Annotation> annotationType) {
}

/**
* Obtain the annotation associated with this type descriptor of the specified type.
* Obtain the annotation of the specified {@code annotationType} that is
* on this type descriptor.
* <p>As of Spring Framework 4.2, this method supports arbitrary levels
* of meta-annotations.
* @param annotationType the annotation type
* @return the annotation, or {@code null} if no such annotation exists on this type descriptor
*/
@SuppressWarnings("unchecked")
public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
// Search in annotations that are "present" (i.e., locally declared or inherited)
//
// NOTE: this unfortunately favors inherited annotations over locally declared composed annotations.
for (Annotation annotation : getAnnotations()) {
if (annotation.annotationType().equals(annotationType)) {
return (T) annotation;
}
}
for (Annotation metaAnn : getAnnotations()) {
T ann = metaAnn.annotationType().getAnnotation(annotationType);

// Search in annotation hierarchy
for (Annotation composedAnnotation : getAnnotations()) {
T ann = AnnotationUtils.findAnnotation(composedAnnotation.annotationType(), annotationType);
if (ann != null) {
return ann;
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
Expand All @@ -20,6 +20,7 @@
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand Down Expand Up @@ -50,6 +51,7 @@
* @author Keith Donald
* @author Andy Clement
* @author Phillip Webb
* @author Sam Brannen
*/
@SuppressWarnings("rawtypes")
public class TypeDescriptorTests {
Expand Down Expand Up @@ -369,22 +371,60 @@ public void setProperty(Map<List<Integer>, List<Long>> property) {
@MethodAnnotation3
private Map<List<Integer>, List<Long>> property;

@Target({ElementType.METHOD})

@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodAnnotation1 {

}

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodAnnotation2 {

}

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodAnnotation3 {
}

@MethodAnnotation1
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface ComposedMethodAnnotation1 {}

@ComposedMethodAnnotation1
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ComposedComposedMethodAnnotation1 {}

@MethodAnnotation1
public void methodWithLocalAnnotation() {}

@ComposedMethodAnnotation1
public void methodWithComposedAnnotation() {}

@ComposedComposedMethodAnnotation1
public void methodWithComposedComposedAnnotation() {}

private void assertAnnotationFoundOnMethod(Class<? extends Annotation> annotationType, String methodName) throws Exception {
TypeDescriptor typeDescriptor = new TypeDescriptor(new MethodParameter(getClass().getMethod(methodName), -1));
assertNotNull("Should have found @" + annotationType.getSimpleName() + " on " + methodName + ".",
typeDescriptor.getAnnotation(annotationType));
}

@Test
public void getAnnotationOnMethodThatIsLocallyAnnotated() throws Exception {
assertAnnotationFoundOnMethod(MethodAnnotation1.class, "methodWithLocalAnnotation");
}

@Test
public void getAnnotationOnMethodThatIsMetaAnnotated() throws Exception {
assertAnnotationFoundOnMethod(MethodAnnotation1.class, "methodWithComposedAnnotation");
}

@Test
public void getAnnotationOnMethodThatIsMetaMetaAnnotated() throws Exception {
assertAnnotationFoundOnMethod(MethodAnnotation1.class, "methodWithComposedComposedAnnotation");
}

@Test
Expand Down

0 comments on commit 04d6afe

Please sign in to comment.