Skip to content

Commit d2e2e0a

Browse files
committed
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. Fixes #2408.
1 parent b3b590d commit d2e2e0a

File tree

6 files changed

+64
-17
lines changed

6 files changed

+64
-17
lines changed

src/main/java/org/springframework/data/mapping/PersistentProperty.java

+25
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,21 @@ public interface PersistentProperty<P extends PersistentProperty<P>> {
7070
* {@link Map}'s value type transparently.
7171
*
7272
* @return
73+
* @deprecated since 2.6 for removal in 3.0. Use {@link #getPersistentEntityTypeInformation()} instead.
7374
*/
75+
@Deprecated
7476
Iterable<? extends TypeInformation<?>> getPersistentEntityTypes();
7577

78+
/**
79+
* Returns the {@link TypeInformation} if the property references a {@link PersistentEntity}. Will return
80+
* {@literal null} in case it refers to a simple type. Will return {@link Collection}'s component type or the
81+
* {@link Map}'s value type transparently.
82+
*
83+
* @return
84+
* @since 2.6
85+
*/
86+
Iterable<? extends TypeInformation<?>> getPersistentEntityTypeInformation();
87+
7688
/**
7789
* Returns the getter method to access the property value if available. Might return {@literal null} in case there is
7890
* no getter method with a return type assignable to the actual property's type.
@@ -395,6 +407,19 @@ default boolean hasActualTypeAnnotation(Class<? extends Annotation> annotationTy
395407
@Nullable
396408
Class<?> getAssociationTargetType();
397409

410+
/**
411+
* Return the type the property refers to in case it's an association, i.e. {@link #isAssociation()} returns
412+
* {@literal true}. That means, that implementations <em>must</em> return a non-{@literal null} value from this method
413+
* in that case. We also recommend to return {@literal null} for non-associations right away to establish symmetry
414+
* between this method and {@link #isAssociation()}.
415+
*
416+
* @return the type the property refers to in case it's an association, i.e. {@link #isAssociation()} returns
417+
* {@literal true}.
418+
* @since 2.6
419+
*/
420+
@Nullable
421+
TypeInformation<?> getAssociationTargetTypeInformation();
422+
398423
/**
399424
* Returns a {@link PersistentPropertyAccessor} for the current property's owning value.
400425
*

src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
import org.slf4j.Logger;
3535
import org.slf4j.LoggerFactory;
36-
3736
import org.springframework.beans.BeanUtils;
3837
import org.springframework.beans.BeansException;
3938
import org.springframework.beans.factory.InitializingBean;
@@ -576,7 +575,7 @@ private void createAndRegisterProperty(Property input) {
576575
return;
577576
}
578577

579-
property.getPersistentEntityTypes().forEach(it -> {
578+
property.getPersistentEntityTypeInformation().forEach(it -> {
580579

581580
if (shouldCreatePersistentEntityFor(it)) {
582581
addPersistentEntity(it);

src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java

+27-6
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ public TypeInformation<?> getTypeInformation() {
164164
*/
165165
@Override
166166
public Iterable<? extends TypeInformation<?>> getPersistentEntityTypes() {
167+
return getPersistentEntityTypeInformation();
168+
}
169+
170+
/*
171+
* (non-Javadoc)
172+
* @see org.springframework.data.mapping.PersistentProperty#getPersistentEntityTypeInformation()
173+
*/
174+
@Override
175+
public Iterable<? extends TypeInformation<?>> getPersistentEntityTypeInformation() {
167176

168177
if (isMap() || isCollectionLike()) {
169178
return entityTypeInformation.get();
@@ -279,11 +288,21 @@ public Association<P> getAssociation() {
279288
@Override
280289
public Class<?> getAssociationTargetType() {
281290

282-
TypeInformation<?> result = associationTargetType.getNullable();
291+
TypeInformation<?> result = getAssociationTargetTypeInformation();
283292

284293
return result != null ? result.getType() : null;
285294
}
286295

296+
/*
297+
* (non-Javadoc)
298+
* @see org.springframework.data.mapping.PersistentProperty#getAssociationTargetTypeInformation()
299+
*/
300+
@Nullable
301+
@Override
302+
public TypeInformation<?> getAssociationTargetTypeInformation() {
303+
return associationTargetType.getNullable();
304+
}
305+
287306
/*
288307
* (non-Javadoc)
289308
* @see org.springframework.data.mapping.PersistentProperty#isCollectionLike()
@@ -356,11 +375,7 @@ public Class<?> getMapValueType() {
356375
*/
357376
@Override
358377
public Class<?> getActualType() {
359-
360-
TypeInformation<?> targetType = associationTargetType.getNullable();
361-
TypeInformation<?> result = targetType == null ? information.getRequiredActualType() : targetType;
362-
363-
return result.getType();
378+
return getActualTypeInformation().getType();
364379
}
365380

366381
/*
@@ -375,6 +390,12 @@ protected Property getProperty() {
375390
return this.property;
376391
}
377392

393+
protected TypeInformation<?> getActualTypeInformation() {
394+
395+
TypeInformation<?> targetType = associationTargetType.getNullable();
396+
return targetType == null ? information.getRequiredActualType() : targetType;
397+
}
398+
378399
/*
379400
* (non-Javadoc)
380401
* @see java.lang.Object#equals(java.lang.Object)

src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@
3737
import org.springframework.data.mapping.MappingException;
3838
import org.springframework.data.mapping.PersistentEntity;
3939
import org.springframework.data.mapping.PersistentProperty;
40+
import org.springframework.data.util.ClassTypeInformation;
4041
import org.springframework.data.util.Lazy;
4142
import org.springframework.data.util.Optionals;
4243
import org.springframework.data.util.StreamUtils;
44+
import org.springframework.data.util.TypeInformation;
4345
import org.springframework.lang.Nullable;
4446
import org.springframework.util.Assert;
4547

@@ -74,7 +76,7 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
7476
&& (isAnnotationPresent(Reference.class) || super.isAssociation()));
7577
private final Lazy<Boolean> isId = Lazy.of(() -> isAnnotationPresent(Id.class));
7678
private final Lazy<Boolean> isVersion = Lazy.of(() -> isAnnotationPresent(Version.class));
77-
private final Lazy<Class<?>> associationTargetType = Lazy.of(() -> {
79+
private final Lazy<TypeInformation<?>> associationTargetType = Lazy.of(() -> {
7880

7981
if (!isAssociation()) {
8082
return null;
@@ -83,8 +85,8 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
8385
return Optional.of(Reference.class) //
8486
.map(this::findAnnotation) //
8587
.map(Reference::to) //
86-
.map(it -> !Class.class.equals(it) ? it : getActualType()) //
87-
.orElseGet(() -> super.getAssociationTargetType());
88+
.map(it -> !Class.class.equals(it) ? ClassTypeInformation.from(it) : getActualTypeInformation()) //
89+
.orElseGet(() -> super.getAssociationTargetTypeInformation());
8890
});
8991

9092
/**
@@ -295,11 +297,11 @@ public boolean usePropertyAccess() {
295297

296298
/*
297299
* (non-Javadoc)
298-
* @see org.springframework.data.mapping.PersistentProperty#getAssociationTargetType()
300+
* @see org.springframework.data.mapping.model.AbstractPersistentProperty#getAssociationTargetTypeInformation()
299301
*/
300302
@Nullable
301303
@Override
302-
public Class<?> getAssociationTargetType() {
304+
public TypeInformation<?> getAssociationTargetTypeInformation() {
303305
return associationTargetType.getNullable();
304306
}
305307

src/test/java/org/springframework/data/mapping/context/AbstractMappingContextIntegrationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ protected T createPersistentProperty(Property property, BasicPersistentEntity<Ob
113113

114114
when(prop.getTypeInformation()).thenReturn(owner.getTypeInformation());
115115
when(prop.getName()).thenReturn(property.getName());
116-
when(prop.getPersistentEntityTypes()).thenReturn(Collections.EMPTY_SET);
116+
when(prop.getPersistentEntityTypeInformation()).thenReturn(Collections.EMPTY_SET);
117117

118118
try {
119119
Thread.sleep(200);

src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void discoversComponentTypeCorrectly() {
7373

7474
@Test // DATACMNS-101
7575
void returnsNestedEntityTypeCorrectly() {
76-
assertThat(getProperty(TestClassComplex.class, "testClassSet").getPersistentEntityTypes()).isEmpty();
76+
assertThat(getProperty(TestClassComplex.class, "testClassSet").getPersistentEntityTypeInformation()).isEmpty();
7777
}
7878

7979
@Test // DATACMNS-132
@@ -198,7 +198,7 @@ void considersCollectionPropertySimpleIfComponentTypeIsSimple() {
198198
void doesNotConsiderPropertyWithTreeMapMapValueAnEntity() {
199199

200200
SamplePersistentProperty property = getProperty(TreeMapWrapper.class, "map");
201-
assertThat(property.getPersistentEntityTypes()).isEmpty();
201+
assertThat(property.getPersistentEntityTypeInformation()).isEmpty();
202202
assertThat(property.isEntity()).isFalse();
203203
}
204204

@@ -233,7 +233,7 @@ void detectsJMoleculesAssociation() {
233233

234234
assertThat(property.isAssociation()).isTrue();
235235
assertThat(property.getAssociationTargetType()).isEqualTo(JMoleculesAggregate.class);
236-
assertThat(property.getPersistentEntityTypes())
236+
assertThat(property.getPersistentEntityTypeInformation())
237237
.extracting(it -> it.getType())
238238
.containsExactly((Class) JMoleculesAggregate.class);
239239
}

0 commit comments

Comments
 (0)