Skip to content

Commit 786ebec

Browse files
committed
Limit BeanWrapper's KotlinCopyUtil to Kotlin Data classes.
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.
1 parent 830f2f7 commit 786ebec

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/main/java/org/springframework/data/mapping/model/BeanWrapper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
import java.util.List;
2626
import java.util.Map;
2727

28-
import org.springframework.core.KotlinDetector;
2928
import org.springframework.data.mapping.MappingException;
3029
import org.springframework.data.mapping.PersistentProperty;
3130
import org.springframework.data.mapping.PersistentPropertyAccessor;
31+
import org.springframework.data.util.KotlinReflectionUtils;
3232
import org.springframework.lang.Nullable;
3333
import org.springframework.util.Assert;
3434
import org.springframework.util.ConcurrentReferenceHashMap;
@@ -77,7 +77,7 @@ public void setProperty(PersistentProperty<?> property, @Nullable Object value)
7777
return;
7878
}
7979

80-
if (KotlinDetector.isKotlinType(property.getOwner().getType())) {
80+
if (KotlinReflectionUtils.isDataClass(property.getOwner().getType())) {
8181

8282
this.bean = (T) KotlinCopyUtil.setProperty(property, bean, value);
8383
return;

src/main/java/org/springframework/data/util/KotlinReflectionUtils.java

+16
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ public static boolean isSupportedKotlinClass(Class<?> type) {
6666
.anyMatch(it -> Integer.valueOf(KotlinClassHeaderKind.CLASS.id).equals(it));
6767
}
6868

69+
/**
70+
* Return {@literal true} if the specified class is a Kotlin data class.
71+
*
72+
* @return {@literal true} if {@code type} is a Kotlin data class.
73+
* @since 2.5.1
74+
*/
75+
public static boolean isDataClass(Class<?> type) {
76+
77+
if (!KotlinDetector.isKotlinType(type)) {
78+
return false;
79+
}
80+
81+
KClass<?> kotlinClass = JvmClassMappingKt.getKotlinClass(type);
82+
return kotlinClass.isData();
83+
}
84+
6985
/**
7086
* Returns a {@link KFunction} instance corresponding to the given Java {@link Method} instance, or {@code null} if
7187
* this method cannot be represented by a Kotlin function.

0 commit comments

Comments
 (0)