ru.inforion.lab403.common.extensions.blackmagic.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-extensions Show documentation
Show all versions of kotlin-extensions Show documentation
Kotlin extension methods and function for different Java libraries
@file:Suppress("UNCHECKED_CAST")
package ru.inforion.lab403.common.extensions
import kotlin.reflect.KClass
import kotlin.reflect.KProperty1
import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.jvm.javaField
data class DelegatedProperty(val property: KProperty1, val delegatingToInstance: D)
/**
* Warning: Don't use this function with non-final instances!
*/
inline fun findDelegatingPropertyInstances(
instance: T,
delegatingTo: KClass
): List> {
val klass = T::class
val properties = klass.declaredMemberProperties
return properties.filter {
val cls = delegatingTo.java
val field = it.javaField ?: return@filter false
val type = field.type
val isAssignableFrom = type.isAssignableFrom(cls)
isAssignableFrom
}.map { DelegatedProperty(it, it.get(instance) as D) }
}