mirror of
https://github.com/null2264/yokai.git
synced 2025-06-21 10:44:42 +00:00
fix: Mimic more classes
Completely fixed source search
This commit is contained in:
parent
83b6dca139
commit
1ee48c91d3
5 changed files with 81 additions and 36 deletions
|
@ -5,6 +5,6 @@ import uy.kohesive.injekt.Injekt
|
|||
class MigrationContext(val dryRun: Boolean) {
|
||||
|
||||
inline fun <reified T> get(): T? {
|
||||
return Injekt.getInstanceOrNull(T::class)
|
||||
return Injekt.getInstanceOrNull(T::class.java)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,25 +1,9 @@
|
|||
package uy.kohesive.injekt
|
||||
|
||||
import org.koin.core.parameter.ParametersDefinition
|
||||
import org.koin.core.qualifier.Qualifier
|
||||
import org.koin.mp.KoinPlatformTools
|
||||
import uy.kohesive.injekt.api.InjektScope
|
||||
import uy.kohesive.injekt.api.fullType
|
||||
|
||||
@Volatile var Injekt: InjektScope = InjektScope()
|
||||
|
||||
inline fun <reified T : Any> getKoinInstance(
|
||||
qualifier: Qualifier? = null,
|
||||
noinline parameters: ParametersDefinition? = null,
|
||||
): T = KoinPlatformTools.defaultContext().get().get<T>(qualifier, parameters)
|
||||
|
||||
inline fun <reified T : Any> getKoinInstanceOrNull(
|
||||
qualifier: Qualifier? = null,
|
||||
noinline parameters: ParametersDefinition? = null,
|
||||
): T? = KoinPlatformTools.defaultContext().getOrNull()?.getOrNull<T>(qualifier, parameters)
|
||||
|
||||
@Suppress("unused")
|
||||
inline fun <reified T : Any> injectLazy(
|
||||
qualifier: Qualifier? = null,
|
||||
mode: LazyThreadSafetyMode = KoinPlatformTools.defaultLazyMode(),
|
||||
noinline parameters: ParametersDefinition? = null,
|
||||
): Lazy<T> = lazy(mode) { getKoinInstance<T>(qualifier, parameters) }
|
||||
inline fun <reified T : Any> injectLazy(): Lazy<T> = lazy { Injekt.getInstance(fullType<T>().type) }
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package uy.kohesive.injekt.api
|
||||
|
||||
import java.lang.reflect.Type
|
||||
import org.koin.core.parameter.ParametersDefinition
|
||||
import org.koin.core.qualifier.Qualifier
|
||||
|
||||
interface InjektFactory {
|
||||
fun <R: Any> getInstance(forType: Type): R
|
||||
/*
|
||||
fun <R: Any> getInstanceOrElse(forType: Type, default: R): R
|
||||
fun <R: Any> getInstanceOrElse(forType: Type, default: ()->R): R
|
||||
*/
|
||||
fun <R: Any> getInstanceOrNull(forType: Type): R?
|
||||
|
||||
/*
|
||||
fun <R: Any, K: Any> getKeyedInstance(forType: Type, key: K): R
|
||||
fun <R: Any, K: Any> getKeyedInstanceOrElse(forType: Type, key: K, default: R): R
|
||||
fun <R: Any, K: Any> getKeyedInstanceOrElse(forType: Type, key: K, default: ()->R): R
|
||||
fun <R: Any, K: Any> getKeyedInstanceOrNull(forType: Type, key: K): R?
|
||||
*/
|
||||
}
|
||||
|
||||
@Suppress("unused")
|
||||
inline fun <reified T : Any> InjektFactory.get(
|
||||
qualifier: Qualifier? = null,
|
||||
noinline parameters: ParametersDefinition? = null,
|
||||
): T = getInstance(fullType<T>().type)
|
|
@ -1,23 +1,13 @@
|
|||
package uy.kohesive.injekt.api
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
import org.koin.core.parameter.ParametersDefinition
|
||||
import org.koin.core.qualifier.Qualifier
|
||||
import java.lang.reflect.Type
|
||||
import org.koin.mp.KoinPlatformTools
|
||||
import uy.kohesive.injekt.getKoinInstance
|
||||
|
||||
class InjektScope {
|
||||
class InjektScope : InjektFactory {
|
||||
|
||||
@Suppress("unused")
|
||||
fun <T> getInstanceOrNull(
|
||||
clazz: KClass<*>,
|
||||
qualifier: Qualifier? = null,
|
||||
parameters: ParametersDefinition? = null,
|
||||
): T? = KoinPlatformTools.defaultContext().getOrNull()?.getOrNull(clazz, qualifier, parameters)
|
||||
override fun <R : Any> getInstance(forType: Type): R =
|
||||
KoinPlatformTools.defaultContext().get().get(forType.kotlinClass)
|
||||
|
||||
override fun <R : Any> getInstanceOrNull(forType: Type): R? =
|
||||
KoinPlatformTools.defaultContext().getOrNull()?.getOrNull(forType.kotlinClass)
|
||||
}
|
||||
|
||||
@Suppress("unused", "UnusedReceiverParameter")
|
||||
inline fun <reified T : Any> InjektScope.get(
|
||||
qualifier: Qualifier? = null,
|
||||
noinline parameters: ParametersDefinition? = null,
|
||||
): T = getKoinInstance<T>(qualifier, parameters)
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
package uy.kohesive.injekt.api
|
||||
|
||||
import java.lang.reflect.GenericArrayType
|
||||
import java.lang.reflect.ParameterizedType
|
||||
import java.lang.reflect.Type
|
||||
import java.lang.reflect.TypeVariable
|
||||
import java.lang.reflect.WildcardType
|
||||
|
||||
@Suppress("UNCHECKED_CAST") fun Type.erasedType(): Class<Any> {
|
||||
return when (this) {
|
||||
is Class<*> -> this as Class<Any>
|
||||
is ParameterizedType -> this.rawType.erasedType()
|
||||
is GenericArrayType -> {
|
||||
val elementType = this.genericComponentType.erasedType()
|
||||
val testArray = java.lang.reflect.Array.newInstance(elementType, 0)
|
||||
testArray.javaClass
|
||||
}
|
||||
is TypeVariable<*> -> {
|
||||
throw IllegalStateException("Not sure what to do here yet")
|
||||
}
|
||||
is WildcardType -> {
|
||||
this.upperBounds[0].erasedType()
|
||||
}
|
||||
else -> throw IllegalStateException("Should not get here.")
|
||||
}
|
||||
}
|
||||
|
||||
val Type.kotlinClass get() = erasedType().kotlin
|
||||
|
||||
inline fun <reified T : Any> typeRef(): FullTypeReference<T> = object : FullTypeReference<T>() {}
|
||||
inline fun <reified T : Any> fullType(): FullTypeReference<T> = object : FullTypeReference<T>() {}
|
||||
|
||||
interface TypeReference<T> {
|
||||
val type: Type
|
||||
}
|
||||
|
||||
abstract class FullTypeReference<T> protected constructor() : TypeReference<T> {
|
||||
override val type: Type = javaClass.getGenericSuperclass().let { superClass ->
|
||||
if (superClass is Class<*>) {
|
||||
throw IllegalArgumentException("Internal error: TypeReference constructed without actual type information")
|
||||
}
|
||||
(superClass as ParameterizedType).actualTypeArguments[0]
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue