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

Commit 6d79fb3

Browse files
committed
処理の共通化
1 parent 0c81260 commit 6d79fb3

File tree

1 file changed

+15
-22
lines changed
  • src/main/kotlin/com/wrongwrong/mapk/core

1 file changed

+15
-22
lines changed

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

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ class KMapper<T : Any> private constructor(
4242
if (parameterMap.isEmpty()) throw IllegalArgumentException("This function is not require arguments.")
4343
}
4444

45+
private fun KClass<*>.bindParameters(targetArray: Array<Any?>, instance: Any) {
46+
memberProperties.forEach { property ->
47+
val javaGetter: Method? = property.javaGetter
48+
if (javaGetter != null && property.visibility == KVisibility.PUBLIC && property.annotations.none { annotation -> annotation is KPropertyIgnore }) {
49+
parameterMap[property.findAnnotation<KGetterAlias>()?.value ?: property.name]?.let {
50+
// javaGetterを呼び出す方が高速
51+
javaGetter.isAccessible = true
52+
targetArray[it.index] = javaGetter.invoke(instance)?.let { value -> mapObject(it, value) }
53+
}
54+
}
55+
}
56+
}
57+
4558
fun map(srcMap: Map<String, Any?>): T {
4659
val array: Array<Any?> = function.argumentArray
4760

@@ -63,18 +76,7 @@ class KMapper<T : Any> private constructor(
6376

6477
fun map(src: Any): T {
6578
val array: Array<Any?> = function.argumentArray
66-
67-
src::class.memberProperties.forEach { property ->
68-
val javaGetter: Method? = property.javaGetter
69-
if (javaGetter != null && property.visibility == KVisibility.PUBLIC && property.annotations.none { annotation -> annotation is KPropertyIgnore }) {
70-
parameterMap[property.findAnnotation<KGetterAlias>()?.value ?: property.name]?.let {
71-
// javaGetterを呼び出す方が高速
72-
javaGetter.isAccessible = true
73-
array[it.index] = javaGetter.invoke(src)?.let { value -> mapObject(it, value) }
74-
}
75-
}
76-
}
77-
79+
src::class.bindParameters(array, src)
7880
return function.call(array)
7981
}
8082

@@ -92,16 +94,7 @@ class KMapper<T : Any> private constructor(
9294
is Pair<*, *> -> parameterMap.getValue(arg.first as String).let {
9395
array[it.index] = arg.second?.let { value -> mapObject(it, value) }
9496
}
95-
else -> arg::class.memberProperties.forEach { property ->
96-
val javaGetter: Method? = property.javaGetter
97-
if (javaGetter != null && property.visibility == KVisibility.PUBLIC && property.annotations.none { annotation -> annotation is KPropertyIgnore }) {
98-
parameterMap[property.findAnnotation<KGetterAlias>()?.value ?: property.name]?.let {
99-
// javaGetterを呼び出す方が高速
100-
javaGetter.isAccessible = true
101-
array[it.index] = javaGetter.invoke(arg)?.let { value -> mapObject(it, value) }
102-
}
103-
}
104-
}
97+
else -> arg::class.bindParameters(array, arg)
10598
}
10699
}
107100

0 commit comments

Comments
 (0)