Document getAttributeAliasMap() in AnnotationUtils

This commit also introduces a cache for attribute alias metadata.

Issue: SPR-11512
This commit is contained in:
Sam Brannen
2015-05-23 20:27:50 +02:00
parent 92bf32b9be
commit 62d1b4b6e8

View File

@@ -118,6 +118,9 @@ public abstract class AnnotationUtils {
private static final Map<Class<? extends Annotation>, Boolean> synthesizableCache = private static final Map<Class<? extends Annotation>, Boolean> synthesizableCache =
new ConcurrentReferenceHashMap<Class<? extends Annotation>, Boolean>(256); new ConcurrentReferenceHashMap<Class<? extends Annotation>, Boolean>(256);
private static final Map<Class<? extends Annotation>, Map<String, String>> attributeAliasCache =
new ConcurrentReferenceHashMap<Class<? extends Annotation>, Map<String, String>>(256);
private static transient Log logger; private static transient Log logger;
@@ -1058,20 +1061,38 @@ public abstract class AnnotationUtils {
return annotation; return annotation;
} }
InvocationHandler handler = new SynthesizedAnnotationInvocationHandler(annotatedElement, annotation, getAliasMap(annotationType)); InvocationHandler handler = new SynthesizedAnnotationInvocationHandler(annotatedElement, annotation,
getAttributeAliasMap(annotationType));
A synthesizedAnnotation = (A) Proxy.newProxyInstance(ClassUtils.getDefaultClassLoader(), new Class<?>[] { A synthesizedAnnotation = (A) Proxy.newProxyInstance(ClassUtils.getDefaultClassLoader(), new Class<?>[] {
(Class<A>) annotationType, SynthesizedAnnotation.class }, handler); (Class<A>) annotationType, SynthesizedAnnotation.class }, handler);
return synthesizedAnnotation; return synthesizedAnnotation;
} }
/** /**
* TODO Document getAliasMap(). * Get a map of all attribute alias pairs, declared via {@code @AliasFor}
* in the supplied annotation type.
*
* <p>The map is keyed by attribute name with each value representing
* the name of the aliased attribute. For each entry {@code [x, y]} in
* the map there will be a corresponding {@code [y, x]} entry in the map.
*
* <p>An empty return value implies that the annotation does not declare
* any attribute aliases.
*
* @param annotationType the annotation type to find attribute aliases in
* @return a map containing attribute alias pairs; never {@code null}
* @since 4.2 * @since 4.2
*/ */
private static Map<String, String> getAliasMap(Class<? extends Annotation> annotationType) { private static Map<String, String> getAttributeAliasMap(Class<? extends Annotation> annotationType) {
if (annotationType == null) { if (annotationType == null) {
return null; return Collections.emptyMap();
}
Map<String, String> cachedMap = attributeAliasCache.get(annotationType);
if (cachedMap != null) {
return cachedMap;
} }
Map<String, String> map = new HashMap<String, String>(); Map<String, String> map = new HashMap<String, String>();
@@ -1082,6 +1103,9 @@ public abstract class AnnotationUtils {
map.put(attributeName, aliasedAttributeName); map.put(attributeName, aliasedAttributeName);
} }
} }
attributeAliasCache.put(annotationType, map);
return map; return map;
} }
@@ -1283,9 +1307,9 @@ public abstract class AnnotationUtils {
/** /**
* TODO Document postProcessAnnotationAttributes(). * TODO Document postProcessAnnotationAttributes().
* *
* @param annotatedElement the element that is annotated with the supplied * @param element the element that is annotated with the supplied annotation,
* annotation, used for contextual logging; may be {@code null} if unknown * used for contextual logging; may be {@code null} if unknown
* @param attributes the annotation attributes to validate * @param attributes the annotation attributes to post-process
* @since 4.2 * @since 4.2
*/ */
static void postProcessAnnotationAttributes(AnnotatedElement element, AnnotationAttributes attributes, static void postProcessAnnotationAttributes(AnnotatedElement element, AnnotationAttributes attributes,
@@ -1297,12 +1321,10 @@ public abstract class AnnotationUtils {
} }
Class<? extends Annotation> annotationType = attributes.annotationType(); Class<? extends Annotation> annotationType = attributes.annotationType();
Map<String, String> aliasMap = getAliasMap(annotationType);
// Validate @AliasFor configuration // Validate @AliasFor configuration
if (aliasMap != null) { Map<String, String> aliasMap = getAttributeAliasMap(annotationType);
Set<String> validated = new HashSet<String>(); Set<String> validated = new HashSet<String>();
for (String attributeName : aliasMap.keySet()) { for (String attributeName : aliasMap.keySet()) {
String aliasedAttributeName = aliasMap.get(attributeName); String aliasedAttributeName = aliasMap.get(attributeName);
@@ -1332,7 +1354,6 @@ public abstract class AnnotationUtils {
} }
} }
} }
}
for (String attributeName : attributes.keySet()) { for (String attributeName : attributes.keySet()) {
Object value = attributes.get(attributeName); Object value = attributes.get(attributeName);