Skip to content

DATACMNS-1261 - Apply Java 8 Functional types and Lambdas to simplify… #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@ public interface ConfigurationUtils {
* @return
* @throws IllegalArgumentException if no {@link ResourceLoader} can be obtained from the {@link XmlReaderContext}.
*/
public static ResourceLoader getRequiredResourceLoader(XmlReaderContext context) {
static ResourceLoader getRequiredResourceLoader(XmlReaderContext context) {

Assert.notNull(context, "XmlReaderContext must not be null!");

ResourceLoader resourceLoader = context.getResourceLoader();

if (resourceLoader == null) {
throw new IllegalArgumentException("Could not obtain ResourceLoader from XmlReaderContext!");
}
Assert.notNull(resourceLoader, "Could not obtain ResourceLoader from XmlReaderContext!");

return resourceLoader;
}
Expand All @@ -57,7 +55,7 @@ public static ResourceLoader getRequiredResourceLoader(XmlReaderContext context)
* @return
* @throws IllegalArgumentException if no {@link ClassLoader} can be obtained from the given {@link XmlReaderContext}.
*/
public static ClassLoader getRequiredClassLoader(XmlReaderContext context) {
static ClassLoader getRequiredClassLoader(XmlReaderContext context) {
return getRequiredClassLoader(getRequiredResourceLoader(context));
}

Expand All @@ -68,15 +66,13 @@ public static ClassLoader getRequiredClassLoader(XmlReaderContext context) {
* @return
* @throws IllegalArgumentException if the given {@link ResourceLoader} does not expose a {@link ClassLoader}.
*/
public static ClassLoader getRequiredClassLoader(ResourceLoader resourceLoader) {
static ClassLoader getRequiredClassLoader(ResourceLoader resourceLoader) {

Assert.notNull(resourceLoader, "ResourceLoader must not be null!");

ClassLoader classLoader = resourceLoader.getClassLoader();

if (classLoader == null) {
throw new IllegalArgumentException("Could not obtain ClassLoader from ResourceLoader!");
}
Assert.notNull(classLoader, "Could not obtain ClassLoader from ResourceLoader!");

return classLoader;
}
Expand All @@ -88,16 +84,14 @@ public static ClassLoader getRequiredClassLoader(ResourceLoader resourceLoader)
* @return
* @throws IllegalArgumentException if the given {@link BeanDefinition} does not contain a bean class name.
*/
public static String getRequiredBeanClassName(BeanDefinition beanDefinition) {
static String getRequiredBeanClassName(BeanDefinition beanDefinition) {

Assert.notNull(beanDefinition, "BeanDefinition must not be null!");

String result = beanDefinition.getBeanClassName();

if (result == null) {
throw new IllegalArgumentException(
String.format("Could not obtain required bean class name from BeanDefinition!", beanDefinition));
}
Assert.notNull(result, String.format("Could not obtain required bean class name from BeanDefinition [%s]!",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change has performance impact as String.format(…) is invoked regardless of whether it's required to. We should either stick with the previous variant or use a message supplier. There are also occurrences of similar changes.

beanDefinition));

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ public AnnotationRepositoryConfigurationSource(AnnotationMetadata metadata, Clas

Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(annotation.getName());

if (annotationAttributes == null) {
throw new IllegalStateException(String.format("Unable to obtain annotation attributes for %s!", annotation));
}
Assert.state(annotationAttributes != null,
String.format("Unable to obtain annotation attributes for %s!", annotation));

this.attributes = new AnnotationAttributes(annotationAttributes);
this.enableAnnotationMetadata = new StandardAnnotationMetadata(annotation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanD
AnnotationRepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(
annotationMetadata, getAnnotation(), resourceLoader, environment, registry);

RepositoryConfigurationExtension extension = getExtension();
RepositoryConfigurationUtils.exposeRegistration(extension, registry, configurationSource);
RepositoryConfigurationExtension configurationExtension = getExtension();
RepositoryConfigurationUtils.exposeRegistration(configurationExtension, registry, configurationSource);

RepositoryConfigurationDelegate delegate = new RepositoryConfigurationDelegate(configurationSource, resourceLoader,
environment);
RepositoryConfigurationDelegate delegate =
new RepositoryConfigurationDelegate(configurationSource, resourceLoader, environment);

delegate.registerRepositoriesIn(registry, extension);
delegate.registerRepositoriesIn(registry, configurationExtension);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.RepositoryDefinition;
import org.springframework.data.repository.util.ClassUtils;
import org.springframework.data.util.IterableUtils;
import org.springframework.util.Assert;

/**
Expand All @@ -61,15 +62,12 @@ public RepositoryComponentProvider(Iterable<? extends TypeFilter> includeFilters

super(false);

Assert.notNull(includeFilters, "Include filters must not be null!");
Assert.notNull(registry, "BeanDefinitionRegistry must not be null!");

this.registry = registry;

if (includeFilters.iterator().hasNext()) {
for (TypeFilter filter : includeFilters) {
addIncludeFilter(filter);
}
if (IterableUtils.isNotEmpty(includeFilters)) {
includeFilters.forEach(this::addIncludeFilter);
} else {
super.addIncludeFilter(new InterfaceTypeFilter(Repository.class));
super.addIncludeFilter(new AnnotationTypeFilter(RepositoryDefinition.class, true, true));
Expand All @@ -79,9 +77,11 @@ public RepositoryComponentProvider(Iterable<? extends TypeFilter> includeFilters
}

/**
* Custom extension of {@link #addIncludeFilter(TypeFilter)} to extend the added {@link TypeFilter}. For the
* {@link TypeFilter} handed we'll have two filters registered: one additionally enforcing the
* {@link RepositoryDefinition} annotation, the other one forcing the extension of {@link Repository}.
* Custom extension of {@link ClassPathScanningCandidateComponentProvider#addIncludeFilter(TypeFilter)}
* to extend the added {@link TypeFilter}.
*
* For the {@link TypeFilter} handed we will have two filters registered: one additionally enforcing the
* {@link RepositoryDefinition} annotation and the other forcing the extension of {@link Repository}.
*
* @see ClassPathScanningCandidateComponentProvider#addIncludeFilter(TypeFilter)
*/
Expand Down Expand Up @@ -123,11 +123,8 @@ public Set<BeanDefinition> findCandidateComponents(String basePackage) {

Set<BeanDefinition> candidates = super.findCandidateComponents(basePackage);

for (BeanDefinition candidate : candidates) {
if (candidate instanceof AnnotatedBeanDefinition) {
AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
}
}
candidates.stream().filter(bd -> bd instanceof AnnotatedBeanDefinition).forEach(bd ->
AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) bd));

return candidates;
}
Expand All @@ -153,7 +150,8 @@ public boolean isConsiderNestedRepositoryInterfaces() {
* Controls whether nested inner-class {@link Repository} interface definitions should be considered for automatic
* discovery. This defaults to {@literal false}.
*
* @param considerNestedRepositoryInterfaces
* @param considerNestedRepositoryInterfaces boolean value indicating whether nested inner-class {@link Repository}
* interface definitions should be considered for automatic discovery.
*/
public void setConsiderNestedRepositoryInterfaces(boolean considerNestedRepositoryInterfaces) {
this.considerNestedRepositoryInterfaces = considerNestedRepositoryInterfaces;
Expand All @@ -170,9 +168,9 @@ private static class InterfaceTypeFilter extends AssignableTypeFilter {
/**
* Creates a new {@link InterfaceTypeFilter}.
*
* @param targetType
* @param targetType {@link Class type} of the interface.
*/
public InterfaceTypeFilter(Class<?> targetType) {
private InterfaceTypeFilter(Class<?> targetType) {
super(targetType);
}

Expand Down Expand Up @@ -202,7 +200,7 @@ private static class AllTypeFilter implements TypeFilter {
*
* @param delegates must not be {@literal null}.
*/
public AllTypeFilter(List<TypeFilter> delegates) {
private AllTypeFilter(List<TypeFilter> delegates) {

Assert.notNull(delegates, "TypeFilter deleages must not be null!");
this.delegates = delegates;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -69,12 +70,13 @@ public class RepositoryConfigurationDelegate {
public RepositoryConfigurationDelegate(RepositoryConfigurationSource configurationSource,
ResourceLoader resourceLoader, Environment environment) {

this.isXml = configurationSource instanceof XmlRepositoryConfigurationSource;
Assert.notNull(resourceLoader, "ResourceLoader must not be null!");

boolean isAnnotation = configurationSource instanceof AnnotationRepositoryConfigurationSource;
this.isXml = configurationSource instanceof XmlRepositoryConfigurationSource;

Assert.isTrue(isXml || isAnnotation,
"Configuration source must either be an Xml- or an AnnotationBasedConfigurationSource!");
Assert.notNull(resourceLoader, "ResourceLoader must not be null!");
Assert.isTrue(this.isXml || isAnnotation,
"Configuration source must either be an XML-based or an Annotation-based RepositoryConfigurationSource!");

this.configurationSource = configurationSource;
this.resourceLoader = resourceLoader;
Expand All @@ -83,12 +85,14 @@ public RepositoryConfigurationDelegate(RepositoryConfigurationSource configurati
}

/**
* Defaults the environment in case the given one is null. Used as fallback, in case the legacy constructor was
* invoked.
* Defaults the {@link Environment} in case the given one is {@literal null}.
*
* Used as fallback in case the legacy constructor was invoked.
*
* @param environment can be {@literal null}.
* @param resourceLoader can be {@literal null}.
* @return
* @return the given {@link Environment} if not {@literal null} or an {@link Environment} obtained
* from the {@link ResourceLoader} if {@link ResourceLoader} is {@link EnvironmentCapable}.
*/
private static Environment defaultEnvironment(@Nullable Environment environment,
@Nullable ResourceLoader resourceLoader) {
Expand All @@ -104,21 +108,22 @@ private static Environment defaultEnvironment(@Nullable Environment environment,
/**
* Registers the found repositories in the given {@link BeanDefinitionRegistry}.
*
* @param registry
* @param extension
* @return {@link BeanComponentDefinition}s for all repository bean definitions found.
* @param registry {@link BeanDefinitionRegistry} used to register {@link Repository Repositories}.
* @param extension {@link RepositoryConfigurationExtension} containing store specific configuration meta-data.
* @return {@link BeanComponentDefinition}s for all {@link Repository} bean definitions found.
*/
public List<BeanComponentDefinition> registerRepositoriesIn(BeanDefinitionRegistry registry,
RepositoryConfigurationExtension extension) {

extension.registerBeansForRoot(registry, configurationSource);

RepositoryBeanDefinitionBuilder builder = new RepositoryBeanDefinitionBuilder(registry, extension, resourceLoader,
environment);
RepositoryBeanDefinitionBuilder builder =
new RepositoryBeanDefinitionBuilder(registry, extension, resourceLoader, environment);

List<BeanComponentDefinition> definitions = new ArrayList<>();

for (RepositoryConfiguration<? extends RepositoryConfigurationSource> configuration : extension
.getRepositoryConfigurations(configurationSource, resourceLoader, inMultiStoreMode)) {
for (RepositoryConfiguration<? extends RepositoryConfigurationSource> configuration
: extension.getRepositoryConfigurations(configurationSource, resourceLoader, inMultiStoreMode)) {

BeanDefinitionBuilder definitionBuilder = builder.build(configuration);

Expand Down Expand Up @@ -152,7 +157,7 @@ public List<BeanComponentDefinition> registerRepositoriesIn(BeanDefinitionRegist
* than a single type is considered a multi-store configuration scenario which will trigger stricter repository
* scanning.
*
* @return
* @return a boolean value indicating if multiple Spring Data store implementations are present on the classpath.
*/
private boolean multipleStoresDetected() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public Streamable<BeanDefinition> getCandidates(ResourceLoader loader) {
scanner.setEnvironment(environment);
scanner.setResourceLoader(loader);

getExcludeFilters().forEach(it -> scanner.addExcludeFilter(it));
getExcludeFilters().forEach(scanner::addExcludeFilter);

return Streamable.of(() -> getBasePackages().stream()//
.flatMap(it -> scanner.findCandidateComponents(it).stream()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package org.springframework.data.repository.query;

import static java.lang.String.*;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -41,11 +39,13 @@ public abstract class Parameters<S extends Parameters<S, T>, T extends Parameter

public static final List<Class<?>> TYPES = Arrays.asList(Pageable.class, Sort.class);

private static final String PARAM_ON_SPECIAL = format("You must not user @%s on a parameter typed %s or %s",
Param.class.getSimpleName(), Pageable.class.getSimpleName(), Sort.class.getSimpleName());
private static final String ALL_OR_NOTHING = String.format(
"Either use @%s on all parameters except %s and %s typed once, or none at all!", Param.class.getSimpleName(),
Pageable.class.getSimpleName(), Sort.class.getSimpleName());
private static final String PARAM_ON_SPECIAL =
String.format("You must not use @%s on a parameter typed %s or %s",
Param.class.getSimpleName(), Pageable.class.getSimpleName(), Sort.class.getSimpleName());

private static final String ALL_OR_NOTHING =
String.format("Either use @%s on all parameters except parameters of type %s and %s, or none at all!",
Param.class.getSimpleName(), Pageable.class.getSimpleName(), Sort.class.getSimpleName());

private final ParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer();
private final int pageableIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ public QueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory

Parameters.TYPES.stream()//
.filter(type -> getNumberOfOccurences(method, type) > 1)//
.findFirst().ifPresent(type -> {
.findFirst()
.ifPresent(type -> {
throw new IllegalStateException(
String.format("Method must only one argument of type %s! Offending method: %s", type.getSimpleName(),
method.toString()));
String.format("Method must only have one argument of type %s! Offending method: %s",
type.getSimpleName(), method.toString()));
});

this.method = method;
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/org/springframework/data/util/IterableUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.util;

import java.util.Collections;

import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;

/**
* Utility class for working with {@link Iterable} objects.
*
* @author John Blum
* @see java.lang.Iterable
* @since 2.1.0
*/
public abstract class IterableUtils {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure that Spring Data is the best place for such a utility. I'd suggest to move it to Spring Framework.


/**
* Determines whether the given {@link Iterable} is empty.
*
* @param iterable {@link Iterable} to evaluate; can be {@literal null}.
* @return {@literal true} if the given {@link Iterable} is empty,
* otherwise return {@literal false}.
* @see #nullSafeIterable(Iterable)
* @see java.lang.Iterable
*/
public static boolean isEmpty(@Nullable Iterable<?> iterable) {
return !isNotEmpty(iterable);
}

/**
* Determines if the given {@link Iterable} is not empty.
*
* @param iterable {@link Iterable} to evaluate; can be {@literal null}.
* @return {@literal true} if the given {@link Iterable} is not empty,
* otherwise return {@literal false}.
* @see #nullSafeIterable(Iterable)
* @see java.lang.Iterable
*/
public static boolean isNotEmpty(@Nullable Iterable<?> iterable) {
return nullSafeIterable(iterable).iterator().hasNext();
}

/**
* Protects against {@literal null} {@link Iterable} references.
*
* @param <T> {@link Class type} of elements contained by the {@link Iterable}.
* @param it {@link Iterable} to safe-guard from {@literal null}.
* @return the given {@link Iterable} if not {@literal null} or an empty {@link Iterable}.
* @see java.lang.Iterable
*/
@NonNull
public static <T> Iterable<T> nullSafeIterable(@Nullable Iterable<T> it) {
return it != null ? it : Collections::emptyIterator;
}
}
Loading