@@ -4,6 +4,7 @@ import com.mapk.fastkfunction.argumentbucket.ArgumentBucket
4
4
import com.mapk.fastkfunction.argumentbucket.BucketGenerator
5
5
import java.lang.Exception
6
6
import java.lang.UnsupportedOperationException
7
+ import java.lang.reflect.Method
7
8
import java.lang.reflect.Modifier
8
9
import kotlin.reflect.KFunction
9
10
import kotlin.reflect.KParameter
@@ -24,6 +25,17 @@ class FastKFunction<T>(private val function: KFunction<T>, instance: Any?) {
24
25
if (3 <= size && get(0 ).kind != KParameter .Kind .VALUE && get(1 ).kind != KParameter .Kind .VALUE )
25
26
throw IllegalArgumentException (" This function is require multiple instances." )
26
27
}
28
+
29
+ private fun <T > getFunctionCall (function : KFunction <T >): (Array <out Any ?>) -> T = { function.call(* it) }
30
+
31
+ // methodはTを返せないため強制キャスト
32
+ @Suppress(" UNCHECKED_CAST" )
33
+ private fun <T > getStaticMethodCall (method : Method , instance : Any ): (Array <out Any ?>) -> T =
34
+ { method.invoke(null , instance, * it) as T }
35
+
36
+ @Suppress(" UNCHECKED_CAST" )
37
+ private fun <T > getInstanceMethodCall (method : Method , instance : Any ): (Array <out Any ?>) -> T =
38
+ { method.invoke(instance, * it) as T }
27
39
}
28
40
29
41
init {
@@ -42,7 +54,6 @@ class FastKFunction<T>(private val function: KFunction<T>, instance: Any?) {
42
54
} else {
43
55
val method = function.javaMethod!!
44
56
45
- @Suppress(" UNCHECKED_CAST" ) // methodはTを返せないため強制キャスト
46
57
when (parameters[0 ].kind) {
47
58
KParameter .Kind .EXTENSION_RECEIVER -> {
48
59
// 対象が拡張関数ならinstanceはreceiver、指定が無ければエラー
@@ -52,7 +63,7 @@ class FastKFunction<T>(private val function: KFunction<T>, instance: Any?) {
52
63
53
64
valueParameters = parameters.subList(1 , parameters.size)
54
65
bucketGenerator = BucketGenerator (parameters, instance)
55
- fullInitializedFunction = { method.invoke( null , instance, * it) as T }
66
+ fullInitializedFunction = getStaticMethodCall(method , instance)
56
67
}
57
68
KParameter .Kind .INSTANCE -> {
58
69
valueParameters = parameters.subList(1 , parameters.size)
@@ -65,7 +76,7 @@ class FastKFunction<T>(private val function: KFunction<T>, instance: Any?) {
65
76
}
66
77
67
78
bucketGenerator = BucketGenerator (parameters, nonNullInstance)
68
- fullInitializedFunction = { method.invoke(nonNullInstance, * it) as T }
79
+ fullInitializedFunction = getInstanceMethodCall(method, nonNullInstance)
69
80
}
70
81
KParameter .Kind .VALUE -> {
71
82
valueParameters = parameters
@@ -74,19 +85,19 @@ class FastKFunction<T>(private val function: KFunction<T>, instance: Any?) {
74
85
fullInitializedFunction = if (instance != null ) {
75
86
// staticメソッドならば渡されたのが拡張関数でinstanceはレシーバと見做す
76
87
if (Modifier .isStatic(method.modifiers)) {
77
- { method.invoke( null , instance, * it) as T }
88
+ getStaticMethodCall(method , instance)
78
89
} else {
79
- { method.invoke(instance, * it) as T }
90
+ getInstanceMethodCall(method, instance)
80
91
}
81
92
} else {
82
93
try {
83
94
// 定義先がobjectであればインスタンスを利用した呼び出しを行い、そうでなければ普通に呼び出す
84
95
method.declaringObject
85
- ?.let { instanceFromClass -> { method.invoke(instanceFromClass, * it) as T } }
86
- ? : { function.call( * it) }
96
+ ?.let { instanceFromClass -> getInstanceMethodCall(method, instanceFromClass) }
97
+ ? : getFunctionCall(function)
87
98
} catch (e: UnsupportedOperationException ) {
88
99
// トップレベル関数でobjectInstanceを取得しようとするとUnsupportedOperationExceptionになるためtryする
89
- { function.call( * it) }
100
+ getFunctionCall(function)
90
101
}
91
102
}
92
103
}
0 commit comments