<commit_before>package pw.kmp.kodeinject

import com.github.salomonbrys.kodein.Kodein
import com.github.salomonbrys.kodein.TT
import com.github.salomonbrys.kodein.TypeToken
import com.github.salomonbrys.kodein.bindings.NoArgBinding
import com.github.salomonbrys.kodein.bindings.NoArgBindingKodein
import com.github.salomonbrys.kodein.description
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.jvm.javaType

/**
 * Provides a factory that performs dependency injection into constructors.
 */
open class InjectionProvider<T : Any>(val type: KClass<T>, final override val createdType: TypeToken<T>) : NoArgBinding<T> {

    override fun getInstance(kodein: NoArgBindingKodein, key: Kodein.Key<Unit, T>): T {
        val constructor = findConstructor(kodein)
            ?: throw IllegalArgumentException("No constructors of ${type.simpleName} match available bindings.\nRegistered in Kodein:\n" + kodein.container.bindings.description)
        val parameters = provideDependencies(constructor, kodein)
        return constructor.call(*parameters)
    }

    internal fun findConstructor(kodein: NoArgBindingKodein): KFunction<T>? {
        return type.constructors.filter { it.parameters.all { p -> kodein.ProviderOrNull(TT(p.type.javaType as Class<*>)) != null } }.firstOrNull()<commit_msg>Don't assume that all bindings are classes.<commit_after>package pw.kmp.kodeinject

import com.github.salomonbrys.kodein.Kodein
import com.github.salomonbrys.kodein.TT
import com.github.salomonbrys.kodein.TypeToken
import com.github.salomonbrys.kodein.bindings.NoArgBinding
import com.github.salomonbrys.kodein.bindings.NoArgBindingKodein
import com.github.salomonbrys.kodein.description
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.jvm.javaType

/**
 * Provides a factory that performs dependency injection into constructors.
 */
open class InjectionProvider<T : Any>(val type: KClass<T>, final override val createdType: TypeToken<T>) : NoArgBinding<T> {

    override fun getInstance(kodein: NoArgBindingKodein, key: Kodein.Key<Unit, T>): T {
        val constructor = findConstructor(kodein)
            ?: throw IllegalArgumentException("No constructors of ${type.simpleName} match available bindings.\nRegistered in Kodein:\n" + kodein.container.bindings.description)
        val parameters = provideDependencies(constructor, kodein)
        return constructor.call(*parameters)
    }

    internal fun findConstructor(kodein: NoArgBindingKodein): KFunction<T>? {
        return type.constructors.filter { it.parameters.all { p -> p.type.javaType is Class<*> && kodein.ProviderOrNull(TT(p.type.javaType as Class<*>)) != null } }.firstOrNull()
{'fraction_non_alphanumeric': 0.10299003322259136, 'fraction_numerical': 0.0, 'mean_word_length': 5.022222222222222, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '823329', 'n_tokens_mistral': 845, 'n_tokens_neox': 834, 'n_words': 174}