diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java index 706921e4d2..701a8d3d01 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java @@ -97,6 +97,7 @@ import org.springframework.util.StringUtils; * @author Mark Fisher * @author Chris Beams * @author Phillip Webb + * @author Oleg Zhurakousky * @since 2.0 * @see AliasFor * @see AnnotationAttributes @@ -484,7 +485,6 @@ public abstract class AnnotationUtils { * @return the first matching annotation, or {@code null} if not found * @since 4.2 */ - @SuppressWarnings("unchecked") private static A findAnnotation( AnnotatedElement annotatedElement, Class annotationType, Set visited) { try { @@ -673,7 +673,6 @@ public abstract class AnnotationUtils { * @param visited the set of annotations that have already been visited * @return the first matching annotation, or {@code null} if not found */ - @SuppressWarnings("unchecked") private static A findAnnotation(Class clazz, Class annotationType, Set visited) { try { A annotation = clazz.getDeclaredAnnotation(annotationType); @@ -1294,8 +1293,11 @@ public abstract class AnnotationUtils { * Retrieve the value of a named attribute, given an annotation instance. * @param annotation the annotation instance from which to retrieve the value * @param attributeName the name of the attribute value to retrieve - * @return the attribute value, or {@code null} if not found + * @return the attribute value, or {@code null} if not found unless the the attribute value + * can not be retrieved due to {@link AnnotationConfigurationException}, in which case it + * will be re-thrown * @see #getValue(Annotation) + * @see #rethrowAnnotationConfigurationException(Throwable) */ public static Object getValue(Annotation annotation, String attributeName) { if (annotation == null || !StringUtils.hasText(attributeName)) { @@ -1306,6 +1308,10 @@ public abstract class AnnotationUtils { ReflectionUtils.makeAccessible(method); return method.invoke(annotation); } + catch (InvocationTargetException ex) { + rethrowAnnotationConfigurationException(ex.getTargetException()); + throw new IllegalStateException("Could not obtain annotation attribute value of " + attributeName, ex); + } catch (Exception ex) { return null; } diff --git a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java index e127215c15..af4670a40a 100644 --- a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java @@ -54,6 +54,7 @@ import static org.springframework.core.annotation.AnnotationUtils.*; * @author Sam Brannen * @author Chris Beams * @author Phillip Webb + * @author Oleg Zhurakousky */ public class AnnotationUtilsTests { @@ -1239,6 +1240,14 @@ public class AnnotationUtilsTests { assertEquals("value: ", "", contextConfig.value()); assertEquals("location: ", "", contextConfig.location()); } + + @ContextConfig(value="foo", location="bar") + @Test(expected=AnnotationConfigurationException.class) + public void synthesizeAnnotationWithAttributeAliasesDifferentValues() throws Exception { + Method m = AnnotationUtilsTests.class.getDeclaredMethod("synthesizeAnnotationWithAttributeAliasesDifferentValues"); + Annotation a = synthesizeAnnotation(m.getDeclaredAnnotation(ContextConfig.class)); + getValue(a); + } @Test public void synthesizeAnnotationFromMapWithMinimalAttributesWithAttributeAliases() throws Exception {