Skip to content

Commit 299b9d6

Browse files
olegzjhoeller
authored andcommitted
SPR-15481 Fixed AnnotationUtils.getValue() operation
- Fixed AnnotationUtils.getValue() operation to ensure it re-throws AnnotationConfigurationException instead of swallowing it (as it is done in few other operations in AnnotationUtils) - Added test - Removed unnecessary '@SuppressWarnings("unchecked")'
1 parent 2579dab commit 299b9d6

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
* @author Mark Fisher
9898
* @author Chris Beams
9999
* @author Phillip Webb
100+
* @author Oleg Zhurakousky
100101
* @since 2.0
101102
* @see AliasFor
102103
* @see AnnotationAttributes
@@ -484,7 +485,6 @@ public static <A extends Annotation> A findAnnotation(AnnotatedElement annotated
484485
* @return the first matching annotation, or {@code null} if not found
485486
* @since 4.2
486487
*/
487-
@SuppressWarnings("unchecked")
488488
private static <A extends Annotation> A findAnnotation(
489489
AnnotatedElement annotatedElement, Class<A> annotationType, Set<Annotation> visited) {
490490
try {
@@ -673,7 +673,6 @@ private static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A>
673673
* @param visited the set of annotations that have already been visited
674674
* @return the first matching annotation, or {@code null} if not found
675675
*/
676-
@SuppressWarnings("unchecked")
677676
private static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType, Set<Annotation> visited) {
678677
try {
679678
A annotation = clazz.getDeclaredAnnotation(annotationType);
@@ -1294,8 +1293,11 @@ public static Object getValue(Annotation annotation) {
12941293
* Retrieve the <em>value</em> of a named attribute, given an annotation instance.
12951294
* @param annotation the annotation instance from which to retrieve the value
12961295
* @param attributeName the name of the attribute value to retrieve
1297-
* @return the attribute value, or {@code null} if not found
1296+
* @return the attribute value, or {@code null} if not found unless the the attribute value
1297+
* can not be retrieved due to {@link AnnotationConfigurationException}, in which case it
1298+
* will be re-thrown
12981299
* @see #getValue(Annotation)
1300+
* @see #rethrowAnnotationConfigurationException(Throwable)
12991301
*/
13001302
public static Object getValue(Annotation annotation, String attributeName) {
13011303
if (annotation == null || !StringUtils.hasText(attributeName)) {
@@ -1306,6 +1308,10 @@ public static Object getValue(Annotation annotation, String attributeName) {
13061308
ReflectionUtils.makeAccessible(method);
13071309
return method.invoke(annotation);
13081310
}
1311+
catch (InvocationTargetException ex) {
1312+
rethrowAnnotationConfigurationException(ex.getTargetException());
1313+
throw new IllegalStateException("Could not obtain annotation attribute value of " + attributeName, ex);
1314+
}
13091315
catch (Exception ex) {
13101316
return null;
13111317
}

spring-core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
* @author Sam Brannen
5555
* @author Chris Beams
5656
* @author Phillip Webb
57+
* @author Oleg Zhurakousky
5758
*/
5859
public class AnnotationUtilsTests {
5960

@@ -1239,6 +1240,14 @@ public void synthesizeAnnotationFromDefaultsWithAttributeAliases() throws Except
12391240
assertEquals("value: ", "", contextConfig.value());
12401241
assertEquals("location: ", "", contextConfig.location());
12411242
}
1243+
1244+
@ContextConfig(value="foo", location="bar")
1245+
@Test(expected=AnnotationConfigurationException.class)
1246+
public void synthesizeAnnotationWithAttributeAliasesDifferentValues() throws Exception {
1247+
Method m = AnnotationUtilsTests.class.getDeclaredMethod("synthesizeAnnotationWithAttributeAliasesDifferentValues");
1248+
Annotation a = synthesizeAnnotation(m.getDeclaredAnnotation(ContextConfig.class));
1249+
getValue(a);
1250+
}
12421251

12431252
@Test
12441253
public void synthesizeAnnotationFromMapWithMinimalAttributesWithAttributeAliases() throws Exception {

0 commit comments

Comments
 (0)