Skip to content

Commit 20d150f

Browse files
committed
Polishing.
Add missing override annotations. Refactor TypeFilterFunction into function to avoid additional property references. Move off deprecated code. Closes #3016
1 parent ac967e2 commit 20d150f

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

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

+16-16
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Map;
2222
import java.util.Optional;
2323
import java.util.Set;
24+
import java.util.function.Function;
2425
import java.util.stream.Stream;
2526

2627
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
@@ -42,7 +43,7 @@
4243
import org.springframework.util.StringUtils;
4344

4445
/**
45-
* Annotation based {@link RepositoryConfigurationSource}.
46+
* Annotation-based {@link RepositoryConfigurationSource}.
4647
*
4748
* @author Oliver Gierke
4849
* @author Thomas Darimont
@@ -67,9 +68,7 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura
6768
private final AnnotationMetadata configMetadata;
6869
private final AnnotationMetadata enableAnnotationMetadata;
6970
private final AnnotationAttributes attributes;
70-
private final ResourceLoader resourceLoader;
71-
private final Environment environment;
72-
private final BeanDefinitionRegistry registry;
71+
private final Function<AnnotationAttributes, Stream<TypeFilter>> typeFilterFunction;
7372
private final boolean hasExplicitFilters;
7473

7574
/**
@@ -83,7 +82,7 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura
8382
* @param registry must not be {@literal null}.
8483
* @deprecated since 2.2. Prefer to use overload taking a {@link BeanNameGenerator} additionally.
8584
*/
86-
@Deprecated
85+
@Deprecated(since = "2.2")
8786
public AnnotationRepositoryConfigurationSource(AnnotationMetadata metadata, Class<? extends Annotation> annotation,
8887
ResourceLoader resourceLoader, Environment environment, BeanDefinitionRegistry registry) {
8988
this(metadata, annotation, resourceLoader, environment, registry, null);
@@ -120,12 +119,12 @@ public AnnotationRepositoryConfigurationSource(AnnotationMetadata metadata, Clas
120119
this.attributes = new AnnotationAttributes(annotationAttributes);
121120
this.enableAnnotationMetadata = AnnotationMetadata.introspect(annotation);
122121
this.configMetadata = metadata;
123-
this.resourceLoader = resourceLoader;
124-
this.environment = environment;
125-
this.registry = registry;
122+
this.typeFilterFunction = it -> TypeFilterUtils.createTypeFiltersFor(it, environment, resourceLoader, registry)
123+
.stream();
126124
this.hasExplicitFilters = hasExplicitFilters(attributes);
127125
}
128126

127+
@Override
129128
public Streamable<String> getBasePackages() {
130129

131130
String[] value = attributes.getStringArray("value");
@@ -139,7 +138,7 @@ public Streamable<String> getBasePackages() {
139138
return Streamable.of(ClassUtils.getPackageName(className));
140139
}
141140

142-
Set<String> packages = new HashSet<>();
141+
Set<String> packages = new HashSet<>(value.length + basePackages.length + basePackageClasses.length);
143142
packages.addAll(Arrays.asList(value));
144143
packages.addAll(Arrays.asList(basePackages));
145144

@@ -150,18 +149,22 @@ public Streamable<String> getBasePackages() {
150149
return Streamable.of(packages);
151150
}
152151

152+
@Override
153153
public Optional<Object> getQueryLookupStrategyKey() {
154154
return Optional.ofNullable(attributes.get(QUERY_LOOKUP_STRATEGY));
155155
}
156156

157+
@Override
157158
public Optional<String> getNamedQueryLocation() {
158159
return getNullDefaultedAttribute(NAMED_QUERIES_LOCATION);
159160
}
160161

162+
@Override
161163
public Optional<String> getRepositoryImplementationPostfix() {
162164
return getNullDefaultedAttribute(REPOSITORY_IMPLEMENTATION_POSTFIX);
163165
}
164166

167+
@Override
165168
@NonNull
166169
public Object getSource() {
167170
return configMetadata;
@@ -265,25 +268,22 @@ public BootstrapMode getBootstrapMode() {
265268
public String getResourceDescription() {
266269

267270
String simpleClassName = ClassUtils.getShortName(configMetadata.getClassName());
268-
String annoationClassName = ClassUtils.getShortName(enableAnnotationMetadata.getClassName());
271+
String annotationClassName = ClassUtils.getShortName(enableAnnotationMetadata.getClassName());
269272

270-
return String.format("@%s declared on %s", annoationClassName, simpleClassName);
273+
return String.format("@%s declared on %s", annotationClassName, simpleClassName);
271274
}
272275

273276
private Streamable<TypeFilter> parseFilters(String attributeName) {
274277

275278
AnnotationAttributes[] filters = attributes.getAnnotationArray(attributeName);
276-
277-
return Streamable.of(() -> Arrays.stream(filters) //
278-
.flatMap(it -> TypeFilterUtils.createTypeFiltersFor(it, this.environment, this.resourceLoader, this.registry)
279-
.stream()));
279+
return Streamable.of(() -> Arrays.stream(filters).flatMap(typeFilterFunction));
280280
}
281281

282282
/**
283283
* Returns the {@link String} attribute with the given name and defaults it to {@literal Optional#empty()} in case
284284
* it's empty.
285285
*
286-
* @param attributeName
286+
* @param attributeName must not be {@literal null}.
287287
* @return
288288
*/
289289
private Optional<String> getNullDefaultedAttribute(String attributeName) {

src/test/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSourceUnitTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void setUp() {
5757
registry = mock(BeanDefinitionRegistry.class);
5858

5959
source = new AnnotationRepositoryConfigurationSource(annotationMetadata, EnableRepositories.class, resourceLoader,
60-
environment, registry);
60+
environment, registry, null);
6161
}
6262

6363
@Test // DATACMNS-47
@@ -115,7 +115,7 @@ void returnsEmptyStringForBasePackage() throws Exception {
115115
var metadata = new StandardAnnotationMetadata(
116116
getClass().getClassLoader().loadClass("TypeInDefaultPackage"), true);
117117
RepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(metadata,
118-
EnableRepositories.class, resourceLoader, environment, registry);
118+
EnableRepositories.class, resourceLoader, environment, registry, null);
119119

120120
assertThat(configurationSource.getBasePackages()).contains("");
121121
}
@@ -132,7 +132,7 @@ void ignoresMissingRepositoryBaseClassNameAttribute() {
132132

133133
AnnotationMetadata metadata = new StandardAnnotationMetadata(ConfigWithSampleAnnotation.class, true);
134134
RepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(metadata,
135-
SampleAnnotation.class, resourceLoader, environment, registry);
135+
SampleAnnotation.class, resourceLoader, environment, registry, null);
136136

137137
assertThat(configurationSource.getRepositoryBaseClassName()).isNotPresent();
138138
}
@@ -169,7 +169,7 @@ private AnnotationRepositoryConfigurationSource getConfigSource(Class<?> type) {
169169

170170
AnnotationMetadata metadata = new StandardAnnotationMetadata(type, true);
171171
return new AnnotationRepositoryConfigurationSource(metadata, EnableRepositories.class, resourceLoader, environment,
172-
registry);
172+
registry, null);
173173
}
174174

175175
static class Person {}

src/test/java/org/springframework/data/repository/config/RepositoryConfigurationDelegateUnitTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void registersRepositoryBeanNameAsAttribute() {
6969

7070
RepositoryConfigurationSource configSource = new AnnotationRepositoryConfigurationSource(
7171
new StandardAnnotationMetadata(TestConfig.class, true), EnableRepositories.class, context, environment,
72-
context.getDefaultListableBeanFactory());
72+
context.getDefaultListableBeanFactory(), null);
7373

7474
var delegate = new RepositoryConfigurationDelegate(configSource, context, environment);
7575

@@ -112,7 +112,7 @@ void writesRepositoryScanningMetrics() {
112112

113113
RepositoryConfigurationSource configSource = new AnnotationRepositoryConfigurationSource(
114114
new StandardAnnotationMetadata(TestConfig.class, true), EnableRepositories.class, context, environment,
115-
context.getDefaultListableBeanFactory());
115+
context.getDefaultListableBeanFactory(), null);
116116

117117
var delegate = new RepositoryConfigurationDelegate(configSource, context, environment);
118118

src/test/java/org/springframework/data/repository/config/RepositoryConfigurationExtensionSupportUnitTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void rejectsReactiveRepositories() {
8080
var registry = mock(BeanDefinitionRegistry.class);
8181

8282
RepositoryConfigurationSource source = new AnnotationRepositoryConfigurationSource(annotationMetadata,
83-
EnableRepositories.class, resourceLoader, environment, registry);
83+
EnableRepositories.class, resourceLoader, environment, registry, null);
8484

8585
assertThatThrownBy(() -> extension.getRepositoryConfigurations(source, resourceLoader))
8686
.isInstanceOf(InvalidDataAccessApiUsageException.class)

0 commit comments

Comments
 (0)