From 994b2ff5b3decbdf16909ebfa936af5273714f30 Mon Sep 17 00:00:00 2001 From: SOR <3272194354@qq.com> Date: Sat, 25 Jul 2026 14:35:25 +0800 Subject: [PATCH] refactor: more secure DataCopy.kt --- .../itstudio/itmanager/entity/DataCopy.kt | 59 +++++++------------ .../club/itstudio/itmanager/utils/Const.kt | 7 ++- 2 files changed, 25 insertions(+), 41 deletions(-) diff --git a/src/beckend/src/main/kotlin/club/itstudio/itmanager/entity/DataCopy.kt b/src/beckend/src/main/kotlin/club/itstudio/itmanager/entity/DataCopy.kt index cd379d8..8108fc1 100644 --- a/src/beckend/src/main/kotlin/club/itstudio/itmanager/entity/DataCopy.kt +++ b/src/beckend/src/main/kotlin/club/itstudio/itmanager/entity/DataCopy.kt @@ -1,56 +1,39 @@ package club.itstudio.itmanager.entity import kotlin.reflect.KClass -import kotlin.reflect.KProperty +import kotlin.reflect.KMutableProperty1 +import kotlin.reflect.KProperty1 import kotlin.reflect.full.createInstance import kotlin.reflect.full.declaredMemberProperties import kotlin.reflect.full.memberProperties -import kotlin.reflect.jvm.javaField +import kotlin.reflect.jvm.isAccessible interface DataCopy { - fun toAnotherObject( toClass: KClass, - otherProperties: Map + otherProperties: Map = emptyMap() ): T { - try { - val voProperties = toClass.declaredMemberProperties + @Suppress("UNCHECKED_CAST") + val sourceProps = this::class.memberProperties + .associate { it.name to (it as KProperty1).get(this) } + return try { val vo = toClass.createInstance() - for (voProperty in voProperties) - copyFieldData(voProperty, otherProperties, vo) - return vo + for (prop in toClass.declaredMemberProperties) { + @Suppress("UNCHECKED_CAST") + val mutableProp = prop as? KMutableProperty1 ?: 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) + } + vo } catch (e: ReflectiveOperationException) { throw RuntimeException( - "${toClass.simpleName} has no fields.", - e + "Failed to instantiate ${toClass.simpleName}. Does it have a no-arg constructor?", e ) } } - - private fun copyFieldData( - toProperty: KProperty<*>, - otherProperties: Map, - another: Any - ) { - try { - val thisProperty = this::class.memberProperties - .firstOrNull { it.name == toProperty.name } - - val value = - if (thisProperty != null) { - thisProperty.getter.call(this) - } else if (otherProperties.containsKey(toProperty.name)) - otherProperties[toProperty.name] - else - throw RuntimeException( - "Property ${toProperty.name} not found." - ) - - val field = toProperty.javaField!! - field.isAccessible = true - field.set(another, value) - } catch (e: Exception) { - e.printStackTrace() - } - } } \ No newline at end of file diff --git a/src/beckend/src/main/kotlin/club/itstudio/itmanager/utils/Const.kt b/src/beckend/src/main/kotlin/club/itstudio/itmanager/utils/Const.kt index e42f6cd..e461c5f 100644 --- a/src/beckend/src/main/kotlin/club/itstudio/itmanager/utils/Const.kt +++ b/src/beckend/src/main/kotlin/club/itstudio/itmanager/utils/Const.kt @@ -4,13 +4,14 @@ import reactor.core.publisher.Mono object Const { const val FLOW_LIMIT_ORDER = -101 - + const val JWT_BLACK_LIST = "jwt:blacklist:" const val VERIFY_EMAIL_LIMIT = "verify:email:limit:" const val VERIFY_EMAIL_DATA = "verify:email:data:" const val FLOW_LIMIT_COUNTER = "flow:counter:" const val FLOW_LIMIT_BLOCK = "flow:block:" - - val INTERNAL_ERROR_MONO = Mono.just("something went wrong. Please contact the administrator.") + + const val INTERNAL_ERROR_STR = "something went wrong. Please contact the administrator." + val INTERNAL_ERROR_MONO = Mono.just(INTERNAL_ERROR_STR) } \ No newline at end of file