1
1
package com.mapk.krowmapper
2
2
3
+ import com.mapk.annotations.KColumnDeserializer
3
4
import com.mapk.core.EnumMapper
5
+ import com.mapk.core.KFunctionWithInstance
4
6
import com.mapk.core.getAliasOrName
7
+ import java.lang.IllegalArgumentException
5
8
import java.sql.ResultSet
6
9
import kotlin.reflect.KClass
7
10
import kotlin.reflect.KFunction
8
11
import kotlin.reflect.KParameter
12
+ import kotlin.reflect.full.companionObjectInstance
13
+ import kotlin.reflect.full.functions
14
+ import kotlin.reflect.full.staticFunctions
15
+ import kotlin.reflect.jvm.isAccessible
16
+ import kotlin.reflect.jvm.jvmName
9
17
10
18
class ParameterForMap private constructor(
11
19
val param : KParameter ,
@@ -16,7 +24,15 @@ class ParameterForMap private constructor(
16
24
private val deserializer: KFunction <* >?
17
25
18
26
init {
19
- deserializer = null
27
+ val deserializers = deserializerFromConstructors(kClazz) +
28
+ deserializerFromStaticMethods(kClazz) +
29
+ deserializerFromCompanionObject(kClazz)
30
+
31
+ deserializer = when {
32
+ deserializers.isEmpty() -> null
33
+ deserializers.size == 1 -> deserializers.single()
34
+ else -> throw IllegalArgumentException (" Find multiple deserializer from ${kClazz.jvmName} " )
35
+ }
20
36
}
21
37
22
38
fun getObject (rs : ResultSet ): Any? = when {
@@ -37,3 +53,35 @@ class ParameterForMap private constructor(
37
53
}
38
54
}
39
55
}
56
+
57
+ private fun <T > Collection<KFunction<T>>.getDeserializerFromFunctions (): Collection <KFunction <T >> {
58
+ return filter { it.annotations.any { annotation -> annotation is KColumnDeserializer } }
59
+ .map { func ->
60
+ func.isAccessible = true
61
+ func
62
+ }
63
+ }
64
+
65
+ private fun <T : Any > deserializerFromConstructors (clazz : KClass <T >): Collection <KFunction <T >> {
66
+ return clazz.constructors.getDeserializerFromFunctions()
67
+ }
68
+
69
+ @Suppress(" UNCHECKED_CAST" )
70
+ private fun <T : Any > deserializerFromStaticMethods (clazz : KClass <T >): Collection <KFunction <T >> {
71
+ val staticFunctions: Collection <KFunction <T >> = clazz.staticFunctions as Collection <KFunction <T >>
72
+ return staticFunctions.getDeserializerFromFunctions()
73
+ }
74
+
75
+ @Suppress(" UNCHECKED_CAST" )
76
+ private fun <T : Any > deserializerFromCompanionObject (clazz : KClass <T >): Collection <KFunction <T >> {
77
+ return clazz.companionObjectInstance?.let { companionObject ->
78
+ companionObject::class .functions
79
+ .filter { it.annotations.any { annotation -> annotation is KColumnDeserializer } }
80
+ .map { function ->
81
+ KFunctionWithInstance (
82
+ function,
83
+ companionObject
84
+ ) as KFunction <T >
85
+ }.toSet()
86
+ } ? : emptySet()
87
+ }
0 commit comments