From c94fe6d2e05b2bb421796e8fded9886f6042631a Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 19 Apr 2021 10:29:55 +0200 Subject: [PATCH] Limit BeanWrapper's KotlinCopyUtil to Kotlin Data classes. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, we tried to invoke the copy(…) method on all Kotlin classes whereas the copy(…) method is only specific to Kotlin data classes. Original pull request: #390. Closes #2358. --- .../data/mapping/model/BeanWrapper.java | 4 ++-- .../data/util/KotlinReflectionUtils.java | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/model/BeanWrapper.java b/src/main/java/org/springframework/data/mapping/model/BeanWrapper.java index 344063e7c..5f49c81e7 100644 --- a/src/main/java/org/springframework/data/mapping/model/BeanWrapper.java +++ b/src/main/java/org/springframework/data/mapping/model/BeanWrapper.java @@ -25,10 +25,10 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import org.springframework.core.KotlinDetector; import org.springframework.data.mapping.MappingException; import org.springframework.data.mapping.PersistentProperty; import org.springframework.data.mapping.PersistentPropertyAccessor; +import org.springframework.data.util.KotlinReflectionUtils; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ConcurrentReferenceHashMap; @@ -77,7 +77,7 @@ class BeanWrapper implements PersistentPropertyAccessor { return; } - if (KotlinDetector.isKotlinType(property.getOwner().getType())) { + if (KotlinReflectionUtils.isDataClass(property.getOwner().getType())) { this.bean = (T) KotlinCopyUtil.setProperty(property, bean, value); return; diff --git a/src/main/java/org/springframework/data/util/KotlinReflectionUtils.java b/src/main/java/org/springframework/data/util/KotlinReflectionUtils.java index 5bc0b2a6a..f4c6c15c5 100644 --- a/src/main/java/org/springframework/data/util/KotlinReflectionUtils.java +++ b/src/main/java/org/springframework/data/util/KotlinReflectionUtils.java @@ -66,6 +66,22 @@ public final class KotlinReflectionUtils { .anyMatch(it -> Integer.valueOf(KotlinClassHeaderKind.CLASS.id).equals(it)); } + /** + * Return {@literal true} if the specified class is a Kotlin data class. + * + * @return {@literal true} if {@code type} is a Kotlin data class. + * @since 2.5.1 + */ + public static boolean isDataClass(Class type) { + + if (!KotlinDetector.isKotlinType(type)) { + return false; + } + + KClass kotlinClass = JvmClassMappingKt.getKotlinClass(type); + return kotlinClass.isData(); + } + /** * Returns a {@link KFunction} instance corresponding to the given Java {@link Method} instance, or {@code null} if * this method cannot be represented by a Kotlin function.