1616package org .springframework .data .repository .config ;
1717
1818import java .lang .annotation .Annotation ;
19- import java .util .ArrayList ;
2019import java .util .Arrays ;
2120import java .util .HashSet ;
22- import java .util .List ;
2321import java .util .Map ;
2422import java .util .Optional ;
2523import java .util .Set ;
26- import java .util .regex .Pattern ;
2724import java .util .stream .Stream ;
2825
29- import org .springframework .beans .BeanUtils ;
3026import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
3127import org .springframework .beans .factory .support .BeanNameGenerator ;
3228import org .springframework .context .annotation .AnnotationBeanNameGenerator ;
3329import org .springframework .context .annotation .ConfigurationClassPostProcessor ;
34- import org .springframework .context .annotation .FilterType ;
30+ import org .springframework .context .annotation .TypeFilterUtils ;
3531import org .springframework .core .annotation .AnnotationAttributes ;
3632import org .springframework .core .env .Environment ;
3733import org .springframework .core .io .ResourceLoader ;
3834import 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 ;
4335import org .springframework .core .type .filter .TypeFilter ;
4436import org .springframework .data .config .ConfigurationUtils ;
4537import org .springframework .data .util .Streamable ;
5850 * @author Jens Schauder
5951 * @author Mark Paluch
6052 * @author Johannes Englmeier
53+ * @author Florian Cramer
6154 */
6255public class AnnotationRepositoryConfigurationSource extends RepositoryConfigurationSourceSupport {
6356
@@ -75,6 +68,8 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura
7568 private final AnnotationMetadata enableAnnotationMetadata ;
7669 private final AnnotationAttributes attributes ;
7770 private final ResourceLoader resourceLoader ;
71+ private final Environment environment ;
72+ private final BeanDefinitionRegistry registry ;
7873 private final boolean hasExplicitFilters ;
7974
8075 /**
@@ -126,6 +121,8 @@ public AnnotationRepositoryConfigurationSource(AnnotationMetadata metadata, Clas
126121 this .enableAnnotationMetadata = AnnotationMetadata .introspect (annotation );
127122 this .configMetadata = metadata ;
128123 this .resourceLoader = resourceLoader ;
124+ this .environment = environment ;
125+ this .registry = registry ;
129126 this .hasExplicitFilters = hasExplicitFilters (attributes );
130127 }
131128
@@ -337,7 +334,9 @@ private Streamable<TypeFilter> parseFilters(String attributeName) {
337334
338335 AnnotationAttributes [] filters = attributes .getAnnotationArray (attributeName );
339336
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 ()));
341340 }
342341
343342 /**
@@ -354,71 +353,6 @@ private Optional<String> getNullDefaultedAttribute(String attributeName) {
354353 return StringUtils .hasText (attribute ) ? Optional .of (attribute ) : Optional .empty ();
355354 }
356355
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-
422356 /**
423357 * Returns whether there's explicit configuration of include- or exclude filters.
424358 *
0 commit comments