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:
parent
23f089ff1e
commit
e543ffdfd7
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2012 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.
|
||||
|
@ -211,7 +211,7 @@ public class FormattingConversionService extends GenericConversionService
|
|||
}
|
||||
|
||||
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
return sourceType.getAnnotation(annotationType) != null;
|
||||
return sourceType.hasAnnotation(annotationType);
|
||||
}
|
||||
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
|
@ -251,7 +251,7 @@ public class FormattingConversionService extends GenericConversionService
|
|||
}
|
||||
|
||||
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
return targetType.getAnnotation(annotationType) != null;
|
||||
return targetType.hasAnnotation(annotationType);
|
||||
}
|
||||
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2012 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.
|
||||
|
@ -191,15 +191,17 @@ public class TypeDescriptorTests {
|
|||
assertEquals(String.class, t1.getType());
|
||||
assertEquals(1, t1.getAnnotations().length);
|
||||
assertNotNull(t1.getAnnotation(ParameterAnnotation.class));
|
||||
assertTrue(t1.hasAnnotation(ParameterAnnotation.class));
|
||||
assertEquals(123, t1.getAnnotation(ParameterAnnotation.class).value());
|
||||
}
|
||||
|
||||
@Target({ElementType.PARAMETER})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ParameterAnnotation {
|
||||
|
||||
int value();
|
||||
}
|
||||
|
||||
public void testAnnotatedMethod(@ParameterAnnotation String parameter) {
|
||||
public void testAnnotatedMethod(@ParameterAnnotation(123) String parameter) {
|
||||
|
||||
}
|
||||
|
||||
|
@ -297,6 +299,7 @@ public class TypeDescriptorTests {
|
|||
assertEquals(List.class, desc.getType());
|
||||
assertEquals(Integer.class, desc.getElementTypeDescriptor().getType());
|
||||
assertNotNull(desc.getAnnotation(MethodAnnotation1.class));
|
||||
assertTrue(desc.hasAnnotation(MethodAnnotation1.class));
|
||||
}
|
||||
|
||||
public static class GenericClass<T> {
|
||||
|
|
Loading…
Reference in New Issue