org.ooverkommelig.jvmreflect.retrievabledefinitions.JvmReflectRetrievableDefinitions.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ooverkommelig-jvm-reflect Show documentation
Show all versions of ooverkommelig-jvm-reflect Show documentation
Manageable dependency injection for Kotlin.
package org.ooverkommelig.jvmreflect.retrievabledefinitions
import org.ooverkommelig.Definition
import org.ooverkommelig.DefinitionCriteria
import org.ooverkommelig.RetrievableDefinitions
import org.ooverkommelig.SubGraphDefinition
import kotlin.reflect.KProperty
import kotlin.reflect.full.isSubtypeOf
internal class JvmReflectRetrievableDefinitions(private val owner: SubGraphDefinition) : RetrievableDefinitions {
private val definitionProperties = mutableListOf()
override fun addDefinitionProperty(property: KProperty<*>, returnsSameObjectForAllRetrievals: Boolean) {
@Suppress("UNCHECKED_CAST")
definitionProperties += DefinitionProperty(property as KProperty>, returnsSameObjectForAllRetrievals)
}
@Suppress("ConvertCallChainIntoSequence")
override fun transitiveRetrievableDefinitions(criteria: DefinitionCriteria) =
definitionProperties.filter { candidateDefinitionProperty ->
candidateDefinitionProperty.type.isSubtypeOf(criteria.getType())
&& (!criteria.mustReturnSameObjectForAllRetrievals
|| candidateDefinitionProperty.returnsSameObjectForAllRetrievals)
}.map { (definitionProperty) ->
@Suppress("UNCHECKED_CAST")
definitionProperty.getter.call(owner) as Definition
}
}