Skip to content

Commit a94d6f2

Browse files
Florian Cramermp911de
authored andcommitted
Use TypeFilterUtils to create TypeFilters.
Original pull request: #2647. Closes #2481
1 parent 4a087f4 commit a94d6f2

File tree

1 file changed

+9
-75
lines changed

1 file changed

+9
-75
lines changed

src/main/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSource.java

Lines changed: 9 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,22 @@
1616
package org.springframework.data.repository.config;
1717

1818
import java.lang.annotation.Annotation;
19-
import java.util.ArrayList;
2019
import java.util.Arrays;
2120
import java.util.HashSet;
22-
import java.util.List;
2321
import java.util.Map;
2422
import java.util.Optional;
2523
import java.util.Set;
26-
import java.util.regex.Pattern;
2724
import java.util.stream.Stream;
2825

29-
import org.springframework.beans.BeanUtils;
3026
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
3127
import org.springframework.beans.factory.support.BeanNameGenerator;
3228
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
3329
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
34-
import org.springframework.context.annotation.FilterType;
30+
import org.springframework.context.annotation.TypeFilterUtils;
3531
import org.springframework.core.annotation.AnnotationAttributes;
3632
import org.springframework.core.env.Environment;
3733
import org.springframework.core.io.ResourceLoader;
3834
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;
4335
import org.springframework.core.type.filter.TypeFilter;
4436
import org.springframework.data.config.ConfigurationUtils;
4537
import org.springframework.data.util.Streamable;
@@ -58,6 +50,7 @@
5850
* @author Jens Schauder
5951
* @author Mark Paluch
6052
* @author Johannes Englmeier
53+
* @author Florian Cramer
6154
*/
6255
public 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

Comments
 (0)