Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Add missing override annotations. Refactor TypeFilterFunction into function to avoid additional property references.
Move off deprecated code.

Closes #3016
  • Loading branch information
mp911de committed Jan 9, 2024
1 parent 2b684ce commit 6623bd8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
Expand All @@ -42,7 +43,7 @@
import org.springframework.util.StringUtils;

/**
* Annotation based {@link RepositoryConfigurationSource}.
* Annotation-based {@link RepositoryConfigurationSource}.
*
* @author Oliver Gierke
* @author Thomas Darimont
Expand All @@ -67,9 +68,7 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura
private final AnnotationMetadata configMetadata;
private final AnnotationMetadata enableAnnotationMetadata;
private final AnnotationAttributes attributes;
private final ResourceLoader resourceLoader;
private final Environment environment;
private final BeanDefinitionRegistry registry;
private final Function<AnnotationAttributes, Stream<TypeFilter>> typeFilterFunction;
private final boolean hasExplicitFilters;

/**
Expand All @@ -83,7 +82,7 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura
* @param registry must not be {@literal null}.
* @deprecated since 2.2. Prefer to use overload taking a {@link BeanNameGenerator} additionally.
*/
@Deprecated
@Deprecated(since = "2.2")
public AnnotationRepositoryConfigurationSource(AnnotationMetadata metadata, Class<? extends Annotation> annotation,
ResourceLoader resourceLoader, Environment environment, BeanDefinitionRegistry registry) {
this(metadata, annotation, resourceLoader, environment, registry, null);
Expand Down Expand Up @@ -120,12 +119,12 @@ public AnnotationRepositoryConfigurationSource(AnnotationMetadata metadata, Clas
this.attributes = new AnnotationAttributes(annotationAttributes);
this.enableAnnotationMetadata = AnnotationMetadata.introspect(annotation);
this.configMetadata = metadata;
this.resourceLoader = resourceLoader;
this.environment = environment;
this.registry = registry;
this.typeFilterFunction = it -> TypeFilterUtils.createTypeFiltersFor(it, environment, resourceLoader, registry)
.stream();
this.hasExplicitFilters = hasExplicitFilters(attributes);
}

@Override
public Streamable<String> getBasePackages() {

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

Set<String> packages = new HashSet<>();
Set<String> packages = new HashSet<>(value.length + basePackages.length + basePackageClasses.length);
packages.addAll(Arrays.asList(value));
packages.addAll(Arrays.asList(basePackages));

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

@Override
public Optional<Object> getQueryLookupStrategyKey() {
return Optional.ofNullable(attributes.get(QUERY_LOOKUP_STRATEGY));
}

@Override
public Optional<String> getNamedQueryLocation() {
return getNullDefaultedAttribute(NAMED_QUERIES_LOCATION);
}

@Override
public Optional<String> getRepositoryImplementationPostfix() {
return getNullDefaultedAttribute(REPOSITORY_IMPLEMENTATION_POSTFIX);
}

@Override
@NonNull
public Object getSource() {
return configMetadata;
Expand Down Expand Up @@ -265,25 +268,22 @@ public BootstrapMode getBootstrapMode() {
public String getResourceDescription() {

String simpleClassName = ClassUtils.getShortName(configMetadata.getClassName());
String annoationClassName = ClassUtils.getShortName(enableAnnotationMetadata.getClassName());
String annotationClassName = ClassUtils.getShortName(enableAnnotationMetadata.getClassName());

return String.format("@%s declared on %s", annoationClassName, simpleClassName);
return String.format("@%s declared on %s", annotationClassName, simpleClassName);
}

private Streamable<TypeFilter> parseFilters(String attributeName) {

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

return Streamable.of(() -> Arrays.stream(filters) //
.flatMap(it -> TypeFilterUtils.createTypeFiltersFor(it, this.environment, this.resourceLoader, this.registry)
.stream()));
return Streamable.of(() -> Arrays.stream(filters).flatMap(typeFilterFunction));
}

/**
* Returns the {@link String} attribute with the given name and defaults it to {@literal Optional#empty()} in case
* it's empty.
*
* @param attributeName
* @param attributeName must not be {@literal null}.
* @return
*/
private Optional<String> getNullDefaultedAttribute(String attributeName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void setUp() {
registry = mock(BeanDefinitionRegistry.class);

source = new AnnotationRepositoryConfigurationSource(annotationMetadata, EnableRepositories.class, resourceLoader,
environment, registry);
environment, registry, null);
}

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

assertThat(configurationSource.getBasePackages()).contains("");
}
Expand All @@ -132,7 +132,7 @@ void ignoresMissingRepositoryBaseClassNameAttribute() {

AnnotationMetadata metadata = new StandardAnnotationMetadata(ConfigWithSampleAnnotation.class, true);
RepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(metadata,
SampleAnnotation.class, resourceLoader, environment, registry);
SampleAnnotation.class, resourceLoader, environment, registry, null);

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

AnnotationMetadata metadata = new StandardAnnotationMetadata(type, true);
return new AnnotationRepositoryConfigurationSource(metadata, EnableRepositories.class, resourceLoader, environment,
registry);
registry, null);
}

static class Person {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void registersRepositoryBeanNameAsAttribute() {

RepositoryConfigurationSource configSource = new AnnotationRepositoryConfigurationSource(
new StandardAnnotationMetadata(TestConfig.class, true), EnableRepositories.class, context, environment,
context.getDefaultListableBeanFactory());
context.getDefaultListableBeanFactory(), null);

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

Expand Down Expand Up @@ -112,7 +112,7 @@ void writesRepositoryScanningMetrics() {

RepositoryConfigurationSource configSource = new AnnotationRepositoryConfigurationSource(
new StandardAnnotationMetadata(TestConfig.class, true), EnableRepositories.class, context, environment,
context.getDefaultListableBeanFactory());
context.getDefaultListableBeanFactory(), null);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void rejectsReactiveRepositories() {
var registry = mock(BeanDefinitionRegistry.class);

RepositoryConfigurationSource source = new AnnotationRepositoryConfigurationSource(annotationMetadata,
EnableRepositories.class, resourceLoader, environment, registry);
EnableRepositories.class, resourceLoader, environment, registry, null);

assertThatThrownBy(() -> extension.getRepositoryConfigurations(source, resourceLoader))
.isInstanceOf(InvalidDataAccessApiUsageException.class)
Expand Down

0 comments on commit 6623bd8

Please sign in to comment.