Skip to content

Commit 2a299b2

Browse files
Florian Cramermp911de
Florian Cramer
authored andcommitted
Use TypeFilterUtils to create TypeFilters.
Original pull request: #2647. Closes #2481
1 parent dfc3060 commit 2a299b2

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

+9-75
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

@@ -277,7 +274,9 @@ private Streamable<TypeFilter> parseFilters(String attributeName) {
277274

278275
AnnotationAttributes[] filters = attributes.getAnnotationArray(attributeName);
279276

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()));
281280
}
282281

283282
/**
@@ -294,71 +293,6 @@ private Optional<String> getNullDefaultedAttribute(String attributeName) {
294293
return StringUtils.hasText(attribute) ? Optional.of(attribute) : Optional.empty();
295294
}
296295

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-
362296
/**
363297
* Returns whether there's explicit configuration of include- or exclude filters.
364298
*

0 commit comments

Comments
 (0)