Support non-public anno. attr. values in AnnoUtils

Prior to this commit, the getValue(Annotation, String) method in
AnnotationUtils failed to retrieve the value of the desired annotation
attribute if the annotation itself was not public -- for example if the
annotation was declared as package private.

This commit addresses this issue by ensuring that getValue(Annotation,
String) uses reflection to make the desired annotation attribute method
accessible before attempting to invoke it to retrieve the value.

Issue: SPR-11104
This commit is contained in:
Sam Brannen
2013-11-20 21:58:36 +01:00
parent 45afd4fbe2
commit b830d7362d
4 changed files with 101 additions and 15 deletions

View File

@@ -31,6 +31,7 @@ import java.util.WeakHashMap;
import org.springframework.core.BridgeMethodResolver;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
/**
* General utility methods for working with annotations, handling bridge methods (which the compiler
@@ -525,6 +526,7 @@ public abstract class AnnotationUtils {
public static Object getValue(Annotation annotation, String attributeName) {
try {
Method method = annotation.annotationType().getDeclaredMethod(attributeName, new Class[0]);
ReflectionUtils.makeAccessible(method);
return method.invoke(annotation);
}
catch (Exception ex) {
@@ -585,7 +587,6 @@ public abstract class AnnotationUtils {
private static class AnnotationCollector<A extends Annotation> {
private final Class<? extends Annotation> containerAnnotationType;
private final Class<A> annotationType;
@@ -628,6 +629,7 @@ public abstract class AnnotationUtils {
private A[] getValue(Annotation annotation) {
try {
Method method = annotation.annotationType().getDeclaredMethod("value");
ReflectionUtils.makeAccessible(method);
return (A[]) method.invoke(annotation);
}
catch (Exception ex) {