Polish ASM-based annotation processing

- AnnotationAttributesReadingVisitor no longer processes annotations
  from the java.lang.annotation package.

- Simplified logic in AnnotationReadingVisitorUtils
  getMergedAnnotationAttributes().

Issue: SPR-11574
This commit is contained in:
Sam Brannen 2014-03-21 15:26:13 +01:00
parent 90e3dbb0f5
commit 796af90ba7
3 changed files with 61 additions and 54 deletions

View File

@ -130,8 +130,8 @@ final class RecursiveAnnotationArrayVisitor extends AbstractRecursiveAnnotationV
} }
else { else {
Class<?> arrayClass = newValue.getClass(); Class<?> arrayClass = newValue.getClass();
if(Enum.class.isAssignableFrom(arrayClass)) { if (Enum.class.isAssignableFrom(arrayClass)) {
while(arrayClass.getSuperclass() != null && !arrayClass.isEnum()) { while (arrayClass.getSuperclass() != null && !arrayClass.isEnum()) {
arrayClass = arrayClass.getSuperclass(); arrayClass = arrayClass.getSuperclass();
} }
} }
@ -153,8 +153,8 @@ final class RecursiveAnnotationArrayVisitor extends AbstractRecursiveAnnotationV
@Override @Override
public void visitEnd() { public void visitEnd() {
if (!this.allNestedAttributes.isEmpty()) { if (!this.allNestedAttributes.isEmpty()) {
this.attributes.put(this.attributeName, this.allNestedAttributes.toArray( this.attributes.put(this.attributeName,
new AnnotationAttributes[this.allNestedAttributes.size()])); this.allNestedAttributes.toArray(new AnnotationAttributes[this.allNestedAttributes.size()]));
} }
} }
} }
@ -169,8 +169,9 @@ class RecursiveAnnotationAttributesVisitor extends AbstractRecursiveAnnotationVi
private final String annotationType; private final String annotationType;
public RecursiveAnnotationAttributesVisitor(
String annotationType, AnnotationAttributes attributes, ClassLoader classLoader) { public RecursiveAnnotationAttributesVisitor(String annotationType, AnnotationAttributes attributes,
ClassLoader classLoader) {
super(classLoader, attributes); super(classLoader, attributes);
this.annotationType = annotationType; this.annotationType = annotationType;
} }
@ -182,8 +183,8 @@ class RecursiveAnnotationAttributesVisitor extends AbstractRecursiveAnnotationVi
doVisitEnd(annotationClass); doVisitEnd(annotationClass);
} }
catch (ClassNotFoundException ex) { catch (ClassNotFoundException ex) {
this.logger.debug("Failed to class-load type while reading annotation metadata. " + this.logger.debug("Failed to class-load type while reading annotation metadata. "
"This is a non-fatal error, but certain annotation metadata may be unavailable.", ex); + "This is a non-fatal error, but certain annotation metadata may be unavailable.", ex);
} }
} }
@ -192,8 +193,9 @@ class RecursiveAnnotationAttributesVisitor extends AbstractRecursiveAnnotationVi
} }
private void registerDefaultValues(Class<?> annotationClass) { private void registerDefaultValues(Class<?> annotationClass) {
// Only do further scanning for public annotations; we'd run into IllegalAccessExceptions // Only do further scanning for public annotations; we'd run into
// otherwise, and don't want to mess with accessibility in a SecurityManager environment. // IllegalAccessExceptions otherwise, and we don't want to mess with
// accessibility in a SecurityManager environment.
if (Modifier.isPublic(annotationClass.getModifiers())) { if (Modifier.isPublic(annotationClass.getModifiers())) {
// Check declared default values of attributes in the annotation type. // Check declared default values of attributes in the annotation type.
Method[] annotationAttributes = annotationClass.getMethods(); Method[] annotationAttributes = annotationClass.getMethods();
@ -202,15 +204,15 @@ class RecursiveAnnotationAttributesVisitor extends AbstractRecursiveAnnotationVi
Object defaultValue = annotationAttribute.getDefaultValue(); Object defaultValue = annotationAttribute.getDefaultValue();
if (defaultValue != null && !this.attributes.containsKey(attributeName)) { if (defaultValue != null && !this.attributes.containsKey(attributeName)) {
if (defaultValue instanceof Annotation) { if (defaultValue instanceof Annotation) {
defaultValue = AnnotationAttributes.fromMap( defaultValue = AnnotationAttributes.fromMap(AnnotationUtils.getAnnotationAttributes(
AnnotationUtils.getAnnotationAttributes((Annotation) defaultValue, false, true)); (Annotation) defaultValue, false, true));
} }
else if (defaultValue instanceof Annotation[]) { else if (defaultValue instanceof Annotation[]) {
Annotation[] realAnnotations = (Annotation[]) defaultValue; Annotation[] realAnnotations = (Annotation[]) defaultValue;
AnnotationAttributes[] mappedAnnotations = new AnnotationAttributes[realAnnotations.length]; AnnotationAttributes[] mappedAnnotations = new AnnotationAttributes[realAnnotations.length];
for (int i = 0; i < realAnnotations.length; i++) { for (int i = 0; i < realAnnotations.length; i++) {
mappedAnnotations[i] = AnnotationAttributes.fromMap( mappedAnnotations[i] = AnnotationAttributes.fromMap(AnnotationUtils.getAnnotationAttributes(
AnnotationUtils.getAnnotationAttributes(realAnnotations[i], false, true)); realAnnotations[i], false, true));
} }
defaultValue = mappedAnnotations; defaultValue = mappedAnnotations;
} }
@ -233,6 +235,7 @@ class RecursiveAnnotationAttributesVisitor extends AbstractRecursiveAnnotationVi
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams * @author Chris Beams
* @author Phillip Webb * @author Phillip Webb
* @author Sam Brannen
* @since 3.0 * @since 3.0
*/ */
final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttributesVisitor { final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttributesVisitor {
@ -244,9 +247,9 @@ final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttrib
private final Map<String, Set<String>> metaAnnotationMap; private final Map<String, Set<String>> metaAnnotationMap;
public AnnotationAttributesReadingVisitor( public AnnotationAttributesReadingVisitor(String annotationType,
String annotationType, MultiValueMap<String, AnnotationAttributes> attributesMap, MultiValueMap<String, AnnotationAttributes> attributesMap, Map<String, Set<String>> metaAnnotationMap,
Map<String, Set<String>> metaAnnotationMap, ClassLoader classLoader) { ClassLoader classLoader) {
super(annotationType, new AnnotationAttributes(), classLoader); super(annotationType, new AnnotationAttributes(), classLoader);
this.annotationType = annotationType; this.annotationType = annotationType;
@ -267,24 +270,28 @@ final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttrib
} }
Set<String> metaAnnotationTypeNames = new LinkedHashSet<String>(); Set<String> metaAnnotationTypeNames = new LinkedHashSet<String>();
for (Annotation metaAnnotation : annotationClass.getAnnotations()) { for (Annotation metaAnnotation : annotationClass.getAnnotations()) {
if (!AnnotationUtils.isInJavaLangAnnotationPackage(metaAnnotation)) {
recursivelyCollectMetaAnnotations(metaAnnotationTypeNames, metaAnnotation); recursivelyCollectMetaAnnotations(metaAnnotationTypeNames, metaAnnotation);
} }
}
if (this.metaAnnotationMap != null) { if (this.metaAnnotationMap != null) {
this.metaAnnotationMap.put(annotationClass.getName(), metaAnnotationTypeNames); this.metaAnnotationMap.put(annotationClass.getName(), metaAnnotationTypeNames);
} }
} }
private void recursivelyCollectMetaAnnotations(Set<String> visited, Annotation annotation) { private void recursivelyCollectMetaAnnotations(Set<String> visited, Annotation annotation) {
if (visited.add(annotation.annotationType().getName())) { String annotationName = annotation.annotationType().getName();
// Only do further scanning for public annotations; we'd run into IllegalAccessExceptions if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotation) && visited.add(annotationName)) {
// otherwise, and don't want to mess with accessibility in a SecurityManager environment. // Only do further scanning for public annotations; we'd run into
// IllegalAccessExceptions otherwise, and we don't want to mess with
// accessibility in a SecurityManager environment.
if (Modifier.isPublic(annotation.annotationType().getModifiers())) { if (Modifier.isPublic(annotation.annotationType().getModifiers())) {
this.attributesMap.add(annotation.annotationType().getName(), this.attributesMap.add(annotationName, AnnotationUtils.getAnnotationAttributes(annotation, false, true));
AnnotationUtils.getAnnotationAttributes(annotation, false, true));
for (Annotation metaMetaAnnotation : annotation.annotationType().getAnnotations()) { for (Annotation metaMetaAnnotation : annotation.annotationType().getAnnotations()) {
recursivelyCollectMetaAnnotations(visited, metaMetaAnnotation); recursivelyCollectMetaAnnotations(visited, metaMetaAnnotation);
} }
} }
} }
} }
} }

View File

@ -53,11 +53,11 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito
protected final Map<String, Set<String>> metaAnnotationMap = new LinkedHashMap<String, Set<String>>(4); protected final Map<String, Set<String>> metaAnnotationMap = new LinkedHashMap<String, Set<String>>(4);
/** /**
* Declared as a {@link LinkedMultiValueMap} instead of {@link MultiValueMap} * Declared as a {@link LinkedMultiValueMap} instead of a {@link MultiValueMap}
* in order to ensure that ordering of entries is enforced. * to ensure that the hierarchical ordering of the entries is preserved.
* @see AnnotationReadingVisitorUtils#getMergedAnnotationAttributes(LinkedMultiValueMap, String) * @see AnnotationReadingVisitorUtils#getMergedAnnotationAttributes(LinkedMultiValueMap, String)
*/ */
protected final LinkedMultiValueMap<String, AnnotationAttributes> attributeMap = new LinkedMultiValueMap<String, AnnotationAttributes>( protected final LinkedMultiValueMap<String, AnnotationAttributes> attributesMap = new LinkedMultiValueMap<String, AnnotationAttributes>(
4); 4);
protected final Set<MethodMetadata> methodMetadataSet = new LinkedHashSet<MethodMetadata>(4); protected final Set<MethodMetadata> methodMetadataSet = new LinkedHashSet<MethodMetadata>(4);
@ -77,7 +77,7 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito
public AnnotationVisitor visitAnnotation(final String desc, boolean visible) { public AnnotationVisitor visitAnnotation(final String desc, boolean visible) {
String className = Type.getType(desc).getClassName(); String className = Type.getType(desc).getClassName();
this.annotationSet.add(className); this.annotationSet.add(className);
return new AnnotationAttributesReadingVisitor(className, this.attributeMap, this.metaAnnotationMap, this.classLoader); return new AnnotationAttributesReadingVisitor(className, this.attributesMap, this.metaAnnotationMap, this.classLoader);
} }
@ -109,7 +109,7 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito
@Override @Override
public boolean isAnnotated(String annotationType) { public boolean isAnnotated(String annotationType) {
return this.attributeMap.containsKey(annotationType); return this.attributesMap.containsKey(annotationType);
} }
@Override @Override
@ -119,7 +119,7 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito
@Override @Override
public AnnotationAttributes getAnnotationAttributes(String annotationType, boolean classValuesAsString) { public AnnotationAttributes getAnnotationAttributes(String annotationType, boolean classValuesAsString) {
AnnotationAttributes raw = AnnotationReadingVisitorUtils.getMergedAnnotationAttributes(this.attributeMap, AnnotationAttributes raw = AnnotationReadingVisitorUtils.getMergedAnnotationAttributes(this.attributesMap,
annotationType); annotationType);
return AnnotationReadingVisitorUtils.convertClassValues(this.classLoader, raw, classValuesAsString); return AnnotationReadingVisitorUtils.convertClassValues(this.classLoader, raw, classValuesAsString);
} }
@ -132,7 +132,7 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito
@Override @Override
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationType, boolean classValuesAsString) { public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationType, boolean classValuesAsString) {
MultiValueMap<String, Object> allAttributes = new LinkedMultiValueMap<String, Object>(); MultiValueMap<String, Object> allAttributes = new LinkedMultiValueMap<String, Object>();
List<AnnotationAttributes> attributes = this.attributeMap.get(annotationType); List<AnnotationAttributes> attributes = this.attributesMap.get(annotationType);
if (attributes == null) { if (attributes == null) {
return null; return null;
} }

View File

@ -18,6 +18,7 @@ package org.springframework.core.type.classreading;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -26,7 +27,7 @@ import org.springframework.asm.Type;
import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import static org.springframework.core.annotation.AnnotationUtils.*; import static org.springframework.core.annotation.AnnotationUtils.VALUE;
/** /**
* Internal utility class used when reading annotations. * Internal utility class used when reading annotations.
@ -98,22 +99,22 @@ abstract class AnnotationReadingVisitorUtils {
/** /**
* Retrieve the merged attributes of the annotation of the given type, if any, * Retrieve the merged attributes of the annotation of the given type, if any,
* from the supplied {@code attributeMap}. * from the supplied {@code attributesMap}.
* <p>Annotation attribute values appearing <em>lower</em> in the annotation * <p>Annotation attribute values appearing <em>lower</em> in the annotation
* hierarchy (i.e., closer to the declaring class) will override those * hierarchy (i.e., closer to the declaring class) will override those
* defined <em>higher</em> in the annotation hierarchy. * defined <em>higher</em> in the annotation hierarchy.
* @param attributeMap the map of annotation attribute lists, keyed by * @param attributesMap the map of annotation attribute lists, keyed by
* annotation type * annotation type name
* @param annotationType the annotation type to look for * @param annotationType the name of the annotation type to look for
* @return the merged annotation attributes; or {@code null} if no matching * @return the merged annotation attributes; or {@code null} if no matching
* annotation is present in the {@code attributeMap} * annotation is present in the {@code attributesMap}
* @since 4.0.3 * @since 4.0.3
*/ */
public static AnnotationAttributes getMergedAnnotationAttributes( public static AnnotationAttributes getMergedAnnotationAttributes(
LinkedMultiValueMap<String, AnnotationAttributes> attributeMap, String annotationType) { LinkedMultiValueMap<String, AnnotationAttributes> attributesMap, String annotationType) {
// Get the unmerged list of attributes for the target annotation. // Get the unmerged list of attributes for the target annotation.
List<AnnotationAttributes> attributesList = attributeMap.get(annotationType); List<AnnotationAttributes> attributesList = attributesMap.get(annotationType);
if (attributesList == null || attributesList.isEmpty()) { if (attributesList == null || attributesList.isEmpty()) {
return null; return null;
} }
@ -121,28 +122,27 @@ abstract class AnnotationReadingVisitorUtils {
// To start with, we populate the results with all attribute values from the // To start with, we populate the results with all attribute values from the
// target annotation. // target annotation.
AnnotationAttributes results = attributesList.get(0); AnnotationAttributes results = attributesList.get(0);
Set<String> supportedAttributeNames = results.keySet();
Set<String> overridableAttributeNames = new HashSet<String>(results.keySet());
overridableAttributeNames.remove(VALUE);
// Since the map is a LinkedMultiValueMap, we depend on the ordering of // Since the map is a LinkedMultiValueMap, we depend on the ordering of
// elements in the map and reverse the order of the keys in order to traverse // elements in the map and reverse the order of the keys in order to traverse
// "down" the meta-annotation hierarchy. // "down" the annotation hierarchy.
List<String> annotationTypes = new ArrayList<String>(attributeMap.keySet()); List<String> annotationTypes = new ArrayList<String>(attributesMap.keySet());
Collections.reverse(annotationTypes); Collections.reverse(annotationTypes);
for (String currentAnnotationType : annotationTypes) { for (String currentAnnotationType : annotationTypes) {
if (!currentAnnotationType.startsWith("java.lang.annotation")) { List<AnnotationAttributes> currentAttributesList = attributesMap.get(currentAnnotationType);
for (String attributeName : supportedAttributeNames) { if (currentAttributesList != null && !currentAttributesList.isEmpty()) {
if (!VALUE.equals(attributeName)) { AnnotationAttributes currentAttributes = currentAttributesList.get(0);
List<AnnotationAttributes> currentAttributes = attributeMap.get(currentAnnotationType); for (String overridableAttributeName : overridableAttributeNames) {
if (currentAttributes != null && !currentAttributes.isEmpty()) { Object value = currentAttributes.get(overridableAttributeName);
Object value = currentAttributes.get(0).get(attributeName);
if (value != null) { if (value != null) {
// Overwrite value from target annotation with the value // Store the value, potentially overriding a value from an
// from an attribute of the same name found lower in the // attribute of the same name found higher in the annotation
// meta-annotation hierarchy. // hierarchy.
results.put(attributeName, value); results.put(overridableAttributeName, value);
}
}
} }
} }
} }