diff --git a/spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java b/spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java
index f6e9ce9fd1..cb6aa8623c 100644
--- a/spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java
+++ b/spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java
@@ -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) {
diff --git a/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java b/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java
index e49cb244ff..58585070a8 100644
--- a/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java
+++ b/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java
@@ -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 true 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 getAnnotation(Class annotationType) {
for (Annotation annotation : getAnnotations()) {
if (annotation.annotationType().equals(annotationType)) {
- return annotation;
+ return (T) annotation;
}
}
return null;
diff --git a/spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java b/spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java
index 94e2379e94..20cefeeec3 100644
--- a/spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java
+++ b/spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java
@@ -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 {