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

Commit fcac5f5

Browse files
committed
処理の共通化
1 parent 79500bc commit fcac5f5

File tree

1 file changed

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

1 file changed

+9
-10
lines changed

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import kotlin.reflect.full.functions
1515
import kotlin.reflect.full.isSuperclassOf
1616
import kotlin.reflect.full.memberProperties
1717
import kotlin.reflect.full.primaryConstructor
18-
import kotlin.reflect.jvm.isAccessible
1918
import kotlin.reflect.jvm.javaGetter
2019

2120
class KMapper<T : Any> private constructor(
@@ -41,21 +40,21 @@ class KMapper<T : Any> private constructor(
4140
if (parameterMap.isEmpty()) throw IllegalArgumentException("This function is not require arguments.")
4241
}
4342

44-
private fun KClass<*>.bindParameters(targetArray: Array<Any?>, instance: Any) {
45-
memberProperties.forEach { property ->
43+
private fun bindParameters(targetArray: Array<Any?>, src: Any) {
44+
src::class.memberProperties.forEach { property ->
4645
val javaGetter: Method? = property.javaGetter
4746
if (javaGetter != null && property.visibility == KVisibility.PUBLIC && property.annotations.none { annotation -> annotation is KPropertyIgnore }) {
4847
parameterMap[property.findAnnotation<KGetterAlias>()?.value ?: property.name]?.let {
4948
// javaGetterを呼び出す方が高速
5049
javaGetter.isAccessible = true
51-
targetArray[it.index] = javaGetter.invoke(instance)?.let { value -> mapObject(it, value) }
50+
targetArray[it.index] = javaGetter.invoke(src)?.let { value -> mapObject(it, value) }
5251
}
5352
}
5453
}
5554
}
5655

57-
private fun Map<*, *>.bindParameters(targetArray: Array<Any?>) {
58-
forEach { (key, value) ->
56+
private fun bindParameters(targetArray: Array<Any?>, src: Map<*, *>) {
57+
src.forEach { (key, value) ->
5958
parameterMap[key]?.let { param ->
6059
// 取得した内容がnullでなければ適切にmapする
6160
targetArray[param.index] = value?.let { mapObject(param, it) }
@@ -71,7 +70,7 @@ class KMapper<T : Any> private constructor(
7170

7271
fun map(srcMap: Map<String, Any?>): T {
7372
val array: Array<Any?> = function.argumentArray
74-
srcMap.bindParameters(array)
73+
bindParameters(array, srcMap)
7574
return function.call(array)
7675
}
7776

@@ -83,7 +82,7 @@ class KMapper<T : Any> private constructor(
8382

8483
fun map(src: Any): T {
8584
val array: Array<Any?> = function.argumentArray
86-
src::class.bindParameters(array, src)
85+
bindParameters(array, src)
8786
return function.call(array)
8887
}
8988

@@ -92,9 +91,9 @@ class KMapper<T : Any> private constructor(
9291

9392
listOf(*args).forEach { arg ->
9493
when (arg) {
95-
is Map<*, *> -> arg.bindParameters(array)
94+
is Map<*, *> -> bindParameters(array, arg)
9695
is Pair<*, *> -> bindParameters(array, arg)
97-
else -> arg::class.bindParameters(array, arg)
96+
else -> bindParameters(array, arg)
9897
}
9998
}
10099

0 commit comments

Comments
 (0)