Skip to content

Instantly share code, notes, and snippets.

@asfdfdfd
Created May 30, 2024 14:20
Show Gist options
  • Save asfdfdfd/09dcbdf2ec3fd2c6916f7be379714bbf to your computer and use it in GitHub Desktop.
Save asfdfdfd/09dcbdf2ec3fd2c6916f7be379714bbf to your computer and use it in GitHub Desktop.
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