fix: CopyData for val

This commit is contained in:
server_main
2026-08-18 05:54:57 +00:00
parent 3c280c5353
commit 774c7f8cc2
@@ -7,6 +7,7 @@ import kotlin.reflect.full.createInstance
import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.full.memberProperties
import kotlin.reflect.jvm.isAccessible
import kotlin.reflect.jvm.javaField
interface DataCopy {
fun <T : Any> toAnotherObject(
@@ -19,15 +20,21 @@ interface DataCopy {
return try {
val vo = toClass.createInstance()
for (prop in toClass.declaredMemberProperties) {
@Suppress("UNCHECKED_CAST")
val mutableProp = prop as? KMutableProperty1<Any, Any?> ?: continue
val value = when {
sourceProps.containsKey(prop.name) -> sourceProps[prop.name]
otherProperties.containsKey(prop.name) -> otherProperties[prop.name]
else -> continue
}
mutableProp.isAccessible = true
mutableProp.set(vo, value)
@Suppress("UNCHECKED_CAST")
val mutableProp = prop as? KMutableProperty1<Any, Any?>
if (mutableProp != null) {
mutableProp.isAccessible = true
mutableProp.set(vo, value)
} else {
val field = prop.javaField ?: continue
field.isAccessible = true
field.set(vo, value)
}
}
vo
} catch (e: ReflectiveOperationException) {