1
1
package com.mapk.kmapper
2
2
3
- import com.mapk.annotations.KConstructor
4
3
import com.mapk.annotations.KGetterAlias
5
4
import com.mapk.annotations.KGetterIgnore
6
- import com.mapk.annotations.KParameterAlias
7
5
import com.mapk.core.ArgumentBucket
8
6
import com.mapk.core.EnumMapper
9
7
import com.mapk.core.KFunctionForCall
8
+ import com.mapk.core.getAliasOrName
9
+ import com.mapk.core.toKConstructor
10
10
import java.lang.reflect.Method
11
11
import kotlin.reflect.KClass
12
12
import kotlin.reflect.KFunction
13
13
import kotlin.reflect.KParameter
14
14
import kotlin.reflect.KVisibility
15
- import kotlin.reflect.full.companionObjectInstance
16
- import kotlin.reflect.full.findAnnotation
17
- import kotlin.reflect.full.functions
18
15
import kotlin.reflect.full.isSuperclassOf
19
16
import kotlin.reflect.full.memberProperties
20
- import kotlin.reflect.full.primaryConstructor
21
17
import kotlin.reflect.jvm.javaGetter
22
18
23
19
class KMapper <T : Any > private constructor(
@@ -29,15 +25,12 @@ class KMapper<T : Any> private constructor(
29
25
)
30
26
31
27
constructor (clazz: KClass <T >, propertyNameConverter: (String ) -> String = { it }) : this (
32
- getTarget( clazz), propertyNameConverter
28
+ clazz.toKConstructor( ), propertyNameConverter
33
29
)
34
30
35
31
private val parameterMap: Map <String , ParameterForMap <* >> = function.parameters
36
32
.filter { it.kind != KParameter .Kind .INSTANCE }
37
- .associate {
38
- (it.findAnnotation<KParameterAlias >()?.value ? : propertyNameConverter(it.name!! )) to
39
- ParameterForMap .newInstance(it)
40
- }
33
+ .associate { (propertyNameConverter(it.getAliasOrName()!! )) to ParameterForMap .newInstance(it) }
41
34
42
35
private fun bindArguments (argumentBucket : ArgumentBucket , src : Any ) {
43
36
src::class .memberProperties.forEach outer@{ property ->
@@ -117,29 +110,6 @@ class KMapper<T : Any> private constructor(
117
110
}
118
111
}
119
112
120
- @Suppress(" UNCHECKED_CAST" )
121
- internal fun <T : Any > getTarget (clazz : KClass <T >): KFunctionForCall <T > {
122
- val factoryConstructor: List <KFunctionForCall <T >> =
123
- clazz.companionObjectInstance?.let { companionObject ->
124
- companionObject::class .functions
125
- .filter { it.annotations.any { annotation -> annotation is KConstructor } }
126
- .map { KFunctionForCall (
127
- it,
128
- companionObject
129
- ) as KFunctionForCall <T > }
130
- } ? : emptyList()
131
-
132
- val constructors: List <KFunctionForCall <T >> = factoryConstructor + clazz.constructors
133
- .filter { it.annotations.any { annotation -> annotation is KConstructor } }
134
- .map { KFunctionForCall (it) }
135
-
136
- if (constructors.size == 1 ) return constructors.single()
137
-
138
- if (constructors.isEmpty()) return KFunctionForCall (clazz.primaryConstructor!! )
139
-
140
- throw IllegalArgumentException (" Find multiple target." )
141
- }
142
-
143
113
private fun <T : Any , R : Any > mapObject (param : ParameterForMap <R >, value : T ): Any? {
144
114
val valueClazz: KClass <* > = value::class
145
115
0 commit comments