23
23
import java .lang .reflect .Modifier ;
24
24
import java .util .Arrays ;
25
25
import java .util .Map ;
26
- import java .util .function .BiPredicate ;
27
26
28
27
import org .springframework .core .BridgeMethodResolver ;
29
28
import org .springframework .core .Ordered ;
@@ -75,70 +74,49 @@ private AnnotationsScanner() {
75
74
static <C , R > R scan (C context , AnnotatedElement source , SearchStrategy searchStrategy ,
76
75
AnnotationsProcessor <C , R > processor ) {
77
76
78
- return scan (context , source , searchStrategy , processor , null );
79
- }
80
-
81
- /**
82
- * Scan the hierarchy of the specified element for relevant annotations and
83
- * call the processor as required.
84
- * @param context an optional context object that will be passed back to the
85
- * processor
86
- * @param source the source element to scan
87
- * @param searchStrategy the search strategy to use
88
- * @param processor the processor that receives the annotations
89
- * @param classFilter an optional filter that can be used to entirely filter
90
- * out a specific class from the hierarchy
91
- * @return the result of {@link AnnotationsProcessor#finish(Object)}
92
- */
93
- @ Nullable
94
- static <C , R > R scan (C context , AnnotatedElement source , SearchStrategy searchStrategy ,
95
- AnnotationsProcessor <C , R > processor , @ Nullable BiPredicate <C , Class <?>> classFilter ) {
96
-
97
- R result = process (context , source , searchStrategy , processor , classFilter );
77
+ R result = process (context , source , searchStrategy , processor );
98
78
return processor .finish (result );
99
79
}
100
80
101
81
@ Nullable
102
82
private static <C , R > R process (C context , AnnotatedElement source ,
103
- SearchStrategy searchStrategy , AnnotationsProcessor <C , R > processor ,
104
- @ Nullable BiPredicate <C , Class <?>> classFilter ) {
83
+ SearchStrategy searchStrategy , AnnotationsProcessor <C , R > processor ) {
105
84
106
85
if (source instanceof Class ) {
107
- return processClass (context , (Class <?>) source , searchStrategy , processor , classFilter );
86
+ return processClass (context , (Class <?>) source , searchStrategy , processor );
108
87
}
109
88
if (source instanceof Method ) {
110
- return processMethod (context , (Method ) source , searchStrategy , processor , classFilter );
89
+ return processMethod (context , (Method ) source , searchStrategy , processor );
111
90
}
112
- return processElement (context , source , processor , classFilter );
91
+ return processElement (context , source , processor );
113
92
}
114
93
115
94
@ Nullable
116
95
private static <C , R > R processClass (C context , Class <?> source ,
117
- SearchStrategy searchStrategy , AnnotationsProcessor <C , R > processor ,
118
- @ Nullable BiPredicate <C , Class <?>> classFilter ) {
96
+ SearchStrategy searchStrategy , AnnotationsProcessor <C , R > processor ) {
119
97
120
98
switch (searchStrategy ) {
121
99
case DIRECT :
122
- return processElement (context , source , processor , classFilter );
100
+ return processElement (context , source , processor );
123
101
case INHERITED_ANNOTATIONS :
124
- return processClassInheritedAnnotations (context , source , searchStrategy , processor , classFilter );
102
+ return processClassInheritedAnnotations (context , source , searchStrategy , processor );
125
103
case SUPERCLASS :
126
- return processClassHierarchy (context , source , processor , classFilter , false , false );
104
+ return processClassHierarchy (context , source , processor , false , false );
127
105
case TYPE_HIERARCHY :
128
- return processClassHierarchy (context , source , processor , classFilter , true , false );
106
+ return processClassHierarchy (context , source , processor , true , false );
129
107
case TYPE_HIERARCHY_AND_ENCLOSING_CLASSES :
130
- return processClassHierarchy (context , source , processor , classFilter , true , true );
108
+ return processClassHierarchy (context , source , processor , true , true );
131
109
}
132
110
throw new IllegalStateException ("Unsupported search strategy " + searchStrategy );
133
111
}
134
112
135
113
@ Nullable
136
114
private static <C , R > R processClassInheritedAnnotations (C context , Class <?> source ,
137
- SearchStrategy searchStrategy , AnnotationsProcessor <C , R > processor , @ Nullable BiPredicate < C , Class <?>> classFilter ) {
115
+ SearchStrategy searchStrategy , AnnotationsProcessor <C , R > processor ) {
138
116
139
117
try {
140
118
if (isWithoutHierarchy (source , searchStrategy )) {
141
- return processElement (context , source , processor , classFilter );
119
+ return processElement (context , source , processor );
142
120
}
143
121
Annotation [] relevant = null ;
144
122
int remaining = Integer .MAX_VALUE ;
@@ -150,14 +128,7 @@ private static <C, R> R processClassInheritedAnnotations(C context, Class<?> sou
150
128
if (result != null ) {
151
129
return result ;
152
130
}
153
- if (isFiltered (source , context , classFilter )) {
154
- // Skip the current level in the class hierarchy.
155
- source = source .getSuperclass ();
156
- aggregateIndex ++;
157
- continue ;
158
- }
159
- Annotation [] declaredAnnotations =
160
- getDeclaredAnnotations (context , source , classFilter , true );
131
+ Annotation [] declaredAnnotations = getDeclaredAnnotations (source , true );
161
132
if (relevant == null && declaredAnnotations .length > 0 ) {
162
133
relevant = root .getAnnotations ();
163
134
remaining = relevant .length ;
@@ -195,17 +166,15 @@ private static <C, R> R processClassInheritedAnnotations(C context, Class<?> sou
195
166
196
167
@ Nullable
197
168
private static <C , R > R processClassHierarchy (C context , Class <?> source ,
198
- AnnotationsProcessor <C , R > processor , @ Nullable BiPredicate <C , Class <?>> classFilter ,
199
- boolean includeInterfaces , boolean includeEnclosing ) {
169
+ AnnotationsProcessor <C , R > processor , boolean includeInterfaces , boolean includeEnclosing ) {
200
170
201
171
return processClassHierarchy (context , new int [] {0 }, source , processor ,
202
- classFilter , includeInterfaces , includeEnclosing );
172
+ includeInterfaces , includeEnclosing );
203
173
}
204
174
205
175
@ Nullable
206
176
private static <C , R > R processClassHierarchy (C context , int [] aggregateIndex , Class <?> source ,
207
- AnnotationsProcessor <C , R > processor , @ Nullable BiPredicate <C , Class <?>> classFilter ,
208
- boolean includeInterfaces , boolean includeEnclosing ) {
177
+ AnnotationsProcessor <C , R > processor , boolean includeInterfaces , boolean includeEnclosing ) {
209
178
210
179
try {
211
180
R result = processor .doWithAggregate (context , aggregateIndex [0 ]);
@@ -215,7 +184,7 @@ private static <C, R> R processClassHierarchy(C context, int[] aggregateIndex, C
215
184
if (hasPlainJavaAnnotationsOnly (source )) {
216
185
return null ;
217
186
}
218
- Annotation [] annotations = getDeclaredAnnotations (context , source , classFilter , false );
187
+ Annotation [] annotations = getDeclaredAnnotations (source , false );
219
188
result = processor .doWithAnnotations (context , aggregateIndex [0 ], source , annotations );
220
189
if (result != null ) {
221
190
return result ;
@@ -224,7 +193,7 @@ private static <C, R> R processClassHierarchy(C context, int[] aggregateIndex, C
224
193
if (includeInterfaces ) {
225
194
for (Class <?> interfaceType : source .getInterfaces ()) {
226
195
R interfacesResult = processClassHierarchy (context , aggregateIndex ,
227
- interfaceType , processor , classFilter , true , includeEnclosing );
196
+ interfaceType , processor , true , includeEnclosing );
228
197
if (interfacesResult != null ) {
229
198
return interfacesResult ;
230
199
}
@@ -233,7 +202,7 @@ private static <C, R> R processClassHierarchy(C context, int[] aggregateIndex, C
233
202
Class <?> superclass = source .getSuperclass ();
234
203
if (superclass != Object .class && superclass != null ) {
235
204
R superclassResult = processClassHierarchy (context , aggregateIndex ,
236
- superclass , processor , classFilter , includeInterfaces , includeEnclosing );
205
+ superclass , processor , includeInterfaces , includeEnclosing );
237
206
if (superclassResult != null ) {
238
207
return superclassResult ;
239
208
}
@@ -248,7 +217,7 @@ private static <C, R> R processClassHierarchy(C context, int[] aggregateIndex, C
248
217
Class <?> enclosingClass = source .getEnclosingClass ();
249
218
if (enclosingClass != null ) {
250
219
R enclosingResult = processClassHierarchy (context , aggregateIndex ,
251
- enclosingClass , processor , classFilter , includeInterfaces , true );
220
+ enclosingClass , processor , includeInterfaces , true );
252
221
if (enclosingResult != null ) {
253
222
return enclosingResult ;
254
223
}
@@ -267,32 +236,31 @@ private static <C, R> R processClassHierarchy(C context, int[] aggregateIndex, C
267
236
268
237
@ Nullable
269
238
private static <C , R > R processMethod (C context , Method source ,
270
- SearchStrategy searchStrategy , AnnotationsProcessor <C , R > processor ,
271
- @ Nullable BiPredicate <C , Class <?>> classFilter ) {
239
+ SearchStrategy searchStrategy , AnnotationsProcessor <C , R > processor ) {
272
240
273
241
switch (searchStrategy ) {
274
242
case DIRECT :
275
243
case INHERITED_ANNOTATIONS :
276
- return processMethodInheritedAnnotations (context , source , processor , classFilter );
244
+ return processMethodInheritedAnnotations (context , source , processor );
277
245
case SUPERCLASS :
278
246
return processMethodHierarchy (context , new int [] {0 }, source .getDeclaringClass (),
279
- processor , classFilter , source , false );
247
+ processor , source , false );
280
248
case TYPE_HIERARCHY :
281
249
case TYPE_HIERARCHY_AND_ENCLOSING_CLASSES :
282
250
return processMethodHierarchy (context , new int [] {0 }, source .getDeclaringClass (),
283
- processor , classFilter , source , true );
251
+ processor , source , true );
284
252
}
285
253
throw new IllegalStateException ("Unsupported search strategy " + searchStrategy );
286
254
}
287
255
288
256
@ Nullable
289
257
private static <C , R > R processMethodInheritedAnnotations (C context , Method source ,
290
- AnnotationsProcessor <C , R > processor , @ Nullable BiPredicate < C , Class <?>> classFilter ) {
258
+ AnnotationsProcessor <C , R > processor ) {
291
259
292
260
try {
293
261
R result = processor .doWithAggregate (context , 0 );
294
262
return (result != null ? result :
295
- processMethodAnnotations (context , 0 , source , processor , classFilter ));
263
+ processMethodAnnotations (context , 0 , source , processor ));
296
264
}
297
265
catch (Throwable ex ) {
298
266
AnnotationUtils .handleIntrospectionFailure (source , ex );
@@ -302,8 +270,7 @@ private static <C, R> R processMethodInheritedAnnotations(C context, Method sour
302
270
303
271
@ Nullable
304
272
private static <C , R > R processMethodHierarchy (C context , int [] aggregateIndex ,
305
- Class <?> sourceClass , AnnotationsProcessor <C , R > processor ,
306
- @ Nullable BiPredicate <C , Class <?>> classFilter , Method rootMethod ,
273
+ Class <?> sourceClass , AnnotationsProcessor <C , R > processor , Method rootMethod ,
307
274
boolean includeInterfaces ) {
308
275
309
276
try {
@@ -317,17 +284,17 @@ private static <C, R> R processMethodHierarchy(C context, int[] aggregateIndex,
317
284
boolean calledProcessor = false ;
318
285
if (sourceClass == rootMethod .getDeclaringClass ()) {
319
286
result = processMethodAnnotations (context , aggregateIndex [0 ],
320
- rootMethod , processor , classFilter );
287
+ rootMethod , processor );
321
288
calledProcessor = true ;
322
289
if (result != null ) {
323
290
return result ;
324
291
}
325
292
}
326
293
else {
327
- for (Method candidateMethod : getBaseTypeMethods (context , sourceClass , classFilter )) {
294
+ for (Method candidateMethod : getBaseTypeMethods (context , sourceClass )) {
328
295
if (candidateMethod != null && isOverride (rootMethod , candidateMethod )) {
329
296
result = processMethodAnnotations (context , aggregateIndex [0 ],
330
- candidateMethod , processor , classFilter );
297
+ candidateMethod , processor );
331
298
calledProcessor = true ;
332
299
if (result != null ) {
333
300
return result ;
@@ -344,7 +311,7 @@ private static <C, R> R processMethodHierarchy(C context, int[] aggregateIndex,
344
311
if (includeInterfaces ) {
345
312
for (Class <?> interfaceType : sourceClass .getInterfaces ()) {
346
313
R interfacesResult = processMethodHierarchy (context , aggregateIndex ,
347
- interfaceType , processor , classFilter , rootMethod , true );
314
+ interfaceType , processor , rootMethod , true );
348
315
if (interfacesResult != null ) {
349
316
return interfacesResult ;
350
317
}
@@ -353,7 +320,7 @@ private static <C, R> R processMethodHierarchy(C context, int[] aggregateIndex,
353
320
Class <?> superclass = sourceClass .getSuperclass ();
354
321
if (superclass != Object .class && superclass != null ) {
355
322
R superclassResult = processMethodHierarchy (context , aggregateIndex ,
356
- superclass , processor , classFilter , rootMethod , includeInterfaces );
323
+ superclass , processor , rootMethod , includeInterfaces );
357
324
if (superclassResult != null ) {
358
325
return superclassResult ;
359
326
}
@@ -365,11 +332,8 @@ private static <C, R> R processMethodHierarchy(C context, int[] aggregateIndex,
365
332
return null ;
366
333
}
367
334
368
- private static <C > Method [] getBaseTypeMethods (
369
- C context , Class <?> baseType , @ Nullable BiPredicate <C , Class <?>> classFilter ) {
370
-
371
- if (baseType == Object .class || hasPlainJavaAnnotationsOnly (baseType ) ||
372
- isFiltered (baseType , context , classFilter )) {
335
+ private static <C > Method [] getBaseTypeMethods (C context , Class <?> baseType ) {
336
+ if (baseType == Object .class || hasPlainJavaAnnotationsOnly (baseType )) {
373
337
return NO_METHODS ;
374
338
}
375
339
@@ -433,16 +397,16 @@ private static boolean hasSameGenericTypeParameters(
433
397
434
398
@ Nullable
435
399
private static <C , R > R processMethodAnnotations (C context , int aggregateIndex , Method source ,
436
- AnnotationsProcessor <C , R > processor , @ Nullable BiPredicate < C , Class <?>> classFilter ) {
400
+ AnnotationsProcessor <C , R > processor ) {
437
401
438
- Annotation [] annotations = getDeclaredAnnotations (context , source , classFilter , false );
402
+ Annotation [] annotations = getDeclaredAnnotations (source , false );
439
403
R result = processor .doWithAnnotations (context , aggregateIndex , source , annotations );
440
404
if (result != null ) {
441
405
return result ;
442
406
}
443
407
Method bridgedMethod = BridgeMethodResolver .findBridgedMethod (source );
444
408
if (bridgedMethod != source ) {
445
- Annotation [] bridgedAnnotations = getDeclaredAnnotations (context , bridgedMethod , classFilter , true );
409
+ Annotation [] bridgedAnnotations = getDeclaredAnnotations (bridgedMethod , true );
446
410
for (int i = 0 ; i < bridgedAnnotations .length ; i ++) {
447
411
if (ObjectUtils .containsElement (annotations , bridgedAnnotations [i ])) {
448
412
bridgedAnnotations [i ] = null ;
@@ -455,31 +419,19 @@ private static <C, R> R processMethodAnnotations(C context, int aggregateIndex,
455
419
456
420
@ Nullable
457
421
private static <C , R > R processElement (C context , AnnotatedElement source ,
458
- AnnotationsProcessor <C , R > processor , @ Nullable BiPredicate < C , Class <?>> classFilter ) {
422
+ AnnotationsProcessor <C , R > processor ) {
459
423
460
424
try {
461
425
R result = processor .doWithAggregate (context , 0 );
462
426
return (result != null ? result : processor .doWithAnnotations (
463
- context , 0 , source , getDeclaredAnnotations (context , source , classFilter , false )));
427
+ context , 0 , source , getDeclaredAnnotations (source , false )));
464
428
}
465
429
catch (Throwable ex ) {
466
430
AnnotationUtils .handleIntrospectionFailure (source , ex );
467
431
}
468
432
return null ;
469
433
}
470
434
471
- private static <C , R > Annotation [] getDeclaredAnnotations (C context ,
472
- AnnotatedElement source , @ Nullable BiPredicate <C , Class <?>> classFilter , boolean copy ) {
473
-
474
- if (source instanceof Class && isFiltered ((Class <?>) source , context , classFilter )) {
475
- return NO_ANNOTATIONS ;
476
- }
477
- if (source instanceof Method && isFiltered (((Method ) source ).getDeclaringClass (), context , classFilter )) {
478
- return NO_ANNOTATIONS ;
479
- }
480
- return getDeclaredAnnotations (source , copy );
481
- }
482
-
483
435
@ SuppressWarnings ("unchecked" )
484
436
@ Nullable
485
437
static <A extends Annotation > A getDeclaredAnnotation (AnnotatedElement source , Class <A > annotationType ) {
@@ -525,12 +477,6 @@ static Annotation[] getDeclaredAnnotations(AnnotatedElement source, boolean defe
525
477
return annotations .clone ();
526
478
}
527
479
528
- private static <C > boolean isFiltered (
529
- Class <?> sourceClass , C context , @ Nullable BiPredicate <C , Class <?>> classFilter ) {
530
-
531
- return (classFilter != null && classFilter .test (context , sourceClass ));
532
- }
533
-
534
480
private static boolean isIgnorable (Class <?> annotationType ) {
535
481
return AnnotationFilter .PLAIN .matches (annotationType );
536
482
}
0 commit comments