Created
May 30, 2024 14:20
-
-
Save asfdfdfd/09dcbdf2ec3fd2c6916f7be379714bbf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <reified T : Any> setPropertyWithReflection(target: T, propertyName: String, propertyValue: Any) { | |
val namespaceMemberProperty = T::class.memberProperties | |
.filter { it.visibility == KVisibility.PUBLIC } | |
.filterIsInstance<KMutableProperty<*>>() | |
.firstOrNull { it.name == propertyName } | |
if (namespaceMemberProperty == null) { | |
throw NullPointerException("Failed to find '$propertyName' at '$target'.") | |
} | |
namespaceMemberProperty.setter.call(target, propertyValue) | |
} | |
inline fun <reified T : Any> getFunctionWithName(functionName: String): KFunction<*>? { | |
return Substitution::class.declaredFunctions | |
.filter { it.visibility == KVisibility.PUBLIC } | |
.firstOrNull { it.name == functionName } | |
.apply { this?.isAccessible = true } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment