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")'
This commit is contained in:
parent
2579dab209
commit
299b9d60fd
|
|
@ -97,6 +97,7 @@ import org.springframework.util.StringUtils;
|
||||||
* @author Mark Fisher
|
* @author Mark Fisher
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Oleg Zhurakousky
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
* @see AliasFor
|
* @see AliasFor
|
||||||
* @see AnnotationAttributes
|
* @see AnnotationAttributes
|
||||||
|
|
@ -484,7 +485,6 @@ public abstract class AnnotationUtils {
|
||||||
* @return the first matching annotation, or {@code null} if not found
|
* @return the first matching annotation, or {@code null} if not found
|
||||||
* @since 4.2
|
* @since 4.2
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
private static <A extends Annotation> A findAnnotation(
|
private static <A extends Annotation> A findAnnotation(
|
||||||
AnnotatedElement annotatedElement, Class<A> annotationType, Set<Annotation> visited) {
|
AnnotatedElement annotatedElement, Class<A> annotationType, Set<Annotation> visited) {
|
||||||
try {
|
try {
|
||||||
|
|
@ -673,7 +673,6 @@ public abstract class AnnotationUtils {
|
||||||
* @param visited the set of annotations that have already been visited
|
* @param visited the set of annotations that have already been visited
|
||||||
* @return the first matching annotation, or {@code null} if not found
|
* @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) {
|
private static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType, Set<Annotation> visited) {
|
||||||
try {
|
try {
|
||||||
A annotation = clazz.getDeclaredAnnotation(annotationType);
|
A annotation = clazz.getDeclaredAnnotation(annotationType);
|
||||||
|
|
@ -1294,8 +1293,11 @@ public abstract class AnnotationUtils {
|
||||||
* Retrieve the <em>value</em> of a named attribute, given an annotation instance.
|
* 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 annotation the annotation instance from which to retrieve the value
|
||||||
* @param attributeName the name of the attribute value to retrieve
|
* @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 #getValue(Annotation)
|
||||||
|
* @see #rethrowAnnotationConfigurationException(Throwable)
|
||||||
*/
|
*/
|
||||||
public static Object getValue(Annotation annotation, String attributeName) {
|
public static Object getValue(Annotation annotation, String attributeName) {
|
||||||
if (annotation == null || !StringUtils.hasText(attributeName)) {
|
if (annotation == null || !StringUtils.hasText(attributeName)) {
|
||||||
|
|
@ -1306,6 +1308,10 @@ public abstract class AnnotationUtils {
|
||||||
ReflectionUtils.makeAccessible(method);
|
ReflectionUtils.makeAccessible(method);
|
||||||
return method.invoke(annotation);
|
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) {
|
catch (Exception ex) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,7 @@ import static org.springframework.core.annotation.AnnotationUtils.*;
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Oleg Zhurakousky
|
||||||
*/
|
*/
|
||||||
public class AnnotationUtilsTests {
|
public class AnnotationUtilsTests {
|
||||||
|
|
||||||
|
|
@ -1239,6 +1240,14 @@ public class AnnotationUtilsTests {
|
||||||
assertEquals("value: ", "", contextConfig.value());
|
assertEquals("value: ", "", contextConfig.value());
|
||||||
assertEquals("location: ", "", contextConfig.location());
|
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
|
@Test
|
||||||
public void synthesizeAnnotationFromMapWithMinimalAttributesWithAttributeAliases() throws Exception {
|
public void synthesizeAnnotationFromMapWithMinimalAttributesWithAttributeAliases() throws Exception {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue