Skip to content

Commit

Permalink
Limit BeanWrapper's KotlinCopyUtil to Kotlin Data classes.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mp911de committed Apr 21, 2021
1 parent 40222d6 commit c94fe6d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
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;
Expand Down Expand Up @@ -77,7 +77,7 @@ public void setProperty(PersistentProperty<?> property, @Nullable Object value)
return;
}

if (KotlinDetector.isKotlinType(property.getOwner().getType())) {
if (KotlinReflectionUtils.isDataClass(property.getOwner().getType())) {

this.bean = (T) KotlinCopyUtil.setProperty(property, bean, value);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ public static boolean isSupportedKotlinClass(Class<?> type) {
.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.
Expand Down

0 comments on commit c94fe6d

Please sign in to comment.