Improve annotation methods in TypeDescriptor

- Use generic typing for getAnnotation()
- Add hasAnnoation() method
- Update existing code and tests to make use of changes

Issue: SPR-9744
This commit is contained in:
Phillip Webb
2012-09-06 12:05:02 -07:00
parent 23f089ff1e
commit e543ffdfd7
3 changed files with 21 additions and 8 deletions

View File

@@ -271,15 +271,25 @@ public class TypeDescriptor {
return this.annotations;
}
/**
* Determine if this type descriptor has the specified annotation.
* @param annotationType the annotation type
* @return <tt>true</tt> if the annotation is present
*/
public boolean hasAnnotation(Class<? extends Annotation> annotationType) {
return getAnnotation(annotationType) != null;
}
/**
* Obtain the annotation associated with this type descriptor of the specified type.
* @param annotationType the annotation type
* @return the annotation, or null if no such annotation exists on this type descriptor
*/
public Annotation getAnnotation(Class<? extends Annotation> annotationType) {
@SuppressWarnings("unchecked")
public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
for (Annotation annotation : getAnnotations()) {
if (annotation.annotationType().equals(annotationType)) {
return annotation;
return (T) annotation;
}
}
return null;