diff --git a/src/main/java/org/springframework/data/util/TypeInformation.java b/src/main/java/org/springframework/data/util/TypeInformation.java index de16747df..a158c4f53 100644 --- a/src/main/java/org/springframework/data/util/TypeInformation.java +++ b/src/main/java/org/springframework/data/util/TypeInformation.java @@ -262,4 +262,15 @@ public interface TypeInformation { * @return will never be {@literal null}. */ TypeInformation specialize(ClassTypeInformation type); + + /** + * Returns whether the current type is a sub type of the given one, i.e. whether it's assignable but not the same one. + * + * @param type must not be {@literal null}. + * @return + * @since 2.2 + */ + default boolean isSubTypeOf(Class type) { + return !type.equals(getType()) && type.isAssignableFrom(getType()); + } } diff --git a/src/test/java/org/springframework/data/util/TypeDiscovererUnitTests.java b/src/test/java/org/springframework/data/util/TypeDiscovererUnitTests.java index 6196cbec2..1feb998c4 100755 --- a/src/test/java/org/springframework/data/util/TypeDiscovererUnitTests.java +++ b/src/test/java/org/springframework/data/util/TypeDiscovererUnitTests.java @@ -27,6 +27,7 @@ import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Set; import org.junit.Test; import org.junit.runner.RunWith; @@ -168,6 +169,16 @@ public class TypeDiscovererUnitTests { assertThat(type.getRequiredProperty("streamable").isCollectionLike()).isTrue(); } + @Test // DATACMNS-1419 + public void detectsSubTypes() { + + ClassTypeInformation type = from(Set.class); + + assertThat(type.isSubTypeOf(Collection.class)).isTrue(); + assertThat(type.isSubTypeOf(Set.class)).isFalse(); + assertThat(type.isSubTypeOf(String.class)).isFalse(); + } + class Person { Addresses addresses;