AnnotationAttributesReadingVisitor.java 3.7 KB
Newer Older
C
Chris Beams 已提交
1
/*
2
 * Copyright 2002-2014 the original author or authors.
C
Chris Beams 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.core.type.classreading;

import java.lang.annotation.Annotation;
20
import java.lang.reflect.Modifier;
C
Chris Beams 已提交
21
import java.util.LinkedHashSet;
22
import java.util.List;
C
Chris Beams 已提交
23 24 25
import java.util.Map;
import java.util.Set;

26
import org.springframework.core.annotation.AnnotationAttributes;
C
Chris Beams 已提交
27
import org.springframework.core.annotation.AnnotationUtils;
28
import org.springframework.util.MultiValueMap;
29 30 31 32 33 34 35 36 37 38 39

/**
 * ASM visitor which looks for the annotations defined on a class or method, including
 * tracking meta-annotations.
 *
 * <p>As of Spring 3.1.1, this visitor is fully recursive, taking into account any nested
 * annotations or nested annotation arrays. These annotations are in turn read into
 * {@link AnnotationAttributes} map structures.
 *
 * @author Juergen Hoeller
 * @author Chris Beams
40
 * @author Phillip Webb
41
 * @author Sam Brannen
42 43 44 45 46 47
 * @since 3.0
 */
final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttributesVisitor {

	private final String annotationType;

48
	private final MultiValueMap<String, AnnotationAttributes> attributesMap;
49 50 51

	private final Map<String, Set<String>> metaAnnotationMap;

52

53 54 55
	public AnnotationAttributesReadingVisitor(String annotationType,
			MultiValueMap<String, AnnotationAttributes> attributesMap, Map<String, Set<String>> metaAnnotationMap,
			ClassLoader classLoader) {
56 57 58 59 60 61 62

		super(annotationType, new AnnotationAttributes(), classLoader);
		this.annotationType = annotationType;
		this.attributesMap = attributesMap;
		this.metaAnnotationMap = metaAnnotationMap;
	}

63

64 65 66
	@Override
	public void doVisitEnd(Class<?> annotationClass) {
		super.doVisitEnd(annotationClass);
67 68 69 70 71 72 73
		List<AnnotationAttributes> attributes = this.attributesMap.get(this.annotationType);
		if (attributes == null) {
			this.attributesMap.add(this.annotationType, this.attributes);
		}
		else {
			attributes.add(0, this.attributes);
		}
74
		Set<String> metaAnnotationTypeNames = new LinkedHashSet<String>();
75
		for (Annotation metaAnnotation : AnnotationUtils.getAnnotations(annotationClass)) {
76 77 78
			if (!AnnotationUtils.isInJavaLangAnnotationPackage(metaAnnotation)) {
				recursivelyCollectMetaAnnotations(metaAnnotationTypeNames, metaAnnotation);
			}
C
Chris Beams 已提交
79
		}
80 81
		if (this.metaAnnotationMap != null) {
			this.metaAnnotationMap.put(annotationClass.getName(), metaAnnotationTypeNames);
C
Chris Beams 已提交
82 83
		}
	}
84

85
	private void recursivelyCollectMetaAnnotations(Set<String> visited, Annotation annotation) {
86 87 88 89 90
		String annotationName = annotation.annotationType().getName();
		if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotation) && visited.add(annotationName)) {
			// 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.
91
			if (Modifier.isPublic(annotation.annotationType().getModifiers())) {
92
				this.attributesMap.add(annotationName, AnnotationUtils.getAnnotationAttributes(annotation, false, true));
93 94 95
				for (Annotation metaMetaAnnotation : annotation.annotationType().getAnnotations()) {
					recursivelyCollectMetaAnnotations(visited, metaMetaAnnotation);
				}
96 97 98
			}
		}
	}
99

P
Phillip Webb 已提交
100
}