From b55f098b432e43e7b714a16f4c33480a37c79efa Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 6 Oct 2022 16:20:22 +0200 Subject: [PATCH] Remove references to `ClassTypeInformation` from `TypeInformation`. Closes #2703 --- .../MappingContextTypeInformationMapper.java | 7 ++- .../data/convert/ValueConversionContext.java | 5 +- .../data/mapping/PropertyPath.java | 1 - .../PersistentPropertyPathFactory.java | 1 - .../data/util/ClassTypeInformation.java | 24 ++++----- .../data/util/TypeDiscoverer.java | 27 ++++------ .../data/util/TypeInformation.java | 52 +++++-------------- .../AbstractMappingContextUnitTests.java | 6 +-- .../support/RepositoriesUnitTests.java | 9 ++-- 9 files changed, 43 insertions(+), 89 deletions(-) diff --git a/src/main/java/org/springframework/data/convert/MappingContextTypeInformationMapper.java b/src/main/java/org/springframework/data/convert/MappingContextTypeInformationMapper.java index abe944e45..1bb5c6885 100644 --- a/src/main/java/org/springframework/data/convert/MappingContextTypeInformationMapper.java +++ b/src/main/java/org/springframework/data/convert/MappingContextTypeInformationMapper.java @@ -22,7 +22,6 @@ import org.springframework.data.mapping.Alias; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentProperty; import org.springframework.data.mapping.context.MappingContext; -import org.springframework.data.util.ClassTypeInformation; import org.springframework.data.util.TypeInformation; import org.springframework.lang.Nullable; import org.springframework.util.Assert; @@ -38,7 +37,7 @@ import org.springframework.util.Assert; */ public class MappingContextTypeInformationMapper implements TypeInformationMapper { - private final Map, Alias> typeMap; + private final Map, Alias> typeMap; private final MappingContext, ?> mappingContext; /** @@ -79,7 +78,7 @@ public class MappingContextTypeInformationMapper implements TypeInformationMappe * @param key must not be {@literal null}. * @param alias can be {@literal null}. */ - private Alias verify(ClassTypeInformation key, Alias alias) { + private Alias verify(TypeInformation key, Alias alias) { // Reject second alias for same type @@ -113,7 +112,7 @@ public class MappingContextTypeInformationMapper implements TypeInformationMappe @Override public TypeInformation resolveTypeFrom(Alias alias) { - for (Map.Entry, Alias> entry : typeMap.entrySet()) { + for (Map.Entry, Alias> entry : typeMap.entrySet()) { if (entry.getValue().hasSamePresentValueAs(alias)) { return entry.getKey(); } diff --git a/src/main/java/org/springframework/data/convert/ValueConversionContext.java b/src/main/java/org/springframework/data/convert/ValueConversionContext.java index f073f1074..563693e57 100644 --- a/src/main/java/org/springframework/data/convert/ValueConversionContext.java +++ b/src/main/java/org/springframework/data/convert/ValueConversionContext.java @@ -16,7 +16,6 @@ package org.springframework.data.convert; import org.springframework.data.mapping.PersistentProperty; -import org.springframework.data.util.ClassTypeInformation; import org.springframework.data.util.TypeInformation; import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; @@ -65,7 +64,7 @@ public interface ValueConversionContext

