Browse Source

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")'
pull/1407/head
Oleg Zhurakousky 8 years ago committed by Juergen Hoeller
parent
commit
299b9d60fd
  1. 12
      spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java
  2. 9
      spring-core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java

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

@ -97,6 +97,7 @@ import org.springframework.util.StringUtils; @@ -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 { @@ -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 extends Annotation> A findAnnotation(
AnnotatedElement annotatedElement, Class<A> annotationType, Set<Annotation> visited) {
try {
@ -673,7 +673,6 @@ public abstract class AnnotationUtils { @@ -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 extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType, Set<Annotation> visited) {
try {
A annotation = clazz.getDeclaredAnnotation(annotationType);
@ -1294,8 +1293,11 @@ public abstract class AnnotationUtils { @@ -1294,8 +1293,11 @@ public abstract class AnnotationUtils {
* Retrieve the <em>value</em> 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 { @@ -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;
}

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

@ -54,6 +54,7 @@ import static org.springframework.core.annotation.AnnotationUtils.*; @@ -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 {
@ -1240,6 +1241,14 @@ public class AnnotationUtilsTests { @@ -1240,6 +1241,14 @@ public class AnnotationUtilsTests {
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 {
Map<String, Object> map = Collections.singletonMap("location", "test.xml");

Loading…
Cancel
Save