Skip to content
This repository has been archived by the owner on Jan 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #32 from k163377/feature
Browse files Browse the repository at this point in the history
Response to commonization.
  • Loading branch information
k163377 authored May 16, 2020
2 parents c3139c9 + 9d5f188 commit 125d976
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
24 changes: 10 additions & 14 deletions src/main/kotlin/com/mapk/kmapper/ParameterUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,11 +19,11 @@ internal fun <T : Any> KClass<T>.getConverters(): Set<Pair<KClass<*>, KFunction<
convertersFromConstructors(this) + convertersFromStaticMethods(this) + convertersFromCompanionObject(this)

private fun <T> Collection<KFunction<T>>.getConvertersFromFunctions(): Set<Pair<KClass<*>, KFunction<T>>> {
return filter { it.annotations.any { annotation -> annotation is KConverter } }
return this.getAnnotatedFunctions<KConverter, T>()
.map { func ->
func.isAccessible = true

(func.parameters.single().type.classifier as KClass<*>) to func
func.parameters.single().getKClass() to func
}.toSet()
}

Expand All @@ -39,17 +40,12 @@ private fun <T : Any> convertersFromStaticMethods(clazz: KClass<T>): Set<Pair<KC

@Suppress("UNCHECKED_CAST")
private fun <T : Any> convertersFromCompanionObject(clazz: KClass<T>): Set<Pair<KClass<*>, KFunction<T>>> {
return clazz.companionObjectInstance?.let { companionObject ->
companionObject::class.functions
.filter { it.annotations.any { annotation -> annotation is KConverter } }
.map { function ->
val func: KFunction<T> = KFunctionWithInstance(
function,
companionObject
) as KFunction<T>
return clazz.getAnnotatedFunctionsFromCompanionObject<KConverter>()?.let { (instance, functions) ->
functions.map { function ->
val func: KFunction<T> = KFunctionWithInstance(function, instance) as KFunction<T>

(func.parameters.single().type.classifier as KClass<*>) to func
}.toSet()
func.parameters.single().getKClass() to func
}.toSet()
} ?: emptySet()
}

Expand Down

0 comments on commit 125d976

Please sign in to comment.