Consolidate names of methods that provide access to TypeInformation and Class on PersistentProperty.

We now name methods returning a TypeInformation<?> …TypeInformation() and ones that return Class<?> …Type(). In case of both flavors provided, overloads should move to the TypeInformation-based variant as the other one simply resolves ….getType() on the returned value.

Closes: #2408
Original Pull Request: #2410
This commit is contained in:
Oliver Drotbohm
2021-07-09 14:48:51 +02:00
committed by Christoph Strobl
parent 7e1c3454b3
commit e565c11dcc
6 changed files with 64 additions and 17 deletions
@@ -70,9 +70,21 @@ public interface PersistentProperty<P extends PersistentProperty<P>> {
* {@link Map}'s value type transparently.
*
* @return
* @deprecated since 2.6 for removal in 3.0. Use {@link #getPersistentEntityTypeInformation()} instead.
*/
@Deprecated
Iterable<? extends TypeInformation<?>> getPersistentEntityTypes();
/**
* Returns the {@link TypeInformation} if the property references a {@link PersistentEntity}. Will return
* {@literal null} in case it refers to a simple type. Will return {@link Collection}'s component type or the
* {@link Map}'s value type transparently.
*
* @return
* @since 2.6
*/
Iterable<? extends TypeInformation<?>> getPersistentEntityTypeInformation();
/**
* Returns the getter method to access the property value if available. Might return {@literal null} in case there is
* no getter method with a return type assignable to the actual property's type.
@@ -395,6 +407,19 @@ public interface PersistentProperty<P extends PersistentProperty<P>> {
@Nullable
Class<?> getAssociationTargetType();
/**
* Return the type the property refers to in case it's an association, i.e. {@link #isAssociation()} returns
* {@literal true}. That means, that implementations <em>must</em> return a non-{@literal null} value from this method
* in that case. We also recommend to return {@literal null} for non-associations right away to establish symmetry
* between this method and {@link #isAssociation()}.
*
* @return the type the property refers to in case it's an association, i.e. {@link #isAssociation()} returns
* {@literal true}.
* @since 2.6
*/
@Nullable
TypeInformation<?> getAssociationTargetTypeInformation();
/**
* Returns a {@link PersistentPropertyAccessor} for the current property's owning value.
*
@@ -33,7 +33,6 @@ import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
@@ -576,7 +575,7 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
return;
}
property.getPersistentEntityTypes().forEach(it -> {
property.getPersistentEntityTypeInformation().forEach(it -> {
if (shouldCreatePersistentEntityFor(it)) {
addPersistentEntity(it);
@@ -164,6 +164,15 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
*/
@Override
public Iterable<? extends TypeInformation<?>> getPersistentEntityTypes() {
return getPersistentEntityTypeInformation();
}
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.PersistentProperty#getPersistentEntityTypeInformation()
*/
@Override
public Iterable<? extends TypeInformation<?>> getPersistentEntityTypeInformation() {
if (isMap() || isCollectionLike()) {
return entityTypeInformation.get();
@@ -279,11 +288,21 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
@Override
public Class<?> getAssociationTargetType() {
TypeInformation<?> result = associationTargetType.getNullable();
TypeInformation<?> result = getAssociationTargetTypeInformation();
return result != null ? result.getType() : null;
}
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.PersistentProperty#getAssociationTargetTypeInformation()
*/
@Nullable
@Override
public TypeInformation<?> getAssociationTargetTypeInformation() {
return associationTargetType.getNullable();
}
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.PersistentProperty#isCollectionLike()
@@ -356,11 +375,7 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
*/
@Override
public Class<?> getActualType() {
TypeInformation<?> targetType = associationTargetType.getNullable();
TypeInformation<?> result = targetType == null ? information.getRequiredActualType() : targetType;
return result.getType();
return getActualTypeInformation().getType();
}
/*
@@ -375,6 +390,12 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
return this.property;
}
protected TypeInformation<?> getActualTypeInformation() {
TypeInformation<?> targetType = associationTargetType.getNullable();
return targetType == null ? information.getRequiredActualType() : targetType;
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
@@ -37,9 +37,11 @@ import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.Lazy;
import org.springframework.data.util.Optionals;
import org.springframework.data.util.StreamUtils;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -74,7 +76,7 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
&& (isAnnotationPresent(Reference.class) || super.isAssociation()));
private final Lazy<Boolean> isId = Lazy.of(() -> isAnnotationPresent(Id.class));
private final Lazy<Boolean> isVersion = Lazy.of(() -> isAnnotationPresent(Version.class));
private final Lazy<Class<?>> associationTargetType = Lazy.of(() -> {
private final Lazy<TypeInformation<?>> associationTargetType = Lazy.of(() -> {
if (!isAssociation()) {
return null;
@@ -83,8 +85,8 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
return Optional.of(Reference.class) //
.map(this::findAnnotation) //
.map(Reference::to) //
.map(it -> !Class.class.equals(it) ? it : getActualType()) //
.orElseGet(() -> super.getAssociationTargetType());
.map(it -> !Class.class.equals(it) ? ClassTypeInformation.from(it) : getActualTypeInformation()) //
.orElseGet(() -> super.getAssociationTargetTypeInformation());
});
/**
@@ -295,11 +297,11 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.PersistentProperty#getAssociationTargetType()
* @see org.springframework.data.mapping.model.AbstractPersistentProperty#getAssociationTargetTypeInformation()
*/
@Nullable
@Override
public Class<?> getAssociationTargetType() {
public TypeInformation<?> getAssociationTargetTypeInformation() {
return associationTargetType.getNullable();
}
@@ -113,7 +113,7 @@ class AbstractMappingContextIntegrationTests<T extends PersistentProperty<T>> {
when(prop.getTypeInformation()).thenReturn(owner.getTypeInformation());
when(prop.getName()).thenReturn(property.getName());
when(prop.getPersistentEntityTypes()).thenReturn(Collections.EMPTY_SET);
when(prop.getPersistentEntityTypeInformation()).thenReturn(Collections.EMPTY_SET);
try {
Thread.sleep(200);
@@ -73,7 +73,7 @@ public class AbstractPersistentPropertyUnitTests {
@Test // DATACMNS-101
void returnsNestedEntityTypeCorrectly() {
assertThat(getProperty(TestClassComplex.class, "testClassSet").getPersistentEntityTypes()).isEmpty();
assertThat(getProperty(TestClassComplex.class, "testClassSet").getPersistentEntityTypeInformation()).isEmpty();
}
@Test // DATACMNS-132
@@ -198,7 +198,7 @@ public class AbstractPersistentPropertyUnitTests {
void doesNotConsiderPropertyWithTreeMapMapValueAnEntity() {
SamplePersistentProperty property = getProperty(TreeMapWrapper.class, "map");
assertThat(property.getPersistentEntityTypes()).isEmpty();
assertThat(property.getPersistentEntityTypeInformation()).isEmpty();
assertThat(property.isEntity()).isFalse();
}
@@ -233,7 +233,7 @@ public class AbstractPersistentPropertyUnitTests {
assertThat(property.isAssociation()).isTrue();
assertThat(property.getAssociationTargetType()).isEqualTo(JMoleculesAggregate.class);
assertThat(property.getPersistentEntityTypes())
assertThat(property.getPersistentEntityTypeInformation())
.extracting(it -> it.getType())
.containsExactly((Class) JMoleculesAggregate.class);
}