Browse Source

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
pull/134/merge
Phillip Webb 13 years ago
parent
commit
e543ffdfd7
  1. 6
      spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java
  2. 14
      spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java
  3. 9
      spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java

6
spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java

@ -1,5 +1,5 @@ @@ -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 @@ -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 @@ -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) {

14
spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java

@ -271,15 +271,25 @@ public class TypeDescriptor { @@ -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;

9
spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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 { @@ -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…
Cancel
Save