All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.inforion.lab403.common.extensions.blackmagic.kt Maven / Gradle / Ivy

There is a newer version: 0.3.5
Show newest version
@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) }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy