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

Commit a2c4962

Browse files
committed
コンストラクタに対する呼び出しを追加
1 parent 9c7b86f commit a2c4962

File tree

1 file changed

+10
-1
lines changed
  • src/main/kotlin/com/wrongwrong/mapk/core

1 file changed

+10
-1
lines changed

src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.wrongwrong.mapk.core
22

3+
import com.wrongwrong.mapk.annotations.KConstructor
34
import com.wrongwrong.mapk.annotations.PropertyAlias
45
import com.wrongwrong.mapk.annotations.PropertyIgnore
56
import kotlin.reflect.KClass
@@ -9,6 +10,7 @@ import kotlin.reflect.KProperty1
910
import kotlin.reflect.KVisibility
1011
import kotlin.reflect.full.isSuperclassOf
1112
import kotlin.reflect.full.memberProperties
13+
import kotlin.reflect.full.primaryConstructor
1214

1315
class KMapper<T: Any>(private val function: KFunction<T>, propertyNameConverter: (String) -> String = { it }) {
1416
constructor(clazz: KClass<T>, propertyNameConverter: (String) -> String = { it }): this(
@@ -92,7 +94,14 @@ class KMapper<T: Any>(private val function: KFunction<T>, propertyNameConverter:
9294
}
9395

9496
private fun <T : Any> getTarget(clazz: KClass<T>): KFunction<T> {
95-
TODO()
97+
val constructors: List<KFunction<T>> = clazz.constructors
98+
.filter { it.annotations.any { annotation -> annotation is KConstructor } }
99+
100+
if (constructors.size == 1) return constructors.single()
101+
102+
if (constructors.isEmpty()) return clazz.primaryConstructor!!
103+
104+
throw IllegalArgumentException("Find multiple target.")
96105
}
97106

98107
private fun Collection<KProperty1<*, *>>.filterTargets(): Collection<KProperty1<*, *>> {

0 commit comments

Comments
 (0)