Skip to content

Introduce ManagedTypes. #2635

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 7 commits 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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-GH-2634-SNAPSHOT</version>

<name>Spring Data Core</name>
<description>Core Spring concepts underpinning every Spring Data module.</description>
Expand Down
129 changes: 129 additions & 0 deletions src/main/java/org/springframework/data/domain/ManagedTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright 2022 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
*
* https://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.domain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Stream;

import org.springframework.data.util.Lazy;

/**
* Types managed by a Spring Data implementation. Used to predefine a set of know entities that might need processing
* during the Spring container, Spring Data Repository initialization phase.
*
* @author Christoph Strobl
* @author John Blum
* @see java.lang.FunctionalInterface
* @since 3.0
*/
@FunctionalInterface
public interface ManagedTypes {
Copy link
Member

Choose a reason for hiding this comment

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

How about implementing Streamable<Class<?>> here?


/**
* Factory method used to construct a new instance of {@link ManagedTypes} containing no {@link Class types}.
*
* @return an empty {@link ManagedTypes} instance.
* @see java.util.Collections#emptySet()
* @see #fromIterable(Iterable)
*/
static ManagedTypes empty() {
return fromIterable(Collections.emptySet());
}

/**
* Factory method used to construct {@link ManagedTypes} from the given, required {@link Iterable} of {@link Class
* types}.
*
* @param types {@link Iterable} of {@link Class types} used to initialize the {@link ManagedTypes}; must not be
* {@literal null}.
* @return new instance of {@link ManagedTypes} initialized the given, required {@link Iterable} of {@link Class
* types}.
* @see java.lang.Iterable
* @see #fromStream(Stream)
* @see #fromSupplier(Supplier)
*/
static ManagedTypes fromIterable(Iterable<? extends Class<?>> types) {
return types::forEach;
}

/**
* Factory method used to construct {@link ManagedTypes} from the given, required {@link Stream} of {@link Class
* types}.
*
* @param types {@link Stream} of {@link Class types} used to initialize the {@link ManagedTypes}; must not be
* {@literal null}.
* @return new instance of {@link ManagedTypes} initialized the given, required {@link Stream} of {@link Class types}.
* @see java.util.stream.Stream
* @see #fromIterable(Iterable)
* @see #fromSupplier(Supplier)
*/
static ManagedTypes fromStream(Stream<? extends Class<?>> types) {
return types::forEach;
}

/**
* Factory method used to construct {@link ManagedTypes} from the given, required {@link Supplier} of an
* {@link Iterable} of {@link Class types}.
*
* @param dataProvider {@link Supplier} of an {@link Iterable} of {@link Class types} used to lazily initialize the
* {@link ManagedTypes}; must not be {@literal null}.
* @return new instance of {@link ManagedTypes} initialized the given, required {@link Supplier} of an
* {@link Iterable} of {@link Class types}.
* @see java.util.function.Supplier
* @see java.lang.Iterable
* @see #fromIterable(Iterable)
* @see #fromStream(Stream)
*/
static ManagedTypes fromSupplier(Supplier<Iterable<Class<?>>> dataProvider) {

return new ManagedTypes() {

final Lazy<Iterable<Class<?>>> lazyProvider = Lazy.of(dataProvider);

@Override
public void forEach(Consumer<Class<?>> action) {
lazyProvider.get().forEach(action);
}
};
}

/**
* Applies the given {@link Consumer action} to each of the {@link Class types} contained in this {@link ManagedTypes}
* instance.
*
* @param action {@link Consumer} defining the action to perform on the {@link Class types} contained in this
* {@link ManagedTypes} instance; must not be {@literal null}.
* @see java.util.function.Consumer
*/
void forEach(Consumer<Class<?>> action);

/**
* Returns all the {@link ManagedTypes} in a {@link List}.
*
* @return these {@link ManagedTypes} in a {@link List}; never {@literal null}.
* @see java.util.List
*/
default List<Class<?>> toList() {

List<Class<?>> list = new ArrayList<>(100);
forEach(list::add);
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.core.KotlinDetector;
import org.springframework.core.NativeDetector;
import org.springframework.data.domain.ManagedTypes;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
Expand Down Expand Up @@ -101,7 +102,8 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
private @Nullable ApplicationEventPublisher applicationEventPublisher;
private EvaluationContextProvider evaluationContextProvider = EvaluationContextProvider.DEFAULT;

private Set<? extends Class<?>> initialEntitySet = new HashSet<>();
private ManagedTypes managedTypes = ManagedTypes.empty();

private boolean strict = false;
private SimpleTypeHolder simpleTypeHolder = SimpleTypeHolder.DEFAULT;

Expand Down Expand Up @@ -141,9 +143,21 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
* Sets the {@link Set} of types to populate the context initially.
*
* @param initialEntitySet
* @see #setManagedTypes(ManagedTypes)
*
*/
public void setInitialEntitySet(Set<? extends Class<?>> initialEntitySet) {
this.initialEntitySet = initialEntitySet;
setManagedTypes(ManagedTypes.fromIterable(initialEntitySet));
}

/**
* Sets the types to populate the context initially.
*
* @param managedTypes must not be {@literal null}. Use {@link ManagedTypes#empty()} instead;
* @since 3.0
*/
public void setManagedTypes(ManagedTypes managedTypes) {
this.managedTypes = managedTypes;
}

/**
Expand Down Expand Up @@ -467,7 +481,7 @@ public void afterPropertiesSet() {
* context.
*/
public void initialize() {
initialEntitySet.forEach(this::addPersistentEntity);
managedTypes.forEach(this::addPersistentEntity);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private String registerRepositoryFragments(RepositoryConfiguration<?> configurat
fragmentsBuilder.addConstructorArgValue(fragmentBeanNames);

String fragmentsBeanName = BeanDefinitionReaderUtils
.uniqueBeanName(String.format("%s.%s.fragments", extension.getModuleName().toLowerCase(Locale.ROOT),
.uniqueBeanName(String.format("%s.%s.fragments", extension.getModulePrefix(),
ClassUtils.getShortName(configuration.getRepositoryInterface())), registry);
registry.registerBeanDefinition(fragmentsBeanName, fragmentsBuilder.getBeanDefinition());
return fragmentsBeanName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.DependencyDescriptor;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@
*/
package org.springframework.data.repository.config;

import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Collections;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.StringUtils;

/**
* SPI to implement store specific extension to the repository bean definition registration process.
*
* @see RepositoryConfigurationExtensionSupport
* @author Oliver Gierke
* @author Christoph Strobl
*/
public interface RepositoryConfigurationExtension {

Expand All @@ -35,7 +39,18 @@ public interface RepositoryConfigurationExtension {
*
* @return
*/
String getModuleName();
default String getModuleName() {
return StringUtils.capitalize(getModulePrefix());
}

/**
* Returns the prefix of the module to be used to create the default location for Spring Data named queries and module
* specific bean definitions.
*
* @return must not be {@literal null}.
* @since 3.0
*/
String getModulePrefix();

/**
* Returns all {@link RepositoryConfiguration}s obtained through the given {@link RepositoryConfigurationSource}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ public abstract class RepositoryConfigurationExtensionSupport implements Reposit

private boolean noMultiStoreSupport = false;

@Override
public String getModuleName() {
return StringUtils.capitalize(getModulePrefix());
}

public <T extends RepositoryConfigurationSource> Collection<RepositoryConfiguration<T>> getRepositoryConfigurations(
T configSource, ResourceLoader loader) {
return getRepositoryConfigurations(configSource, loader, false);
Expand Down Expand Up @@ -109,13 +104,6 @@ public String getDefaultNamedQueryLocation() {
public void registerBeansForRoot(BeanDefinitionRegistry registry,
RepositoryConfigurationSource configurationSource) {}

/**
* Returns the prefix of the module to be used to create the default location for Spring Data named queries.
*
* @return must not be {@literal null}.
*/
protected abstract String getModulePrefix();

public void postProcess(BeanDefinitionBuilder builder, RepositoryConfigurationSource source) {}

public void postProcess(BeanDefinitionBuilder builder, AnnotationRepositoryConfigurationSource config) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
*
* @author Jens Schauder
* @author Oliver Gierke
* @author Christoph Strobl
*/
public class HidingClassLoader extends ShadowingClassLoader {

Expand Down Expand Up @@ -61,11 +62,21 @@ public static HidingClassLoader hide(Class<?>... packages) {
.collect(Collectors.toList()));
}

public static HidingClassLoader hideTypes(Class<?>... types) {

Assert.notNull(types, "Types must not be null!");

return new HidingClassLoader(Arrays.stream(types)//
.map(it -> it.getName())//
.collect(Collectors.toList()));
}

@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {

checkIfHidden(name);
return super.loadClass(name);
Class<?> loaded = super.loadClass(name);
checkIfHidden(loaded);
return loaded;
}

@Override
Expand All @@ -76,13 +87,14 @@ protected boolean isEligibleForShadowing(String className) {
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {

checkIfHidden(name);
return super.findClass(name);
Class<?> loaded = super.findClass(name);
checkIfHidden(loaded);
return loaded;
}

private void checkIfHidden(String name) throws ClassNotFoundException {
private void checkIfHidden(Class<?> type) throws ClassNotFoundException {

if (hidden.stream().anyMatch(it -> name.startsWith(it))) {
if (hidden.stream().anyMatch(it -> type.getName().startsWith(it))) {
throw new ClassNotFoundException();
}
}
Expand Down
Loading