/
SimpleAnnotationMetadataReadingVisitor.java
195 lines (155 loc) · 5.34 KB
/
SimpleAnnotationMetadataReadingVisitor.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
/*
* Copyright 2002-2023 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.LinkedHashSet;
import java.util.Set;
import org.springframework.asm.AnnotationVisitor;
import org.springframework.asm.ClassVisitor;
import org.springframework.asm.MethodVisitor;
import org.springframework.asm.Opcodes;
import org.springframework.asm.SpringAsmInfo;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.type.MethodMetadata;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* ASM class visitor that creates {@link SimpleAnnotationMetadata}.
*
* @author Phillip Webb
* @author Juergen Hoeller
* @since 5.2
*/
final class SimpleAnnotationMetadataReadingVisitor extends ClassVisitor {
@Nullable
private final ClassLoader classLoader;
private String className = "";
private int access;
@Nullable
private String superClassName;
@Nullable
private String enclosingClassName;
private boolean independentInnerClass;
private final Set<String> interfaceNames = new LinkedHashSet<>(4);
private final Set<String> memberClassNames = new LinkedHashSet<>(4);
private final Set<MergedAnnotation<?>> annotations = new LinkedHashSet<>(4);
private final Set<MethodMetadata> declaredMethods = new LinkedHashSet<>(4);
@Nullable
private SimpleAnnotationMetadata metadata;
@Nullable
private Source source;
SimpleAnnotationMetadataReadingVisitor(@Nullable ClassLoader classLoader) {
super(SpringAsmInfo.ASM_VERSION);
this.classLoader = classLoader;
}
@Override
public void visit(int version, int access, String name, String signature,
@Nullable String supername, String[] interfaces) {
this.className = toClassName(name);
this.access = access;
if (supername != null && !isInterface(access)) {
this.superClassName = toClassName(supername);
}
for (String element : interfaces) {
this.interfaceNames.add(toClassName(element));
}
}
@Override
public void visitOuterClass(String owner, String name, String desc) {
this.enclosingClassName = toClassName(owner);
}
@Override
public void visitInnerClass(String name, @Nullable String outerName, String innerName, int access) {
if (outerName != null) {
String className = toClassName(name);
String outerClassName = toClassName(outerName);
if (this.className.equals(className)) {
this.enclosingClassName = outerClassName;
this.independentInnerClass = ((access & Opcodes.ACC_STATIC) != 0);
}
else if (this.className.equals(outerClassName)) {
this.memberClassNames.add(className);
}
}
}
@Override
@Nullable
public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
return MergedAnnotationReadingVisitor.get(this.classLoader, getSource(),
descriptor, visible, this.annotations::add);
}
@Override
@Nullable
public MethodVisitor visitMethod(
int access, String name, String descriptor, String signature, String[] exceptions) {
// Skip bridge methods and constructors - we're only interested in original user methods.
if (isBridge(access) || name.equals("<init>")) {
return null;
}
return new SimpleMethodMetadataReadingVisitor(this.classLoader, this.className,
access, name, descriptor, this.declaredMethods::add);
}
@Override
public void visitEnd() {
MergedAnnotations annotations = MergedAnnotations.of(this.annotations);
this.metadata = new SimpleAnnotationMetadata(this.className, this.access,
this.enclosingClassName, this.superClassName, this.independentInnerClass,
this.interfaceNames, this.memberClassNames, this.declaredMethods, annotations);
}
public SimpleAnnotationMetadata getMetadata() {
Assert.state(this.metadata != null, "AnnotationMetadata not initialized");
return this.metadata;
}
private Source getSource() {
Source source = this.source;
if (source == null) {
source = new Source(this.className);
this.source = source;
}
return source;
}
private String toClassName(String name) {
return ClassUtils.convertResourcePathToClassName(name);
}
private boolean isBridge(int access) {
return (access & Opcodes.ACC_BRIDGE) != 0;
}
private boolean isInterface(int access) {
return (access & Opcodes.ACC_INTERFACE) != 0;
}
/**
* {@link MergedAnnotation} source.
*/
private static final class Source {
private final String className;
Source(String className) {
this.className = className;
}
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof Source that && this.className.equals(that.className)));
}
@Override
public int hashCode() {
return this.className.hashCode();
}
@Override
public String toString() {
return this.className;
}
}
}