> { * @return can be {@literal null}. * @throws IllegalStateException if value cannot be written as an instance of {@link Class type}. * @see #write(Object, TypeInformation) - * @see ClassTypeInformation + * @see TypeInformation */ @Nullable default T write(@Nullable Object value, @NonNull Class target) { @@ -115,7 +114,7 @@ public interface ValueConversionContext

> { * @return can be {@literal null}. * @throws IllegalStateException if value cannot be read as an instance of {@link Class type}. * @see #read(Object, TypeInformation) - * @see ClassTypeInformation + * @see TypeInformation */ @Nullable default T read(@Nullable Object value, @NonNull Class target) { diff --git a/src/main/java/org/springframework/data/mapping/PropertyPath.java b/src/main/java/org/springframework/data/mapping/PropertyPath.java index 51185d114..615c804ea 100644 --- a/src/main/java/org/springframework/data/mapping/PropertyPath.java +++ b/src/main/java/org/springframework/data/mapping/PropertyPath.java @@ -25,7 +25,6 @@ import java.util.Stack; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.springframework.data.util.ClassTypeInformation; import org.springframework.data.util.Streamable; import org.springframework.data.util.TypeInformation; import org.springframework.lang.Nullable; diff --git a/src/main/java/org/springframework/data/mapping/context/PersistentPropertyPathFactory.java b/src/main/java/org/springframework/data/mapping/context/PersistentPropertyPathFactory.java index 70538cf3d..21c3b14f4 100644 --- a/src/main/java/org/springframework/data/mapping/context/PersistentPropertyPathFactory.java +++ b/src/main/java/org/springframework/data/mapping/context/PersistentPropertyPathFactory.java @@ -28,7 +28,6 @@ import org.springframework.data.mapping.PersistentPropertyPath; import org.springframework.data.mapping.PersistentPropertyPaths; import org.springframework.data.mapping.PropertyHandler; import org.springframework.data.mapping.PropertyPath; -import org.springframework.data.util.ClassTypeInformation; import org.springframework.data.util.Pair; import org.springframework.data.util.StreamUtils; import org.springframework.data.util.TypeInformation; diff --git a/src/main/java/org/springframework/data/util/ClassTypeInformation.java b/src/main/java/org/springframework/data/util/ClassTypeInformation.java index 2dd5902f1..be55c41c1 100644 --- a/src/main/java/org/springframework/data/util/ClassTypeInformation.java +++ b/src/main/java/org/springframework/data/util/ClassTypeInformation.java @@ -33,18 +33,18 @@ import org.springframework.util.ConcurrentLruCache; * @author Christoph Strobl * @deprecated since 3.0 to go package protected at some point. Refer to {@link TypeInformation} only. */ -@Deprecated +@Deprecated(since = "3.0", forRemoval = true) @SuppressWarnings({ "rawtypes", "unchecked" }) public class ClassTypeInformation extends TypeDiscoverer { private static final ConcurrentLruCache> cache = new ConcurrentLruCache<>(64, ClassTypeInformation::new); - @Deprecated public static final ClassTypeInformation COLLECTION; - @Deprecated public static final ClassTypeInformation LIST; - @Deprecated public static final ClassTypeInformation SET; - @Deprecated public static final ClassTypeInformation MAP; - @Deprecated public static final ClassTypeInformation OBJECT; + public static final ClassTypeInformation COLLECTION; + public static final ClassTypeInformation LIST; + public static final ClassTypeInformation SET; + public static final ClassTypeInformation MAP; + public static final ClassTypeInformation OBJECT; static { @@ -62,11 +62,6 @@ public class ClassTypeInformation extends TypeDiscoverer { this.type = (Class) type.resolve(Object.class); } - private ClassTypeInformation(Class type) { - super(ResolvableType.forClass(type)); - this.type = type; - } - /** * @param * @param type @@ -75,10 +70,10 @@ public class ClassTypeInformation extends TypeDiscoverer { */ @Deprecated public static ClassTypeInformation from(Class type) { - return cti(ResolvableType.forClass(type)); + return from(ResolvableType.forClass(type)); } - static ClassTypeInformation cti(ResolvableType type) { + static ClassTypeInformation from(ResolvableType type) { Assert.notNull(type, "Type must not be null"); @@ -104,8 +99,7 @@ public class ClassTypeInformation extends TypeDiscoverer { */ static TypeInformation fromReturnTypeOf(Method method, @Nullable Class actualType) { - var type = actualType == null - ? ResolvableType.forMethodReturnType(method) + var type = actualType == null ? ResolvableType.forMethodReturnType(method) : ResolvableType.forMethodReturnType(method, actualType); return TypeInformation.of(type); diff --git a/src/main/java/org/springframework/data/util/TypeDiscoverer.java b/src/main/java/org/springframework/data/util/TypeDiscoverer.java index 480d362e7..e7123c1d2 100644 --- a/src/main/java/org/springframework/data/util/TypeDiscoverer.java +++ b/src/main/java/org/springframework/data/util/TypeDiscoverer.java @@ -47,9 +47,7 @@ import org.springframework.util.ReflectionUtils; * @author Jürgen Diez * @author Alessandro Nistico * @author Johannes Englmeier - * @deprecated since 3.0 to go package protected at some point. Prefer to refer to {@link TypeInformation} instead. */ -@Deprecated class TypeDiscoverer implements TypeInformation { private static final ConcurrentLruCache> CACHE = new ConcurrentLruCache<>(64, @@ -124,8 +122,7 @@ class TypeDiscoverer implements TypeInformation { return type.isArray() // || Iterable.class.equals(type) // || Collection.class.isAssignableFrom(type) // - || Streamable.class.isAssignableFrom(type) - || CustomCollections.isCollection(type); + || Streamable.class.isAssignableFrom(type) || CustomCollections.isCollection(type); } @Nullable @@ -195,7 +192,7 @@ class TypeDiscoverer implements TypeInformation { } @Override - public ClassTypeInformation getRawTypeInformation() { + public TypeInformation getRawTypeInformation() { return new ClassTypeInformation<>(ResolvableType.forRawClass(resolvableType.toClass())); } @@ -257,13 +254,10 @@ class TypeDiscoverer implements TypeInformation { return TypeInformation.of(resolvableSuperType); } - var noGenericsResolvable = !Arrays.stream(resolvableSuperType.resolveGenerics()) - .filter(it -> it != null) - .findAny() + var noGenericsResolvable = !Arrays.stream(resolvableSuperType.resolveGenerics()).filter(it -> it != null).findAny() .isPresent(); - return noGenericsResolvable - ? new ClassTypeInformation<>(ResolvableType.forRawClass(superType)) + return noGenericsResolvable ? new ClassTypeInformation<>(ResolvableType.forRawClass(superType)) : TypeInformation.of(resolvableSuperType); } @@ -299,8 +293,7 @@ class TypeDiscoverer implements TypeInformation { } return Arrays.stream(resolvableType.getGenerics()) - .> map(it -> it.resolve(Object.class) == null ? null : TypeInformation.of(it)) - .toList(); + .> map(it -> it.resolve(Object.class) == null ? null : TypeInformation.of(it)).toList(); } @Override @@ -308,8 +301,8 @@ class TypeDiscoverer implements TypeInformation { public TypeInformation specialize(TypeInformation type) { if (this.getTypeArguments().size() == type.getTypeArguments().size()) { - return (TypeInformation) TypeInformation.of( - ResolvableType.forClassWithGenerics(type.getType(), this.resolvableType.getGenerics())); + return (TypeInformation) TypeInformation + .of(ResolvableType.forClassWithGenerics(type.getType(), this.resolvableType.getGenerics())); } return TypeInformation.of((Class) type.getType()); @@ -380,10 +373,8 @@ class TypeDiscoverer implements TypeInformation { var rawType = getType(); var field = ReflectionUtils.findField(rawType, fieldname); - return field != null - ? Optional.of(TypeInformation.of(ResolvableType.forField(field, resolvableType))) - : Optional.ofNullable(BeanUtils.getPropertyDescriptor(rawType, fieldname)) - .map(it -> from(it, rawType)) + return field != null ? Optional.of(TypeInformation.of(ResolvableType.forField(field, resolvableType))) + : Optional.ofNullable(BeanUtils.getPropertyDescriptor(rawType, fieldname)).map(it -> from(it, rawType)) .map(TypeInformation::of); } diff --git a/src/main/java/org/springframework/data/util/TypeInformation.java b/src/main/java/org/springframework/data/util/TypeInformation.java index ec638f9b0..1c8e61239 100644 --- a/src/main/java/org/springframework/data/util/TypeInformation.java +++ b/src/main/java/org/springframework/data/util/TypeInformation.java @@ -42,15 +42,11 @@ import org.springframework.util.Assert; @SuppressWarnings({ "deprecation", "rawtypes" }) public interface TypeInformation { - public static final TypeInformation COLLECTION = ClassTypeInformation.COLLECTION; - public static final TypeInformation LIST = ClassTypeInformation.LIST; - public static final TypeInformation SET = ClassTypeInformation.SET; - public static final TypeInformation MAP = ClassTypeInformation.MAP; - public static final TypeInformation OBJECT = ClassTypeInformation.OBJECT; - - static TypeInformation orObject(@Nullable ResolvableType type) { - return type == null ? ClassTypeInformation.OBJECT : of(type); - } + TypeInformation COLLECTION = ClassTypeInformation.COLLECTION; + TypeInformation LIST = ClassTypeInformation.LIST; + TypeInformation SET = ClassTypeInformation.SET; + TypeInformation MAP = ClassTypeInformation.MAP; + TypeInformation OBJECT = ClassTypeInformation.OBJECT; /** * Creates a new {@link TypeInformation} from the given {@link ResolvableType}. @@ -63,11 +59,8 @@ public interface TypeInformation { Assert.notNull(type, "Type must not be null"); - return type.hasGenerics() - || (type.isArray() && type.getComponentType().hasGenerics()) // - || (type.getType() instanceof TypeVariable) - ? TypeDiscoverer.td(type) - : ClassTypeInformation.cti(type); + return type.hasGenerics() || (type.isArray() && type.getComponentType().hasGenerics()) // + || (type.getType() instanceof TypeVariable) ? TypeDiscoverer.td(type) : ClassTypeInformation.from(type); } /** @@ -108,8 +101,7 @@ public interface TypeInformation { */ public static TypeInformation fromReturnTypeOf(Method method, @Nullable Class type) { - ResolvableType intermediate = type == null - ? ResolvableType.forMethodReturnType(method) + ResolvableType intermediate = type == null ? ResolvableType.forMethodReturnType(method) : ResolvableType.forMethodReturnType(method, type); return TypeInformation.of(intermediate); @@ -161,8 +153,7 @@ public interface TypeInformation { return typeInformation; } - throw new IllegalArgumentException( - String.format("Could not find required property %s on %s", property, getType())); + throw new IllegalArgumentException(String.format("Could not find required property %s on %s", property, getType())); } /** @@ -259,12 +250,11 @@ public interface TypeInformation { } /** - * Returns a {@link ClassTypeInformation} to represent the {@link TypeInformation} of the raw type of the current - * instance. + * Returns a {@link TypeInformation} to represent the {@link TypeInformation} of the raw type of the current instance. * * @return */ - ClassTypeInformation getRawTypeInformation(); + TypeInformation getRawTypeInformation(); /** * Transparently returns the {@link java.util.Map} value type if the type is a {@link java.util.Map}, returns the @@ -338,8 +328,7 @@ public interface TypeInformation { if (result == null) { throw new IllegalArgumentException(String.format( - "Can't retrieve super type information for %s; Does current type really implement the given one", - superType)); + "Can't retrieve super type information for %s; Does current type really implement the given one", superType)); } return result; @@ -362,21 +351,6 @@ public interface TypeInformation { */ List> getTypeArguments(); - /** - * Specializes the given (raw) {@link ClassTypeInformation} using the context of the current potentially parameterized - * type, basically turning the given raw type into a parameterized one. Will return the given type as is if no - * generics are involved. - * - * @param type must not be {@literal null}. - * @return will never be {@literal null}. - * @deprecated since 3.0. Use {@link #specialize(TypeInformation)} instead, i.e. switch the given parameter's type to - * {@link TypeInformation} in the first place. - */ - @Deprecated - default TypeInformation specialize(ClassTypeInformation type) { - return specialize((TypeInformation) type); - } - /** * Specializes the given (raw) {@link TypeInformation} using the context of the current potentially parameterized * type, basically turning the given raw type into a parameterized one. Will return the given type as is if no @@ -391,7 +365,7 @@ public interface TypeInformation { } /** - * Returns whether the current type is a sub type of the given one, i.e. whether it's assignable but not the same one. + * Returns whether the current type is a subtype of the given one, i.e. whether it's assignable but not the same one. * * @param type must not be {@literal null}. * @return diff --git a/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java b/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java index 0816481d5..b8af92233 100755 --- a/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java @@ -38,6 +38,7 @@ import java.util.function.Supplier; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; + import org.springframework.aop.SpringProxy; import org.springframework.aop.framework.Advised; import org.springframework.context.ApplicationContext; @@ -52,7 +53,6 @@ import org.springframework.data.mapping.ShadowingPropertyType; import org.springframework.data.mapping.ShadowingPropertyTypeWithCtor; import org.springframework.data.mapping.model.BasicPersistentEntity; 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; @@ -154,13 +154,13 @@ class AbstractMappingContextUnitTests { void exposesCopyOfPersistentEntitiesToAvoidConcurrentModificationException() { var context = new SampleMappingContext(); - context.getPersistentEntity(ClassTypeInformation.MAP); + context.getPersistentEntity(TypeInformation.MAP); var iterator = context.getPersistentEntities() .iterator(); while (iterator.hasNext()) { - context.getPersistentEntity(ClassTypeInformation.SET); + context.getPersistentEntity(TypeInformation.SET); iterator.next(); } } diff --git a/src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java b/src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java index 9741e2834..9c1a72d77 100755 --- a/src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java +++ b/src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java @@ -28,6 +28,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; + import org.springframework.aop.framework.ProxyFactory; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; @@ -47,7 +48,6 @@ import org.springframework.data.repository.core.support.DummyRepositoryFactoryBe import org.springframework.data.repository.core.support.DummyRepositoryInformation; import org.springframework.data.repository.core.support.RepositoryFactoryInformation; import org.springframework.data.repository.query.QueryMethod; -import org.springframework.data.util.ClassTypeInformation; import org.springframework.data.util.TypeInformation; import org.springframework.util.ClassUtils; @@ -139,8 +139,7 @@ class RepositoriesUnitTests { @Test // DATACMNS-794 void exposesRepositoryFactoryInformationForRepository() { - var information = new Repositories(context) - .getRepositoryInformation(PersonRepository.class); + var information = new Repositories(context).getRepositoryInformation(PersonRepository.class); assertThat(information) .hasValueSatisfying(it -> assertThat(it.getRepositoryInterface()).isEqualTo(PersonRepository.class)); @@ -304,8 +303,8 @@ class RepositoriesUnitTests { var domainType = super.getDomainType().getName().concat("Entity"); try { - this.domainType = ClassTypeInformation - .from(ClassUtils.forName(domainType, CustomRepositoryMetadata.class.getClassLoader())); + this.domainType = TypeInformation + .of(ClassUtils.forName(domainType, CustomRepositoryMetadata.class.getClassLoader())); } catch (Exception e) { throw new RuntimeException(e); }