/
AnnotationMetadataReadingVisitor.java
199 lines (173 loc) · 6.75 KB
/
AnnotationMetadataReadingVisitor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* Copyright 2002-2019 the original author or authors.
*
* 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
*
* https://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.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.asm.AnnotationVisitor;
import org.springframework.asm.MethodVisitor;
import org.springframework.asm.Opcodes;
import org.springframework.asm.Type;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.MethodMetadata;
import org.springframework.lang.Nullable;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
/**
* ASM class visitor which looks for the class name and implemented types as
* well as for the annotations defined on the class, exposing them through
* the {@link org.springframework.core.type.AnnotationMetadata} interface.
*
* @author Juergen Hoeller
* @author Mark Fisher
* @author Costin Leau
* @author Phillip Webb
* @author Sam Brannen
* @since 2.5
* @deprecated As of Spring Framework 5.2, this class has been replaced by
* {@link SimpleAnnotationMetadataReadingVisitor} for internal use within the
* framework, but there is no public replacement for
* {@code AnnotationMetadataReadingVisitor}.
*/
@Deprecated
public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisitor implements AnnotationMetadata {
@Nullable
protected final ClassLoader classLoader;
protected final Set<String> annotationSet = new LinkedHashSet<>(4);
protected final Map<String, Set<String>> metaAnnotationMap = new LinkedHashMap<>(4);
/**
* Declared as a {@link LinkedMultiValueMap} instead of a {@link MultiValueMap}
* to ensure that the hierarchical ordering of the entries is preserved.
* @see AnnotationReadingVisitorUtils#getMergedAnnotationAttributes
*/
protected final LinkedMultiValueMap<String, AnnotationAttributes> attributesMap = new LinkedMultiValueMap<>(4);
protected final Set<MethodMetadata> methodMetadataSet = new LinkedHashSet<>(4);
public AnnotationMetadataReadingVisitor(@Nullable ClassLoader classLoader) {
this.classLoader = classLoader;
}
@Override
public MergedAnnotations getAnnotations() {
throw new UnsupportedOperationException();
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
// Skip bridge methods - we're only interested in original annotation-defining user methods.
// On JDK 8, we'd otherwise run into double detection of the same annotated method...
if ((access & Opcodes.ACC_BRIDGE) != 0) {
return super.visitMethod(access, name, desc, signature, exceptions);
}
return new MethodMetadataReadingVisitor(name, access, getClassName(),
Type.getReturnType(desc).getClassName(), this.classLoader, this.methodMetadataSet);
}
@Override
@Nullable
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
if (!visible) {
return null;
}
String className = Type.getType(desc).getClassName();
if (AnnotationUtils.isInJavaLangAnnotationPackage(className)) {
return null;
}
this.annotationSet.add(className);
return new AnnotationAttributesReadingVisitor(
className, this.attributesMap, this.metaAnnotationMap, this.classLoader);
}
@Override
public Set<String> getAnnotationTypes() {
return this.annotationSet;
}
@Override
public Set<String> getMetaAnnotationTypes(String annotationName) {
Set<String> metaAnnotationTypes = this.metaAnnotationMap.get(annotationName);
return (metaAnnotationTypes != null ? metaAnnotationTypes : Collections.emptySet());
}
@Override
public boolean hasMetaAnnotation(String metaAnnotationType) {
if (AnnotationUtils.isInJavaLangAnnotationPackage(metaAnnotationType)) {
return false;
}
Collection<Set<String>> allMetaTypes = this.metaAnnotationMap.values();
for (Set<String> metaTypes : allMetaTypes) {
if (metaTypes.contains(metaAnnotationType)) {
return true;
}
}
return false;
}
@Override
public boolean isAnnotated(String annotationName) {
return (!AnnotationUtils.isInJavaLangAnnotationPackage(annotationName) &&
this.attributesMap.containsKey(annotationName));
}
@Override
public boolean hasAnnotation(String annotationName) {
return getAnnotationTypes().contains(annotationName);
}
@Override
@Nullable
public AnnotationAttributes getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
AnnotationAttributes raw = AnnotationReadingVisitorUtils.getMergedAnnotationAttributes(
this.attributesMap, this.metaAnnotationMap, annotationName);
if (raw == null) {
return null;
}
return AnnotationReadingVisitorUtils.convertClassValues(
"class '" + getClassName() + "'", this.classLoader, raw, classValuesAsString);
}
@Override
@Nullable
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) {
MultiValueMap<String, Object> allAttributes = new LinkedMultiValueMap<>();
List<AnnotationAttributes> attributes = this.attributesMap.get(annotationName);
if (attributes == null) {
return null;
}
for (AnnotationAttributes raw : attributes) {
for (Map.Entry<String, Object> entry : AnnotationReadingVisitorUtils.convertClassValues(
"class '" + getClassName() + "'", this.classLoader, raw, classValuesAsString).entrySet()) {
allAttributes.add(entry.getKey(), entry.getValue());
}
}
return allAttributes;
}
@Override
public boolean hasAnnotatedMethods(String annotationName) {
for (MethodMetadata methodMetadata : this.methodMetadataSet) {
if (methodMetadata.isAnnotated(annotationName)) {
return true;
}
}
return false;
}
@Override
public Set<MethodMetadata> getAnnotatedMethods(String annotationName) {
Set<MethodMetadata> annotatedMethods = new LinkedHashSet<>(4);
for (MethodMetadata methodMetadata : this.methodMetadataSet) {
if (methodMetadata.isAnnotated(annotationName)) {
annotatedMethods.add(methodMetadata);
}
}
return annotatedMethods;
}
}