16
16
package org .springframework .data .repository .config ;
17
17
18
18
import java .lang .annotation .Annotation ;
19
- import java .util .ArrayList ;
20
19
import java .util .Arrays ;
21
20
import java .util .HashSet ;
22
- import java .util .List ;
23
21
import java .util .Map ;
24
22
import java .util .Optional ;
25
23
import java .util .Set ;
26
- import java .util .regex .Pattern ;
27
24
import java .util .stream .Stream ;
28
25
29
- import org .springframework .beans .BeanUtils ;
30
26
import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
31
27
import org .springframework .beans .factory .support .BeanNameGenerator ;
32
28
import org .springframework .context .annotation .AnnotationBeanNameGenerator ;
33
29
import org .springframework .context .annotation .ConfigurationClassPostProcessor ;
34
- import org .springframework .context .annotation .FilterType ;
30
+ import org .springframework .context .annotation .TypeFilterUtils ;
35
31
import org .springframework .core .annotation .AnnotationAttributes ;
36
32
import org .springframework .core .env .Environment ;
37
33
import org .springframework .core .io .ResourceLoader ;
38
34
import org .springframework .core .type .AnnotationMetadata ;
39
- import org .springframework .core .type .filter .AnnotationTypeFilter ;
40
- import org .springframework .core .type .filter .AspectJTypeFilter ;
41
- import org .springframework .core .type .filter .AssignableTypeFilter ;
42
- import org .springframework .core .type .filter .RegexPatternTypeFilter ;
43
35
import org .springframework .core .type .filter .TypeFilter ;
44
36
import org .springframework .data .config .ConfigurationUtils ;
45
37
import org .springframework .data .util .Streamable ;
58
50
* @author Jens Schauder
59
51
* @author Mark Paluch
60
52
* @author Johannes Englmeier
53
+ * @author Florian Cramer
61
54
*/
62
55
public class AnnotationRepositoryConfigurationSource extends RepositoryConfigurationSourceSupport {
63
56
@@ -75,6 +68,8 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura
75
68
private final AnnotationMetadata enableAnnotationMetadata ;
76
69
private final AnnotationAttributes attributes ;
77
70
private final ResourceLoader resourceLoader ;
71
+ private final Environment environment ;
72
+ private final BeanDefinitionRegistry registry ;
78
73
private final boolean hasExplicitFilters ;
79
74
80
75
/**
@@ -126,6 +121,8 @@ public AnnotationRepositoryConfigurationSource(AnnotationMetadata metadata, Clas
126
121
this .enableAnnotationMetadata = AnnotationMetadata .introspect (annotation );
127
122
this .configMetadata = metadata ;
128
123
this .resourceLoader = resourceLoader ;
124
+ this .environment = environment ;
125
+ this .registry = registry ;
129
126
this .hasExplicitFilters = hasExplicitFilters (attributes );
130
127
}
131
128
@@ -337,7 +334,9 @@ private Streamable<TypeFilter> parseFilters(String attributeName) {
337
334
338
335
AnnotationAttributes [] filters = attributes .getAnnotationArray (attributeName );
339
336
340
- return Streamable .of (() -> Arrays .stream (filters ).flatMap (it -> typeFiltersFor (it ).stream ()));
337
+ return Streamable .of (() -> Arrays .stream (filters ) //
338
+ .flatMap (it -> TypeFilterUtils .createTypeFiltersFor (it , this .environment , this .resourceLoader , this .registry )
339
+ .stream ()));
341
340
}
342
341
343
342
/**
@@ -354,71 +353,6 @@ private Optional<String> getNullDefaultedAttribute(String attributeName) {
354
353
return StringUtils .hasText (attribute ) ? Optional .of (attribute ) : Optional .empty ();
355
354
}
356
355
357
- /**
358
- * Copy of {@code ComponentScanAnnotationParser#typeFiltersFor}.
359
- *
360
- * @param filterAttributes
361
- * @return
362
- */
363
- private List <TypeFilter > typeFiltersFor (AnnotationAttributes filterAttributes ) {
364
-
365
- List <TypeFilter > typeFilters = new ArrayList <>();
366
- FilterType filterType = filterAttributes .getEnum ("type" );
367
-
368
- for (Class <?> filterClass : filterAttributes .getClassArray ("value" )) {
369
- switch (filterType ) {
370
- case ANNOTATION :
371
- Assert .isAssignable (Annotation .class , filterClass ,
372
- "An error occured when processing a @ComponentScan " + "ANNOTATION type filter: " );
373
- @ SuppressWarnings ("unchecked" )
374
- Class <Annotation > annoClass = (Class <Annotation >) filterClass ;
375
- typeFilters .add (new AnnotationTypeFilter (annoClass ));
376
- break ;
377
- case ASSIGNABLE_TYPE :
378
- typeFilters .add (new AssignableTypeFilter (filterClass ));
379
- break ;
380
- case CUSTOM :
381
- Assert .isAssignable (TypeFilter .class , filterClass ,
382
- "An error occured when processing a @ComponentScan " + "CUSTOM type filter: " );
383
- typeFilters .add (BeanUtils .instantiateClass (filterClass , TypeFilter .class ));
384
- break ;
385
- default :
386
- throw new IllegalArgumentException ("Unknown filter type " + filterType );
387
- }
388
- }
389
-
390
- for (String expression : getPatterns (filterAttributes )) {
391
-
392
- String rawName = filterType .toString ();
393
-
394
- if ("REGEX" .equals (rawName )) {
395
- typeFilters .add (new RegexPatternTypeFilter (Pattern .compile (expression )));
396
- } else if ("ASPECTJ" .equals (rawName )) {
397
- typeFilters .add (new AspectJTypeFilter (expression , this .resourceLoader .getClassLoader ()));
398
- } else {
399
- throw new IllegalArgumentException ("Unknown filter type " + filterType );
400
- }
401
- }
402
-
403
- return typeFilters ;
404
- }
405
-
406
- /**
407
- * Safely reads the {@code pattern} attribute from the given {@link AnnotationAttributes} and returns an empty list if
408
- * the attribute is not present.
409
- *
410
- * @param filterAttributes must not be {@literal null}.
411
- * @return
412
- */
413
- private String [] getPatterns (AnnotationAttributes filterAttributes ) {
414
-
415
- try {
416
- return filterAttributes .getStringArray ("pattern" );
417
- } catch (IllegalArgumentException o_O ) {
418
- return new String [0 ];
419
- }
420
- }
421
-
422
356
/**
423
357
* Returns whether there's explicit configuration of include- or exclude filters.
424
358
*
0 commit comments