diff --git a/build.gradle.kts b/build.gradle.kts index feb3af9..b9b731c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -30,7 +30,7 @@ repositories { dependencies { implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") implementation(kotlin("reflect")) - api("com.github.ProjectMapK:Shared:0.12") + api("com.github.ProjectMapK:Shared:0.13") // https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter testImplementation(group = "org.junit.jupiter", name = "junit-jupiter", version = "5.6.2") { diff --git a/src/main/kotlin/com/mapk/kmapper/ParameterUtils.kt b/src/main/kotlin/com/mapk/kmapper/ParameterUtils.kt index 9dd4d97..e7048d7 100644 --- a/src/main/kotlin/com/mapk/kmapper/ParameterUtils.kt +++ b/src/main/kotlin/com/mapk/kmapper/ParameterUtils.kt @@ -4,11 +4,12 @@ import com.mapk.annotations.KConverter import com.mapk.conversion.KConvertBy import com.mapk.core.KFunctionWithInstance import com.mapk.core.ValueParameter +import com.mapk.core.getAnnotatedFunctions +import com.mapk.core.getAnnotatedFunctionsFromCompanionObject +import com.mapk.core.getKClass import kotlin.reflect.KClass import kotlin.reflect.KFunction -import kotlin.reflect.full.companionObjectInstance import kotlin.reflect.full.findAnnotation -import kotlin.reflect.full.functions import kotlin.reflect.full.isSubclassOf import kotlin.reflect.full.primaryConstructor import kotlin.reflect.full.staticFunctions @@ -18,11 +19,11 @@ internal fun KClass.getConverters(): Set, KFunction< convertersFromConstructors(this) + convertersFromStaticMethods(this) + convertersFromCompanionObject(this) private fun Collection>.getConvertersFromFunctions(): Set, KFunction>> { - return filter { it.annotations.any { annotation -> annotation is KConverter } } + return this.getAnnotatedFunctions() .map { func -> func.isAccessible = true - (func.parameters.single().type.classifier as KClass<*>) to func + func.parameters.single().getKClass() to func }.toSet() } @@ -39,17 +40,12 @@ private fun convertersFromStaticMethods(clazz: KClass): Set convertersFromCompanionObject(clazz: KClass): Set, KFunction>> { - return clazz.companionObjectInstance?.let { companionObject -> - companionObject::class.functions - .filter { it.annotations.any { annotation -> annotation is KConverter } } - .map { function -> - val func: KFunction = KFunctionWithInstance( - function, - companionObject - ) as KFunction + return clazz.getAnnotatedFunctionsFromCompanionObject()?.let { (instance, functions) -> + functions.map { function -> + val func: KFunction = KFunctionWithInstance(function, instance) as KFunction - (func.parameters.single().type.classifier as KClass<*>) to func - }.toSet() + func.parameters.single().getKClass() to func + }.toSet() } ?: emptySet() }