Skip to content

Commit 1150e40

Browse files
committed
DATACMNS-1497 - Polishing.
1 parent 753d8bb commit 1150e40

File tree

2 files changed

+89
-91
lines changed

2 files changed

+89
-91
lines changed

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

+84-86
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Optional;
2525
import java.util.Set;
2626
import java.util.regex.Pattern;
27+
import java.util.stream.Stream;
2728

2829
import javax.annotation.Nonnull;
2930

@@ -129,24 +130,6 @@ public AnnotationRepositoryConfigurationSource(AnnotationMetadata metadata, Clas
129130
this.hasExplicitFilters = hasExplicitFilters(attributes);
130131
}
131132

132-
/**
133-
* Returns whether there's explicit configuration of include- or exclude filters.
134-
*
135-
* @param attributes must not be {@literal null}.
136-
* @return
137-
*/
138-
private static boolean hasExplicitFilters(AnnotationAttributes attributes) {
139-
140-
for (String attribute : Arrays.asList("includeFilters", "excludeFilters")) {
141-
142-
if (attributes.getAnnotationArray(attribute).length > 0) {
143-
return true;
144-
}
145-
}
146-
147-
return false;
148-
}
149-
150133
/*
151134
* (non-Javadoc)
152135
* @see org.springframework.data.repository.config.RepositoryConfigurationSource#getBasePackages()
@@ -226,25 +209,6 @@ public Streamable<TypeFilter> getExcludeFilters() {
226209
return parseFilters("excludeFilters");
227210
}
228211

229-
private Streamable<TypeFilter> parseFilters(String attributeName) {
230-
231-
AnnotationAttributes[] filters = attributes.getAnnotationArray(attributeName);
232-
233-
return Streamable.of(() -> Arrays.stream(filters).flatMap(it -> typeFiltersFor(it).stream()));
234-
}
235-
236-
/**
237-
* Returns the {@link String} attribute with the given name and defaults it to {@literal Optional#empty()} in case
238-
* it's empty.
239-
*
240-
* @param attributeName
241-
* @return
242-
*/
243-
private Optional<String> getNullDefaultedAttribute(String attributeName) {
244-
String attribute = attributes.getString(attributeName);
245-
return StringUtils.hasText(attribute) ? Optional.of(attribute) : Optional.empty();
246-
}
247-
248212
/*
249213
* (non-Javadoc)
250214
* @see org.springframework.data.repository.config.RepositoryConfigurationSource#getRepositoryFactoryBeanClassName()
@@ -288,55 +252,6 @@ public AnnotationMetadata getEnableAnnotationMetadata() {
288252
return enableAnnotationMetadata;
289253
}
290254

291-
/**
292-
* Copy of {@code ComponentScanAnnotationParser#typeFiltersFor}.
293-
*
294-
* @param filterAttributes
295-
* @return
296-
*/
297-
private List<TypeFilter> typeFiltersFor(AnnotationAttributes filterAttributes) {
298-
299-
List<TypeFilter> typeFilters = new ArrayList<>();
300-
FilterType filterType = filterAttributes.getEnum("type");
301-
302-
for (Class<?> filterClass : filterAttributes.getClassArray("value")) {
303-
switch (filterType) {
304-
case ANNOTATION:
305-
Assert.isAssignable(Annotation.class, filterClass,
306-
"An error occured when processing a @ComponentScan " + "ANNOTATION type filter: ");
307-
@SuppressWarnings("unchecked")
308-
Class<Annotation> annoClass = (Class<Annotation>) filterClass;
309-
typeFilters.add(new AnnotationTypeFilter(annoClass));
310-
break;
311-
case ASSIGNABLE_TYPE:
312-
typeFilters.add(new AssignableTypeFilter(filterClass));
313-
break;
314-
case CUSTOM:
315-
Assert.isAssignable(TypeFilter.class, filterClass,
316-
"An error occured when processing a @ComponentScan " + "CUSTOM type filter: ");
317-
typeFilters.add(BeanUtils.instantiateClass(filterClass, TypeFilter.class));
318-
break;
319-
default:
320-
throw new IllegalArgumentException("Unknown filter type " + filterType);
321-
}
322-
}
323-
324-
for (String expression : getPatterns(filterAttributes)) {
325-
326-
String rawName = filterType.toString();
327-
328-
if ("REGEX".equals(rawName)) {
329-
typeFilters.add(new RegexPatternTypeFilter(Pattern.compile(expression)));
330-
} else if ("ASPECTJ".equals(rawName)) {
331-
typeFilters.add(new AspectJTypeFilter(expression, this.resourceLoader.getClassLoader()));
332-
} else {
333-
throw new IllegalArgumentException("Unknown filter type " + filterType);
334-
}
335-
}
336-
337-
return typeFilters;
338-
}
339-
340255
/*
341256
* (non-Javadoc)
342257
* @see org.springframework.data.repository.config.RepositoryConfigurationSourceSupport#shouldConsiderNestedRepositories()
@@ -406,6 +321,76 @@ public BootstrapMode getBootstrapMode() {
406321
}
407322
}
408323

324+
private Streamable<TypeFilter> parseFilters(String attributeName) {
325+
326+
AnnotationAttributes[] filters = attributes.getAnnotationArray(attributeName);
327+
328+
return Streamable.of(() -> Arrays.stream(filters).flatMap(it -> typeFiltersFor(it).stream()));
329+
}
330+
331+
/**
332+
* Returns the {@link String} attribute with the given name and defaults it to {@literal Optional#empty()} in case
333+
* it's empty.
334+
*
335+
* @param attributeName
336+
* @return
337+
*/
338+
private Optional<String> getNullDefaultedAttribute(String attributeName) {
339+
340+
String attribute = attributes.getString(attributeName);
341+
342+
return StringUtils.hasText(attribute) ? Optional.of(attribute) : Optional.empty();
343+
}
344+
345+
/**
346+
* Copy of {@code ComponentScanAnnotationParser#typeFiltersFor}.
347+
*
348+
* @param filterAttributes
349+
* @return
350+
*/
351+
private List<TypeFilter> typeFiltersFor(AnnotationAttributes filterAttributes) {
352+
353+
List<TypeFilter> typeFilters = new ArrayList<>();
354+
FilterType filterType = filterAttributes.getEnum("type");
355+
356+
for (Class<?> filterClass : filterAttributes.getClassArray("value")) {
357+
switch (filterType) {
358+
case ANNOTATION:
359+
Assert.isAssignable(Annotation.class, filterClass,
360+
"An error occured when processing a @ComponentScan " + "ANNOTATION type filter: ");
361+
@SuppressWarnings("unchecked")
362+
Class<Annotation> annoClass = (Class<Annotation>) filterClass;
363+
typeFilters.add(new AnnotationTypeFilter(annoClass));
364+
break;
365+
case ASSIGNABLE_TYPE:
366+
typeFilters.add(new AssignableTypeFilter(filterClass));
367+
break;
368+
case CUSTOM:
369+
Assert.isAssignable(TypeFilter.class, filterClass,
370+
"An error occured when processing a @ComponentScan " + "CUSTOM type filter: ");
371+
typeFilters.add(BeanUtils.instantiateClass(filterClass, TypeFilter.class));
372+
break;
373+
default:
374+
throw new IllegalArgumentException("Unknown filter type " + filterType);
375+
}
376+
}
377+
378+
for (String expression : getPatterns(filterAttributes)) {
379+
380+
String rawName = filterType.toString();
381+
382+
if ("REGEX".equals(rawName)) {
383+
typeFilters.add(new RegexPatternTypeFilter(Pattern.compile(expression)));
384+
} else if ("ASPECTJ".equals(rawName)) {
385+
typeFilters.add(new AspectJTypeFilter(expression, this.resourceLoader.getClassLoader()));
386+
} else {
387+
throw new IllegalArgumentException("Unknown filter type " + filterType);
388+
}
389+
}
390+
391+
return typeFilters;
392+
}
393+
409394
/**
410395
* Safely reads the {@code pattern} attribute from the given {@link AnnotationAttributes} and returns an empty list if
411396
* the attribute is not present.
@@ -422,6 +407,18 @@ private String[] getPatterns(AnnotationAttributes filterAttributes) {
422407
}
423408
}
424409

410+
/**
411+
* Returns whether there's explicit configuration of include- or exclude filters.
412+
*
413+
* @param attributes must not be {@literal null}.
414+
* @return
415+
*/
416+
private static boolean hasExplicitFilters(AnnotationAttributes attributes) {
417+
418+
return Stream.of("includeFilters", "excludeFilters") //
419+
.anyMatch(it -> attributes.getAnnotationArray(it).length > 0);
420+
}
421+
425422
/**
426423
* Returns the {@link BeanNameGenerator} to use falling back to an {@link AnnotationBeanNameGenerator} if either the
427424
* given generator is {@literal null} or it's the one locally declared in {@link ConfigurationClassPostProcessor}'s
@@ -430,6 +427,7 @@ private String[] getPatterns(AnnotationAttributes filterAttributes) {
430427
*
431428
* @param generator can be {@literal null}.
432429
* @return
430+
* @since 2.2
433431
*/
434432
private static BeanNameGenerator defaultBeanNameGenerator(@Nullable BeanNameGenerator generator) {
435433

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public boolean shouldConsiderNestedRepositories() {
123123
return false;
124124
}
125125

126-
/*
126+
/*
127127
* (non-Javadoc)
128128
* @see org.springframework.data.repository.config.RepositoryConfigurationSource#toImplementationDetectionConfiguration()
129129
*/
@@ -138,7 +138,7 @@ private class SpringImplementationDetectionConfiguration implements Implementati
138138
private final RepositoryConfigurationSource source;
139139
private final @Getter MetadataReaderFactory metadataReaderFactory;
140140

141-
/*
141+
/*
142142
* (non-Javadoc)
143143
* @see org.springframework.data.repository.config.CustomRepositoryImplementationDetector.ImplementationDetectionConfiguration#getImplementationPostfix()
144144
*/
@@ -148,7 +148,7 @@ public String getImplementationPostfix() {
148148
.orElse(DefaultRepositoryConfiguration.DEFAULT_REPOSITORY_IMPLEMENTATION_POSTFIX);
149149
}
150150

151-
/*
151+
/*
152152
* (non-Javadoc)
153153
* @see org.springframework.data.repository.config.CustomRepositoryImplementationDetector.ImplementationDetectionConfiguration#getBasePackages()
154154
*/
@@ -157,7 +157,7 @@ public Streamable<String> getBasePackages() {
157157
return source.getBasePackages();
158158
}
159159

160-
/*
160+
/*
161161
* (non-Javadoc)
162162
* @see org.springframework.data.repository.config.CustomRepositoryImplementationDetector.ImplementationDetectionConfiguration#getExcludeFilters()
163163
*/
@@ -166,7 +166,7 @@ public Streamable<TypeFilter> getExcludeFilters() {
166166
return source.getExcludeFilters();
167167
}
168168

169-
/*
169+
/*
170170
* (non-Javadoc)
171171
* @see org.springframework.data.repository.config.CustomRepositoryImplementationDetector.ImplementationDetectionConfiguration#generateBeanName(org.springframework.beans.factory.config.BeanDefinition)
172172
*/

0 commit comments

Comments
 (0)