Introduce ManagedTypes.
We now provide an abstraction to describe a collection of entity types that can be provided to the mapping context as improvement over `Set<Class>` for easier context setup. Original pull request: #2635. Closes #2634.
This commit is contained in:
committed by
Mark Paluch
parent
2a1a8f387f
commit
02bf260d6d
@@ -34,6 +34,7 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Jens Schauder
|
||||
* @author Oliver Gierke
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
public class HidingClassLoader extends ShadowingClassLoader {
|
||||
|
||||
@@ -61,11 +62,21 @@ public class HidingClassLoader extends ShadowingClassLoader {
|
||||
.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
|
||||
@@ -76,13 +87,14 @@ public class HidingClassLoader extends ShadowingClassLoader {
|
||||
@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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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 static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class ManagedTypesUnitTests {
|
||||
|
||||
@Mock Consumer<Class<?>> action;
|
||||
|
||||
@Test // GH-2634
|
||||
void emptyNeverCallsAction() {
|
||||
|
||||
ManagedTypes.empty().forEach(action);
|
||||
verify(action, never()).accept(any());
|
||||
}
|
||||
|
||||
@Test // GH-2634
|
||||
void supplierBasedManagedTypesAreEvaluatedLazily() {
|
||||
|
||||
Supplier<Iterable<Class<?>>> typesSupplier = spy(new Supplier<Iterable<Class<?>>>() {
|
||||
@Override
|
||||
public Iterable<Class<?>> get() {
|
||||
return Collections.singleton(Object.class);
|
||||
}
|
||||
});
|
||||
|
||||
ManagedTypes managedTypes = ManagedTypes.fromSupplier(typesSupplier);
|
||||
|
||||
managedTypes.forEach(action); // 1st invocation
|
||||
verify(action).accept(any());
|
||||
verify(typesSupplier).get();
|
||||
|
||||
managedTypes.forEach(action); // 2nd invocation
|
||||
verify(action, times(2)).accept(any());
|
||||
verify(typesSupplier, times(1)).get();
|
||||
}
|
||||
|
||||
@Test // GH-2634
|
||||
void toListOnEmptyReturnsEmptyList() {
|
||||
assertThat(ManagedTypes.empty().toList()).isEmpty();
|
||||
}
|
||||
|
||||
@Test // GH-2634
|
||||
void toListContainsEntriesInOrder() {
|
||||
assertThat(ManagedTypes.fromIterable(Arrays.asList(Object.class, List.class)).toList()).containsExactly(Object.class,
|
||||
List.class);
|
||||
}
|
||||
}
|
||||
@@ -187,7 +187,7 @@ class RepositoryBeanDefinitionRegistrarSupportUnitTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getModulePrefix() {
|
||||
public String getModulePrefix() {
|
||||
return "commons";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,6 @@ import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.junit.jupiter.MockitoSettings;
|
||||
import org.mockito.quality.Strictness;
|
||||
|
||||
import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
@@ -100,7 +100,7 @@ class RepositoryConfigurationExtensionSupportUnitTests {
|
||||
static class SampleRepositoryConfigurationExtension extends RepositoryConfigurationExtensionSupport {
|
||||
|
||||
@Override
|
||||
protected String getModulePrefix() {
|
||||
public String getModulePrefix() {
|
||||
return "core";
|
||||
}
|
||||
|
||||
@@ -123,7 +123,7 @@ class RepositoryConfigurationExtensionSupportUnitTests {
|
||||
static class NonIdentifyingConfigurationExtension extends RepositoryConfigurationExtensionSupport {
|
||||
|
||||
@Override
|
||||
protected String getModulePrefix() {
|
||||
public String getModulePrefix() {
|
||||
return "non-identifying";
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user