24
24
import java .util .Set ;
25
25
26
26
import org .springframework .core .annotation .AnnotatedElementUtils ;
27
+ import org .springframework .core .annotation .AnnotationFilter ;
27
28
import org .springframework .core .annotation .AnnotationUtils ;
29
+ import org .springframework .core .annotation .MergedAnnotations ;
30
+ import org .springframework .core .annotation .MergedAnnotations .SearchStrategy ;
31
+ import org .springframework .core .annotation .RepeatableContainers ;
28
32
import org .springframework .lang .Nullable ;
29
33
import org .springframework .util .MultiValueMap ;
30
34
import org .springframework .util .ReflectionUtils ;
42
46
*/
43
47
public class StandardAnnotationMetadata extends StandardClassMetadata implements AnnotationMetadata {
44
48
45
- private final Annotation [] annotations ;
49
+ private final MergedAnnotations mergedAnnotations ;
46
50
47
51
private final boolean nestedAnnotationsAsMap ;
48
52
53
+ private Set <String > annotationTypes ;
49
54
50
55
/**
51
56
* Create a new {@code StandardAnnotationMetadata} wrapper for the given Class.
@@ -69,72 +74,49 @@ public StandardAnnotationMetadata(Class<?> introspectedClass) {
69
74
*/
70
75
public StandardAnnotationMetadata (Class <?> introspectedClass , boolean nestedAnnotationsAsMap ) {
71
76
super (introspectedClass );
72
- this .annotations = introspectedClass .getDeclaredAnnotations ();
77
+ this .mergedAnnotations = MergedAnnotations .from (introspectedClass ,
78
+ SearchStrategy .DIRECT , RepeatableContainers .none (),
79
+ AnnotationFilter .NONE );
73
80
this .nestedAnnotationsAsMap = nestedAnnotationsAsMap ;
74
81
}
75
82
76
83
77
84
@ Override
78
- public Set <String > getAnnotationTypes () {
79
- Set <String > types = new LinkedHashSet <>();
80
- for (Annotation ann : this .annotations ) {
81
- if (!AnnotationUtils .isInJavaLangAnnotationPackage (ann .annotationType ().getName ())) {
82
- types .add (ann .annotationType ().getName ());
83
- }
84
- }
85
- return types ;
86
- }
87
-
88
- @ Override
89
- public Set <String > getMetaAnnotationTypes (String annotationName ) {
90
- if (AnnotationUtils .isInJavaLangAnnotationPackage (annotationName )) {
91
- return Collections .emptySet ();
92
- }
93
- return (this .annotations .length > 0 ?
94
- AnnotatedElementUtils .getMetaAnnotationTypes (getIntrospectedClass (), annotationName ) :
95
- Collections .emptySet ());
85
+ public MergedAnnotations getAnnotations () {
86
+ return this .mergedAnnotations ;
96
87
}
97
88
98
89
@ Override
99
- public boolean hasAnnotation (String annotationName ) {
100
- if (AnnotationUtils .isInJavaLangAnnotationPackage (annotationName )) {
101
- return false ;
102
- }
103
- for (Annotation ann : this .annotations ) {
104
- if (ann .annotationType ().getName ().equals (annotationName )) {
105
- return true ;
106
- }
107
- }
108
- return false ;
109
- }
110
-
111
- @ Override
112
- public boolean hasMetaAnnotation (String annotationName ) {
113
- if (AnnotationUtils .isInJavaLangAnnotationPackage (annotationName )) {
114
- return false ;
90
+ public Set <String > getAnnotationTypes () {
91
+ Set <String > annotationTypes = this .annotationTypes ;
92
+ if (annotationTypes == null ) {
93
+ annotationTypes = Collections .unmodifiableSet (
94
+ AnnotationMetadata .super .getAnnotationTypes ());
95
+ this .annotationTypes = annotationTypes ;
115
96
}
116
- return (this .annotations .length > 0 &&
117
- AnnotatedElementUtils .hasMetaAnnotationTypes (getIntrospectedClass (), annotationName ));
118
- }
119
-
120
- @ Override
121
- public boolean isAnnotated (String annotationName ) {
122
- return (this .annotations .length > 0 &&
123
- AnnotatedElementUtils .isAnnotated (getIntrospectedClass (), annotationName ));
97
+ return annotationTypes ;
124
98
}
125
99
126
100
@ Override
127
101
@ Nullable
128
102
public Map <String , Object > getAnnotationAttributes (String annotationName , boolean classValuesAsString ) {
129
- return (this .annotations .length > 0 ? AnnotatedElementUtils .getMergedAnnotationAttributes (
130
- getIntrospectedClass (), annotationName , classValuesAsString , this .nestedAnnotationsAsMap ) : null );
103
+ if (this .nestedAnnotationsAsMap ) {
104
+ return AnnotationMetadata .super .getAnnotationAttributes (annotationName ,
105
+ classValuesAsString );
106
+ }
107
+ return AnnotatedElementUtils .getMergedAnnotationAttributes (
108
+ getIntrospectedClass (), annotationName , classValuesAsString , this .nestedAnnotationsAsMap );
131
109
}
132
110
133
111
@ Override
134
112
@ Nullable
135
113
public MultiValueMap <String , Object > getAllAnnotationAttributes (String annotationName , boolean classValuesAsString ) {
136
- return (this .annotations .length > 0 ? AnnotatedElementUtils .getAllAnnotationAttributes (
137
- getIntrospectedClass (), annotationName , classValuesAsString , this .nestedAnnotationsAsMap ) : null );
114
+ if (this .nestedAnnotationsAsMap ) {
115
+ return AnnotationMetadata .super .getAllAnnotationAttributes (annotationName ,
116
+ classValuesAsString );
117
+ }
118
+ return AnnotatedElementUtils .getAllAnnotationAttributes (
119
+ getIntrospectedClass (), annotationName , classValuesAsString , this .nestedAnnotationsAsMap );
138
120
}
139
121
140
122
@ Override
@@ -143,8 +125,7 @@ public boolean hasAnnotatedMethods(String annotationName) {
143
125
try {
144
126
Method [] methods = ReflectionUtils .getDeclaredMethods (getIntrospectedClass ());
145
127
for (Method method : methods ) {
146
- if (!method .isBridge () && method .getAnnotations ().length > 0 &&
147
- AnnotatedElementUtils .isAnnotated (method , annotationName )) {
128
+ if (isAnnotatedMethod (method , annotationName )) {
148
129
return true ;
149
130
}
150
131
}
@@ -158,13 +139,15 @@ public boolean hasAnnotatedMethods(String annotationName) {
158
139
159
140
@ Override
160
141
public Set <MethodMetadata > getAnnotatedMethods (String annotationName ) {
161
- Set <MethodMetadata > annotatedMethods = new LinkedHashSet <>( 4 ) ;
142
+ Set <MethodMetadata > annotatedMethods = null ;
162
143
if (AnnotationUtils .isCandidateClass (getIntrospectedClass (), annotationName )) {
163
144
try {
164
- Method [] methods = getIntrospectedClass () .getDeclaredMethods ();
145
+ Method [] methods = ReflectionUtils .getDeclaredMethods (getIntrospectedClass () );
165
146
for (Method method : methods ) {
166
- if (!method .isBridge () && method .getAnnotations ().length > 0 &&
167
- AnnotatedElementUtils .isAnnotated (method , annotationName )) {
147
+ if (isAnnotatedMethod (method , annotationName )) {
148
+ if (annotatedMethods == null ) {
149
+ annotatedMethods = new LinkedHashSet <>(4 );
150
+ }
168
151
annotatedMethods .add (new StandardMethodMetadata (method , this .nestedAnnotationsAsMap ));
169
152
}
170
153
}
@@ -173,7 +156,12 @@ public Set<MethodMetadata> getAnnotatedMethods(String annotationName) {
173
156
throw new IllegalStateException ("Failed to introspect annotated methods on " + getIntrospectedClass (), ex );
174
157
}
175
158
}
176
- return annotatedMethods ;
159
+ return annotatedMethods != null ? annotatedMethods : Collections .emptySet ();
160
+ }
161
+
162
+ private boolean isAnnotatedMethod (Method method , String annotationName ) {
163
+ return !method .isBridge () && method .getAnnotations ().length > 0 &&
164
+ AnnotatedElementUtils .isAnnotated (method , annotationName );
177
165
}
178
166
179
167
}
0 commit comments