From a600d8160c5017ade2436c71a6c93dd4b42be756 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 4 Aug 2020 12:16:55 +0200 Subject: [PATCH] DATACMNS-1781 - Move off deprecated Spring Framework API. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use KotlinDetector instead of ReflectionUtils.isKotlinClass(…). Use replacement MethodParameter(…).withContainingClass(…) instead of GenericTypeResolver.resolveParameterType(…). Use MergedAnnotations.from(…).isPresent(…) instead of AnnotationUtils.isAnnotationMetaPresent(…). --- .../springframework/data/mapping/PersistentEntity.java | 4 ++-- .../data/mapping/PersistentPropertyPathAccessor.java | 10 ---------- .../model/InstantiationAwarePropertyAccessor.java | 4 ++-- .../model/KotlinClassGeneratingEntityInstantiator.java | 3 ++- .../mapping/model/MappingInstantiationException.java | 4 ++-- .../mapping/model/PreferredConstructorDiscoverer.java | 3 ++- .../core/support/MethodInvocationValidator.java | 5 +++-- .../data/repository/core/support/MethodLookups.java | 7 ++++--- .../org/springframework/data/util/NullableUtils.java | 3 ++- 9 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/PersistentEntity.java b/src/main/java/org/springframework/data/mapping/PersistentEntity.java index 03d71e52f..b43bb0bcb 100644 --- a/src/main/java/org/springframework/data/mapping/PersistentEntity.java +++ b/src/main/java/org/springframework/data/mapping/PersistentEntity.java @@ -19,7 +19,6 @@ import java.lang.annotation.Annotation; import java.util.Iterator; import org.springframework.data.annotation.Immutable; -import org.springframework.data.convert.EntityInstantiator; import org.springframework.data.util.TypeInformation; import org.springframework.lang.Nullable; @@ -47,7 +46,8 @@ public interface PersistentEntity> extends It * * @return {@literal null} in case no suitable constructor for automatic construction can be found. This usually * indicates that the instantiation of the object of that persistent entity is done through either a customer - * {@link EntityInstantiator} or handled by custom conversion mechanisms entirely. + * {@link org.springframework.data.mapping.model.EntityInstantiator} or handled by custom conversion + * mechanisms entirely. */ @Nullable PreferredConstructor getPersistenceConstructor(); diff --git a/src/main/java/org/springframework/data/mapping/PersistentPropertyPathAccessor.java b/src/main/java/org/springframework/data/mapping/PersistentPropertyPathAccessor.java index 8839d1e8c..e9b0b16c3 100644 --- a/src/main/java/org/springframework/data/mapping/PersistentPropertyPathAccessor.java +++ b/src/main/java/org/springframework/data/mapping/PersistentPropertyPathAccessor.java @@ -55,16 +55,6 @@ public interface PersistentPropertyPathAccessor extends PersistentPropertyAcc @Nullable Object getProperty(PersistentPropertyPath> path, GetOptions context); - /** - * Sets the given value for the {@link PersistentProperty} pointed to by the given {@link PersistentPropertyPath}. The - * lookup of intermediate values must not yield {@literal null}. - * - * @param path must not be {@literal null} or empty. - * @param value can be {@literal null}. - * @see AccessOptions#DEFAULT - */ - void setProperty(PersistentPropertyPath> path, @Nullable Object value); - /** * Sets the given value for the {@link PersistentProperty} pointed to by the given {@link PersistentPropertyPath} * considering the given {@link AccessOptions}. diff --git a/src/main/java/org/springframework/data/mapping/model/InstantiationAwarePropertyAccessor.java b/src/main/java/org/springframework/data/mapping/model/InstantiationAwarePropertyAccessor.java index f29ee04a3..19aa41215 100644 --- a/src/main/java/org/springframework/data/mapping/model/InstantiationAwarePropertyAccessor.java +++ b/src/main/java/org/springframework/data/mapping/model/InstantiationAwarePropertyAccessor.java @@ -17,13 +17,13 @@ package org.springframework.data.mapping.model; import java.util.function.Function; +import org.springframework.core.KotlinDetector; import org.springframework.data.annotation.PersistenceConstructor; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentProperty; import org.springframework.data.mapping.PersistentPropertyAccessor; import org.springframework.data.mapping.PreferredConstructor; import org.springframework.data.mapping.PreferredConstructor.Parameter; -import org.springframework.data.util.ReflectionUtils; import org.springframework.lang.Nullable; import org.springframework.util.Assert; @@ -99,7 +99,7 @@ public class InstantiationAwarePropertyAccessor implements PersistentProperty PersistentEntity owner = property.getOwner(); PersistentPropertyAccessor delegate = delegateFunction.apply(this.bean); - if (!property.isImmutable() || property.getWither() != null || ReflectionUtils.isKotlinClass(owner.getType())) { + if (!property.isImmutable() || property.getWither() != null || KotlinDetector.isKotlinType(owner.getType())) { delegate.setProperty(property, value); this.bean = delegate.getBean(); diff --git a/src/main/java/org/springframework/data/mapping/model/KotlinClassGeneratingEntityInstantiator.java b/src/main/java/org/springframework/data/mapping/model/KotlinClassGeneratingEntityInstantiator.java index 4b93c9009..6d625a914 100644 --- a/src/main/java/org/springframework/data/mapping/model/KotlinClassGeneratingEntityInstantiator.java +++ b/src/main/java/org/springframework/data/mapping/model/KotlinClassGeneratingEntityInstantiator.java @@ -28,6 +28,7 @@ import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentProperty; import org.springframework.data.mapping.PreferredConstructor; import org.springframework.data.mapping.PreferredConstructor.Parameter; +import org.springframework.data.util.KotlinReflectionUtils; import org.springframework.data.util.ReflectionUtils; import org.springframework.lang.Nullable; @@ -50,7 +51,7 @@ class KotlinClassGeneratingEntityInstantiator extends ClassGeneratingEntityInsta PreferredConstructor constructor = entity.getPersistenceConstructor(); - if (ReflectionUtils.isSupportedKotlinClass(entity.getType()) && constructor != null) { + if (KotlinReflectionUtils.isSupportedKotlinClass(entity.getType()) && constructor != null) { PreferredConstructor defaultConstructor = new DefaultingKotlinConstructorResolver(entity) .getDefaultConstructor(); diff --git a/src/main/java/org/springframework/data/mapping/model/MappingInstantiationException.java b/src/main/java/org/springframework/data/mapping/model/MappingInstantiationException.java index 89639b38f..5470bf555 100644 --- a/src/main/java/org/springframework/data/mapping/model/MappingInstantiationException.java +++ b/src/main/java/org/springframework/data/mapping/model/MappingInstantiationException.java @@ -25,7 +25,7 @@ import java.util.Optional; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PreferredConstructor; -import org.springframework.data.util.ReflectionUtils; +import org.springframework.data.util.KotlinReflectionUtils; import org.springframework.lang.Nullable; import org.springframework.util.ObjectUtils; @@ -102,7 +102,7 @@ public class MappingInstantiationException extends RuntimeException { Constructor constructor = preferredConstructor.getConstructor(); - if (ReflectionUtils.isSupportedKotlinClass(constructor.getDeclaringClass())) { + if (KotlinReflectionUtils.isSupportedKotlinClass(constructor.getDeclaringClass())) { KFunction kotlinFunction = ReflectJvmMapping.getKotlinFunction(constructor); diff --git a/src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java b/src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java index 8d1ab7065..b788f9cd4 100644 --- a/src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java +++ b/src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java @@ -34,6 +34,7 @@ import org.springframework.data.mapping.PersistentProperty; import org.springframework.data.mapping.PreferredConstructor; import org.springframework.data.mapping.PreferredConstructor.Parameter; import org.springframework.data.util.ClassTypeInformation; +import org.springframework.data.util.KotlinReflectionUtils; import org.springframework.data.util.ReflectionUtils; import org.springframework.data.util.TypeInformation; import org.springframework.lang.Nullable; @@ -182,7 +183,7 @@ public interface PreferredConstructorDiscoverer type) { - return ReflectionUtils.isSupportedKotlinClass(type) ? KOTLIN : DEFAULT; + return KotlinReflectionUtils.isSupportedKotlinClass(type) ? KOTLIN : DEFAULT; } /** diff --git a/src/main/java/org/springframework/data/repository/core/support/MethodInvocationValidator.java b/src/main/java/org/springframework/data/repository/core/support/MethodInvocationValidator.java index b6de75bb8..5e91bb8de 100644 --- a/src/main/java/org/springframework/data/repository/core/support/MethodInvocationValidator.java +++ b/src/main/java/org/springframework/data/repository/core/support/MethodInvocationValidator.java @@ -26,6 +26,7 @@ import org.springframework.core.DefaultParameterNameDiscoverer; import org.springframework.core.MethodParameter; import org.springframework.core.ParameterNameDiscoverer; import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.data.util.KotlinReflectionUtils; import org.springframework.data.util.NullableUtils; import org.springframework.data.util.ReflectionUtils; import org.springframework.lang.Nullable; @@ -57,7 +58,7 @@ public class MethodInvocationValidator implements MethodInterceptor { */ public static boolean supports(Class repositoryInterface) { - return ReflectionUtils.isSupportedKotlinClass(repositoryInterface) + return KotlinReflectionUtils.isSupportedKotlinClass(repositoryInterface) || NullableUtils.isNonNull(repositoryInterface, ElementType.METHOD) || NullableUtils.isNonNull(repositoryInterface, ElementType.PARAMETER); } @@ -155,7 +156,7 @@ public class MethodInvocationValidator implements MethodInterceptor { private static boolean isNullableParameter(MethodParameter parameter) { return requiresNoValue(parameter) || NullableUtils.isExplicitNullable(parameter) - || (ReflectionUtils.isSupportedKotlinClass(parameter.getDeclaringClass()) + || (KotlinReflectionUtils.isSupportedKotlinClass(parameter.getDeclaringClass()) && ReflectionUtils.isNullable(parameter)); } diff --git a/src/main/java/org/springframework/data/repository/core/support/MethodLookups.java b/src/main/java/org/springframework/data/repository/core/support/MethodLookups.java index d126ce72c..5bbe6d01a 100644 --- a/src/main/java/org/springframework/data/repository/core/support/MethodLookups.java +++ b/src/main/java/org/springframework/data/repository/core/support/MethodLookups.java @@ -194,8 +194,8 @@ interface MethodLookups { Type genericType = genericTypes[i]; Class type = types[i]; - MethodParameter parameter = new MethodParameter(invokedMethod, i); - Class parameterType = resolveParameterType(parameter, repositoryInterface); + MethodParameter parameter = new MethodParameter(invokedMethod, i).withContainingClass(repositoryInterface); + Class parameterType = parameter.getParameterType(); if (genericType instanceof TypeVariable) { @@ -273,7 +273,8 @@ interface MethodLookups { return (parameterCriteria) -> { - Class parameterType = resolveParameterType(parameterCriteria.getDeclared(), repositoryInterface); + Class parameterType = parameterCriteria.getDeclared().withContainingClass(repositoryInterface) + .getParameterType(); Type genericType = parameterCriteria.getGenericBaseType(); if (genericType instanceof TypeVariable) { diff --git a/src/main/java/org/springframework/data/util/NullableUtils.java b/src/main/java/org/springframework/data/util/NullableUtils.java index f3b48f3e4..a6059dec5 100644 --- a/src/main/java/org/springframework/data/util/NullableUtils.java +++ b/src/main/java/org/springframework/data/util/NullableUtils.java @@ -34,6 +34,7 @@ import javax.annotation.Nonnull; import org.springframework.core.MethodParameter; import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.core.annotation.MergedAnnotations; import org.springframework.lang.NonNullApi; import org.springframework.lang.Nullable; import org.springframework.util.ClassUtils; @@ -159,7 +160,7 @@ public abstract class NullableUtils { return true; } - if (!AnnotationUtils.isAnnotationMetaPresent(annotation.annotationType(), annotationClass) + if (!MergedAnnotations.from(annotation.annotationType()).isPresent(annotationClass) || !isNonNull(annotation)) { return false; }