diff --git a/src/main/java/org/springframework/data/mapping/PersistentProperty.java b/src/main/java/org/springframework/data/mapping/PersistentProperty.java index 907e993db..0c4560e82 100644 --- a/src/main/java/org/springframework/data/mapping/PersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/PersistentProperty.java @@ -20,7 +20,6 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Collection; import java.util.Map; -import java.util.Optional; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.data.util.TypeInformation; @@ -179,10 +178,10 @@ public interface PersistentProperty

> { * Returns the component type of the type if it is a {@link java.util.Collection}. Will return the type of the key if * the property is a {@link java.util.Map}. * - * @return the component type, the map's key type or {@link Optional#empty()} if neither {@link java.util.Collection} - * nor {@link java.util.Map}. + * @return the component type, the map's key type or {@literal null} if neither {@link java.util.Collection} nor + * {@link java.util.Map}. */ - Optional> getComponentType(); + Class getComponentType(); /** * Returns the raw type as it's pulled from from the reflected property. @@ -194,9 +193,9 @@ public interface PersistentProperty

> { /** * Returns the type of the values if the property is a {@link java.util.Map}. * - * @return the map's value type or {@link Optional#empty()} if no {@link java.util.Map} + * @return the map's value type or {@literal null} if no {@link java.util.Map} */ - Optional> getMapValueType(); + Class getMapValueType(); /** * Returns the actual type of the property. This will be the original property type if no generics were used, the diff --git a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java index 190d0c385..fd7c4bcb0 100644 --- a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java @@ -258,13 +258,8 @@ public abstract class AbstractPersistentProperty

* @see org.springframework.data.mapping.PersistentProperty#getComponentType() */ @Override - public Optional> getComponentType() { - - if (!isMap() && !isCollectionLike()) { - return Optional.empty(); - } - - return Optional.of(information.getRequiredComponentType().getType()); + public Class getComponentType() { + return isMap() || isCollectionLike() ? information.getRequiredComponentType().getType() : null; } /* @@ -272,8 +267,8 @@ public abstract class AbstractPersistentProperty

* @see org.springframework.data.mapping.PersistentProperty#getMapValueType() */ @Override - public Optional> getMapValueType() { - return isMap() ? information.getMapValueType().map(TypeInformation::getType) : Optional.empty(); + public Class getMapValueType() { + return isMap() ? information.getMapValueType().map(TypeInformation::getType).orElse(null) : null; } /* diff --git a/src/test/java/org/springframework/data/mapping/MappingMetadataTests.java b/src/test/java/org/springframework/data/mapping/MappingMetadataTests.java index 90a5ca7d4..a4e9ade0e 100755 --- a/src/test/java/org/springframework/data/mapping/MappingMetadataTests.java +++ b/src/test/java/org/springframework/data/mapping/MappingMetadataTests.java @@ -51,7 +51,7 @@ public class MappingMetadataTests { PersistentEntity person = ctx.getRequiredPersistentEntity(PersonWithChildren.class); person.doWithAssociations((AssociationHandler) association -> { - assertThat(association.getInverse().getComponentType()).hasValue(Child.class); + assertThat(association.getInverse().getComponentType()).isEqualTo(Child.class); }); } } diff --git a/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java b/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java index 88497faba..028ef07fc 100755 --- a/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java @@ -61,7 +61,7 @@ public class AbstractPersistentPropertyUnitTests { @Test // DATACMNS-68 public void discoversComponentTypeCorrectly() throws Exception { - assertThat(getProperty(TestClassComplex.class, "testClassSet").getComponentType()).hasValue(Object.class); + assertThat(getProperty(TestClassComplex.class, "testClassSet").getComponentType()).isEqualTo(Object.class); } @Test // DATACMNS-101