Polishing.

Remove isProxyTypeInformation from TypeInformation. Use computeIfAbsent to fall back to user type information when attempting to add a entity to the MappingContext.

See #2485
Original pull request: #2486.
This commit is contained in:
Mark Paluch
2021-10-27 09:19:11 +02:00
parent c30f3fc8af
commit ac9d11638c
3 changed files with 16 additions and 150 deletions

View File

@@ -354,23 +354,12 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
read.lock();
Optional<E> persistentEntity = persistentEntities.get(typeInformation);
Optional<E> persistentEntity = persistentEntities.computeIfAbsent(typeInformation,
it -> persistentEntities.get(typeInformation.getUserTypeInformation()));
if (persistentEntity != null) {
return persistentEntity;
}
if(typeInformation.isProxyTypeInformation()) {
TypeInformation<?> userTypeInformation = typeInformation.getUserTypeInformation();
persistentEntity = persistentEntities.get(userTypeInformation);
if (persistentEntity != null) {
persistentEntities.put(typeInformation, persistentEntity);
return persistentEntity;
}
}
} finally {
read.unlock();
}
@@ -380,11 +369,13 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
try {
write.lock();
TypeInformation<?> typeInformationToUse = typeInformation.getUserTypeInformation();
entity = doAddPersistentEntity(typeInformation.getUserTypeInformation());
if(typeInformation.isProxyTypeInformation()) {
if (!typeInformationToUse.equals(typeInformation)) {
persistentEntities.put(typeInformation, Optional.of(entity));
}
} catch (BeansException e) {
throw new MappingException(e.getMessage(), e);
} finally {

View File

@@ -162,16 +162,6 @@ public interface TypeInformation<S> {
return userType.equals(getType()) ? this : ClassTypeInformation.from(userType);
}
/**
* Returns if {@link #getType()} refers to a proxy or user class.
*
* @return true if type is a proxy.
* @since 2.6
*/
default boolean isProxyTypeInformation() {
return !this.equals(getUserTypeInformation());
}
/**
* Returns a {@link ClassTypeInformation} to represent the {@link TypeInformation} of the raw type of the current
* instance.

View File

@@ -37,14 +37,11 @@ import java.util.Optional;
import java.util.TreeMap;
import java.util.function.Supplier;
import org.aopalliance.aop.Advice;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.aop.Advisor;
import org.springframework.aop.SpringProxy;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.framework.AopConfigException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.data.annotation.Id;
@@ -60,7 +57,6 @@ import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.StreamUtils;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
/**
* Unit test for {@link AbstractMappingContext}.
@@ -334,13 +330,15 @@ class AbstractMappingContextUnitTests {
void contextSeesUserTypeBeforeProxy() {
SampleMappingContext context = new SampleMappingContext();
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntity = context.getPersistentEntity(Base.class);
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntity = context
.getRequiredPersistentEntity(Base.class);
persistentEntity.getTypeInformation().getType().equals(Base.class);
assertThat(context.hasPersistentEntityFor(Base.class)).isTrue();
assertThat(context.hasPersistentEntityFor(Base$$SpringProxy$873fa2e.class)).isFalse();
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntityForProxy = context.getPersistentEntity(Base$$SpringProxy$873fa2e.class);
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntityForProxy = context
.getRequiredPersistentEntity(Base$$SpringProxy$873fa2e.class);
persistentEntityForProxy.getTypeInformation().getType().equals(Base.class);
assertThat(context.hasPersistentEntityFor(Base$$SpringProxy$873fa2e.class)).isTrue();
@@ -352,13 +350,15 @@ class AbstractMappingContextUnitTests {
SampleMappingContext context = new SampleMappingContext();
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntityForProxy = context.getPersistentEntity(Base$$SpringProxy$873fa2e.class);
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntityForProxy = context
.getRequiredPersistentEntity(Base$$SpringProxy$873fa2e.class);
persistentEntityForProxy.getTypeInformation().getType().equals(Base.class);
assertThat(context.hasPersistentEntityFor(Base$$SpringProxy$873fa2e.class)).isTrue();
assertThat(context.hasPersistentEntityFor(Base.class)).isTrue();
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntity = context.getPersistentEntity(Base.class);
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntity = context
.getRequiredPersistentEntity(Base.class);
persistentEntity.getTypeInformation().getType().equals(Base.class);
assertThat(context.getPersistentEntities()).hasSize(1); // only one distinct instance
@@ -546,123 +546,8 @@ class AbstractMappingContextUnitTests {
Map<MapKey, Integer> mapOfKeyToPerson;
}
static class Base$$SpringProxy$873fa2e extends Base implements SpringProxy, Advised {
static abstract class Base$$SpringProxy$873fa2e extends Base implements SpringProxy, Advised {
@Override
public boolean isFrozen() {
return false;
}
@Override
public boolean isProxyTargetClass() {
return false;
}
@Override
public Class<?>[] getProxiedInterfaces() {
return new Class[0];
}
@Override
public boolean isInterfaceProxied(Class<?> intf) {
return false;
}
@Override
public void setTargetSource(TargetSource targetSource) {
}
@Override
public TargetSource getTargetSource() {
return null;
}
@Override
public void setExposeProxy(boolean exposeProxy) {
}
@Override
public boolean isExposeProxy() {
return false;
}
@Override
public void setPreFiltered(boolean preFiltered) {
}
@Override
public boolean isPreFiltered() {
return false;
}
@Override
public Advisor[] getAdvisors() {
return new Advisor[0];
}
@Override
public void addAdvisor(Advisor advisor) throws AopConfigException {
}
@Override
public void addAdvisor(int pos, Advisor advisor) throws AopConfigException {
}
@Override
public boolean removeAdvisor(Advisor advisor) {
return false;
}
@Override
public void removeAdvisor(int index) throws AopConfigException {
}
@Override
public int indexOf(Advisor advisor) {
return 0;
}
@Override
public boolean replaceAdvisor(Advisor a, Advisor b) throws AopConfigException {
return false;
}
@Override
public void addAdvice(Advice advice) throws AopConfigException {
}
@Override
public void addAdvice(int pos, Advice advice) throws AopConfigException {
}
@Override
public boolean removeAdvice(Advice advice) {
return false;
}
@Override
public int indexOf(Advice advice) {
return 0;
}
@Override
public String toProxyConfigString() {
return null;
}
@Nullable
@Override
public Class<?> getTargetClass() {
return null;
}
}
}