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
@@ -277,7 +274,9 @@ private Streamable<TypeFilter> parseFilters(String attributeName) {
277
274
278
275
AnnotationAttributes [] filters = attributes .getAnnotationArray (attributeName );
279
276
280
- return Streamable .of (() -> Arrays .stream (filters ).flatMap (it -> typeFiltersFor (it ).stream ()));
277
+ return Streamable .of (() -> Arrays .stream (filters ) //
278
+ .flatMap (it -> TypeFilterUtils .createTypeFiltersFor (it , this .environment , this .resourceLoader , this .registry )
279
+ .stream ()));
281
280
}
282
281
283
282
/**
@@ -294,71 +293,6 @@ private Optional<String> getNullDefaultedAttribute(String attributeName) {
294
293
return StringUtils .hasText (attribute ) ? Optional .of (attribute ) : Optional .empty ();
295
294
}
296
295
297
- /**
298
- * Copy of {@code ComponentScanAnnotationParser#typeFiltersFor}.
299
- *
300
- * @param filterAttributes
301
- * @return
302
- */
303
- private List <TypeFilter > typeFiltersFor (AnnotationAttributes filterAttributes ) {
304
-
305
- List <TypeFilter > typeFilters = new ArrayList <>();
306
- FilterType filterType = filterAttributes .getEnum ("type" );
307
-
308
- for (Class <?> filterClass : filterAttributes .getClassArray ("value" )) {
309
- switch (filterType ) {
310
- case ANNOTATION :
311
- Assert .isAssignable (Annotation .class , filterClass ,
312
- "An error occured when processing a @ComponentScan " + "ANNOTATION type filter: " );
313
- @ SuppressWarnings ("unchecked" )
314
- Class <Annotation > annoClass = (Class <Annotation >) filterClass ;
315
- typeFilters .add (new AnnotationTypeFilter (annoClass ));
316
- break ;
317
- case ASSIGNABLE_TYPE :
318
- typeFilters .add (new AssignableTypeFilter (filterClass ));
319
- break ;
320
- case CUSTOM :
321
- Assert .isAssignable (TypeFilter .class , filterClass ,
322
- "An error occured when processing a @ComponentScan " + "CUSTOM type filter: " );
323
- typeFilters .add (BeanUtils .instantiateClass (filterClass , TypeFilter .class ));
324
- break ;
325
- default :
326
- throw new IllegalArgumentException ("Unknown filter type " + filterType );
327
- }
328
- }
329
-
330
- for (String expression : getPatterns (filterAttributes )) {
331
-
332
- String rawName = filterType .toString ();
333
-
334
- if ("REGEX" .equals (rawName )) {
335
- typeFilters .add (new RegexPatternTypeFilter (Pattern .compile (expression )));
336
- } else if ("ASPECTJ" .equals (rawName )) {
337
- typeFilters .add (new AspectJTypeFilter (expression , this .resourceLoader .getClassLoader ()));
338
- } else {
339
- throw new IllegalArgumentException ("Unknown filter type " + filterType );
340
- }
341
- }
342
-
343
- return typeFilters ;
344
- }
345
-
346
- /**
347
- * Safely reads the {@code pattern} attribute from the given {@link AnnotationAttributes} and returns an empty list if
348
- * the attribute is not present.
349
- *
350
- * @param filterAttributes must not be {@literal null}.
351
- * @return
352
- */
353
- private String [] getPatterns (AnnotationAttributes filterAttributes ) {
354
-
355
- try {
356
- return filterAttributes .getStringArray ("pattern" );
357
- } catch (IllegalArgumentException o_O ) {
358
- return new String [0 ];
359
- }
360
- }
361
-
362
296
/**
363
297
* Returns whether there's explicit configuration of include- or exclude filters.
364
298
*
0 commit comments