@@ -4,6 +4,7 @@ import com.mapk.fastkfunction.argumentbucket.ArgumentBucket
44import com.mapk.fastkfunction.argumentbucket.BucketGenerator
55import java.lang.Exception
66import java.lang.UnsupportedOperationException
7+ import java.lang.reflect.Method
78import java.lang.reflect.Modifier
89import kotlin.reflect.KFunction
910import kotlin.reflect.KParameter
@@ -24,6 +25,17 @@ class FastKFunction<T>(private val function: KFunction<T>, instance: Any?) {
2425 if (3 <= size && get(0 ).kind != KParameter .Kind .VALUE && get(1 ).kind != KParameter .Kind .VALUE )
2526 throw IllegalArgumentException (" This function is require multiple instances." )
2627 }
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 }
2739 }
2840
2941 init {
@@ -42,7 +54,6 @@ class FastKFunction<T>(private val function: KFunction<T>, instance: Any?) {
4254 } else {
4355 val method = function.javaMethod!!
4456
45- @Suppress(" UNCHECKED_CAST" ) // methodはTを返せないため強制キャスト
4657 when (parameters[0 ].kind) {
4758 KParameter .Kind .EXTENSION_RECEIVER -> {
4859 // 対象が拡張関数ならinstanceはreceiver、指定が無ければエラー
@@ -52,7 +63,7 @@ class FastKFunction<T>(private val function: KFunction<T>, instance: Any?) {
5263
5364 valueParameters = parameters.subList(1 , parameters.size)
5465 bucketGenerator = BucketGenerator (parameters, instance)
55- fullInitializedFunction = { method.invoke( null , instance, * it) as T }
66+ fullInitializedFunction = getStaticMethodCall(method , instance)
5667 }
5768 KParameter .Kind .INSTANCE -> {
5869 valueParameters = parameters.subList(1 , parameters.size)
@@ -65,7 +76,7 @@ class FastKFunction<T>(private val function: KFunction<T>, instance: Any?) {
6576 }
6677
6778 bucketGenerator = BucketGenerator (parameters, nonNullInstance)
68- fullInitializedFunction = { method.invoke(nonNullInstance, * it) as T }
79+ fullInitializedFunction = getInstanceMethodCall(method, nonNullInstance)
6980 }
7081 KParameter .Kind .VALUE -> {
7182 valueParameters = parameters
@@ -74,19 +85,19 @@ class FastKFunction<T>(private val function: KFunction<T>, instance: Any?) {
7485 fullInitializedFunction = if (instance != null ) {
7586 // staticメソッドならば渡されたのが拡張関数でinstanceはレシーバと見做す
7687 if (Modifier .isStatic(method.modifiers)) {
77- { method.invoke( null , instance, * it) as T }
88+ getStaticMethodCall(method , instance)
7889 } else {
79- { method.invoke(instance, * it) as T }
90+ getInstanceMethodCall(method, instance)
8091 }
8192 } else {
8293 try {
8394 // 定義先がobjectであればインスタンスを利用した呼び出しを行い、そうでなければ普通に呼び出す
8495 method.declaringObject
85- ?.let { instanceFromClass -> { method.invoke(instanceFromClass, * it) as T } }
86- ? : { function.call( * it) }
96+ ?.let { instanceFromClass -> getInstanceMethodCall(method, instanceFromClass) }
97+ ? : getFunctionCall(function)
8798 } catch (e: UnsupportedOperationException ) {
8899 // トップレベル関数でobjectInstanceを取得しようとするとUnsupportedOperationExceptionになるためtryする
89- { function.call( * it) }
100+ getFunctionCall(function)
90101 }
91102 }
92103 }
0 commit comments