diff --git a/build.gradle b/build.gradle index 0134720..7d8d339 100644 --- a/build.gradle +++ b/build.gradle @@ -36,7 +36,7 @@ kotlin { commonMain { dependencies { implementation kotlin('stdlib') - api "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" + api kotlin('reflect') api "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$kotlin_serialisation_runtime_version" } } diff --git a/src/commonMain/kotlin/com/insanusmokrassar/sdi/ModuleSerializer.kt b/src/commonMain/kotlin/com/insanusmokrassar/sdi/ModuleSerializer.kt index 50516de..267f021 100644 --- a/src/commonMain/kotlin/com/insanusmokrassar/sdi/ModuleSerializer.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/sdi/ModuleSerializer.kt @@ -1,11 +1,10 @@ package com.insanusmokrassar.sdi -import com.insanusmokrassar.sdi.utils.* +import com.insanusmokrassar.sdi.utils.createModuleBasedOnConfigRoot import kotlinx.serialization.* -import kotlinx.serialization.internal.* +import kotlinx.serialization.internal.HashMapSerializer +import kotlinx.serialization.internal.StringSerializer import kotlinx.serialization.json.* -import kotlinx.serialization.modules.* -import kotlin.reflect.KClass @ImplicitReflectionSerializer @Serializable(ModuleSerializer::class) diff --git a/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/DependencyResolver.kt b/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/DependencyResolver.kt index 488d650..5ccaf93 100644 --- a/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/DependencyResolver.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/DependencyResolver.kt @@ -2,7 +2,8 @@ package com.insanusmokrassar.sdi.utils import kotlinx.serialization.* import kotlinx.serialization.json.* -import kotlinx.serialization.modules.* +import kotlinx.serialization.modules.SerializerAlreadyRegisteredException +import kotlinx.serialization.modules.SerializersModuleBuilder import kotlin.reflect.KClass @ImplicitReflectionSerializer @@ -13,7 +14,7 @@ internal class DependencyResolver( private val dependencyGetter: (String) -> Any ) : KSerializer { private val originalSerializer: KSerializer = try { - resolveSerializerByKClass(kClass) + kClass.serializer() } catch (e: Exception) { PolymorphicSerializer(kClass) } diff --git a/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/JsonAdaptations.kt b/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/JsonAdaptations.kt index 7a065b4..2d57c3d 100644 --- a/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/JsonAdaptations.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/JsonAdaptations.kt @@ -1,8 +1,9 @@ package com.insanusmokrassar.sdi.utils -import kotlinx.serialization.* +import kotlinx.serialization.ImplicitReflectionSerializer import kotlinx.serialization.json.* -import kotlinx.serialization.modules.* +import kotlinx.serialization.modules.SerializerAlreadyRegisteredException +import kotlinx.serialization.modules.SerializersModule private typealias PackageOrOtherDependencyNamePair = Pair @@ -63,25 +64,26 @@ internal fun createModuleBasedOnConfigRoot(jsonObject: JsonObject): Json { key to packageName }.toMap() - return Json( - configuration = JsonConfiguration(useArrayPolymorphism = true), - context = SerializersModule { - keysToPackages.values.forEach { - val kclass = resolveKClassByPackageName(it) + val context = SerializersModule { + keysToPackages.values.forEach { + val kclass = resolveKClassByPackageName(it) - try { - DependencyResolver( - this, - kclass, - { jsonStringFormat } - ) { - caches.getValue(it).invoke() - } - } catch (e: SerializerAlreadyRegisteredException) { - // here we are thinking that already registered + try { + DependencyResolver( + this, + kclass, + { jsonStringFormat } + ) { + caches.getValue(it).invoke() } + } catch (e: SerializerAlreadyRegisteredException) { + // here we are thinking that already registered } } + } + return Json( + configuration = JsonConfiguration(useArrayPolymorphism = true), + context = context ).also { jsonStringFormat = it } diff --git a/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/TypesHelper.kt b/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/TypesHelper.kt index 1dccd38..8fe8c03 100644 --- a/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/TypesHelper.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/TypesHelper.kt @@ -1,24 +1,22 @@ package com.insanusmokrassar.sdi.utils -import kotlinx.serialization.ImplicitReflectionSerializer -import kotlinx.serialization.KSerializer +import kotlinx.serialization.* import kotlin.reflect.KClass -import kotlin.reflect.KType - -@ImplicitReflectionSerializer -expect fun resolveSerializerByPackageName(packageName: String): KSerializer<*> - -@ImplicitReflectionSerializer -expect fun resolveSerializerByKClass(kClass: KClass): KSerializer expect fun resolveKClassByPackageName(packageName: String): KClass<*> -expect val KClass<*>.currentSupertypes: List +@ImplicitReflectionSerializer +internal fun resolveSerializerByKClass(kClass: KClass): KSerializer = kClass.serializer() -val KClass<*>.allSubclasses: Set> +@ImplicitReflectionSerializer +internal fun resolveSerializerByPackageName(packageName: String): KSerializer<*> = resolveSerializerByKClass( + resolveKClassByPackageName(packageName) +) + +internal val KClass<*>.allSubclasses: Set> get() { val subclasses = mutableSetOf>() - val leftToVisit = currentSupertypes.mapNotNull { it.classifier as? KClass<*> }.toMutableList() + val leftToVisit = supertypes.mapNotNull { it.classifier as? KClass<*> }.toMutableList() while (leftToVisit.isNotEmpty()) { val top = leftToVisit.removeAt(0) if (subclasses.add(top)) { diff --git a/src/commonTest/kotlin/com/insanusmokrassar/sdi/ListTest.kt b/src/commonTest/kotlin/com/insanusmokrassar/sdi/ListTest.kt index c93983f..62abb6f 100644 --- a/src/commonTest/kotlin/com/insanusmokrassar/sdi/ListTest.kt +++ b/src/commonTest/kotlin/com/insanusmokrassar/sdi/ListTest.kt @@ -2,7 +2,8 @@ package com.insanusmokrassar.sdi import kotlinx.serialization.* import kotlinx.serialization.json.Json -import kotlin.test.* +import kotlin.test.Test +import kotlin.test.assertEquals interface List_ParentalAPI { val services: List diff --git a/src/commonTest/kotlin/com/insanusmokrassar/sdi/SimpleCustomObjectTest.kt b/src/commonTest/kotlin/com/insanusmokrassar/sdi/SimpleCustomObjectTest.kt index 6d76ace..23091e6 100644 --- a/src/commonTest/kotlin/com/insanusmokrassar/sdi/SimpleCustomObjectTest.kt +++ b/src/commonTest/kotlin/com/insanusmokrassar/sdi/SimpleCustomObjectTest.kt @@ -2,7 +2,8 @@ package com.insanusmokrassar.sdi import kotlinx.serialization.* import kotlinx.serialization.json.Json -import kotlin.test.* +import kotlin.test.Test +import kotlin.test.assertEquals interface SimpleCustomObject_ControllerAPI { fun showUp() diff --git a/src/commonTest/kotlin/com/insanusmokrassar/sdi/SimpleTest.kt b/src/commonTest/kotlin/com/insanusmokrassar/sdi/SimpleTest.kt index 7866ec0..a02fe3a 100644 --- a/src/commonTest/kotlin/com/insanusmokrassar/sdi/SimpleTest.kt +++ b/src/commonTest/kotlin/com/insanusmokrassar/sdi/SimpleTest.kt @@ -2,7 +2,8 @@ package com.insanusmokrassar.sdi import kotlinx.serialization.* import kotlinx.serialization.json.Json -import kotlin.test.* +import kotlin.test.Test +import kotlin.test.assertEquals interface Simple_ControllerAPI { fun showUp() diff --git a/src/jvmMain/kotlin/com/insanusmokrassar/sdi/utils/currentSupertypes.kt b/src/jvmMain/kotlin/com/insanusmokrassar/sdi/utils/currentSupertypes.kt deleted file mode 100644 index b31e1a0..0000000 --- a/src/jvmMain/kotlin/com/insanusmokrassar/sdi/utils/currentSupertypes.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.insanusmokrassar.sdi.utils - -import kotlin.reflect.KClass -import kotlin.reflect.KType - -actual val KClass<*>.currentSupertypes: List - get() = this.supertypes diff --git a/src/jvmMain/kotlin/com/insanusmokrassar/sdi/utils/resolveSerializerByPackageName.kt b/src/jvmMain/kotlin/com/insanusmokrassar/sdi/utils/resolveSerializerByPackageName.kt index 238a50e..0405c8e 100644 --- a/src/jvmMain/kotlin/com/insanusmokrassar/sdi/utils/resolveSerializerByPackageName.kt +++ b/src/jvmMain/kotlin/com/insanusmokrassar/sdi/utils/resolveSerializerByPackageName.kt @@ -1,12 +1,5 @@ package com.insanusmokrassar.sdi.utils -import kotlinx.serialization.* import kotlin.reflect.KClass -@ImplicitReflectionSerializer -actual fun resolveSerializerByPackageName(packageName: String): KSerializer<*> = Class.forName(packageName).kotlin.serializer() - -@ImplicitReflectionSerializer -actual fun resolveSerializerByKClass(kClass: KClass): KSerializer = kClass.serializer() - actual fun resolveKClassByPackageName(packageName: String): KClass<*> = Class.forName(packageName).kotlin