@@ -2,6 +2,9 @@ package com.mapk.fastkfunction
2
2
3
3
import com.mapk.fastkfunction.argumentbucket.ArgumentBucket
4
4
import com.mapk.fastkfunction.argumentbucket.BucketGenerator
5
+ import com.mapk.fastkfunction.spreadwrapper.ForConstructor
6
+ import com.mapk.fastkfunction.spreadwrapper.ForKFunction
7
+ import com.mapk.fastkfunction.spreadwrapper.ForMethod
5
8
import java.lang.reflect.Method
6
9
import java.lang.reflect.Modifier
7
10
import kotlin.reflect.KFunction
@@ -22,64 +25,68 @@ sealed class FastKFunction<T> {
22
25
23
26
internal class Constructor <T >(
24
27
private val function : KFunction <T >,
25
- private val constructor : JavaConstructor <T >,
28
+ constructor : JavaConstructor <T >,
26
29
override val valueParameters : List <KParameter >
27
30
) : FastKFunction<T>() {
31
+ private val spreadWrapper = ForConstructor (constructor )
28
32
override val bucketGenerator = BucketGenerator (valueParameters, null )
29
33
30
34
override fun callBy (bucket : ArgumentBucket ): T = if (bucket.isFullInitialized()) {
31
- constructor .newInstance( * bucket.getValueArray())
35
+ spreadWrapper.call( bucket.getValueArray())
32
36
} else {
33
37
function.callBy(bucket)
34
38
}
35
39
36
- override fun callByCollection (args : Collection <Any ?>): T = constructor .newInstance( * args.toTypedArray())
40
+ override fun callByCollection (args : Collection <Any ?>): T = spreadWrapper.call( args.toTypedArray())
37
41
38
- override fun call (vararg args : Any? ): T = constructor .newInstance( * args)
42
+ override fun call (vararg args : Any? ): T = spreadWrapper.call( args)
39
43
}
40
44
41
45
internal class Function <T >(
42
46
private val function : KFunction <T >,
43
47
override val valueParameters : List <KParameter >
44
48
) : FastKFunction<T>() {
49
+ private val spreadWrapper = ForKFunction (function)
45
50
override val bucketGenerator = BucketGenerator (valueParameters, null )
46
51
47
52
override fun callBy (bucket : ArgumentBucket ): T = if (bucket.isFullInitialized()) {
48
- function .call(* bucket.getValueArray())
53
+ spreadWrapper .call(bucket.getValueArray())
49
54
} else {
50
55
function.callBy(bucket)
51
56
}
52
57
53
- override fun callByCollection (args : Collection <Any ?>): T = function .call(* args.toTypedArray())
58
+ override fun callByCollection (args : Collection <Any ?>): T = spreadWrapper .call(args.toTypedArray())
54
59
55
- override fun call (vararg args : Any? ): T = function .call(* args)
60
+ override fun call (vararg args : Any? ): T = spreadWrapper .call(args)
56
61
}
57
62
58
63
internal class TopLevelFunction <T >(
59
64
private val function : KFunction <T >,
60
- private val method : Method ,
65
+ method : Method ,
61
66
override val valueParameters : List <KParameter >
62
67
) : FastKFunction<T>() {
68
+ private val spreadWrapper = ForMethod (method, null )
63
69
override val bucketGenerator = BucketGenerator (valueParameters, null )
64
70
65
71
@Suppress(" UNCHECKED_CAST" )
66
72
override fun callBy (bucket : ArgumentBucket ): T = if (bucket.isFullInitialized()) {
67
- method.invoke( null , * bucket.getValueArray()) as T
73
+ spreadWrapper.call( bucket.getValueArray()) as T
68
74
} else {
69
75
function.callBy(bucket)
70
76
}
71
77
72
78
@Suppress(" UNCHECKED_CAST" )
73
- override fun callByCollection (args : Collection <Any ?>): T = method.invoke( null , * args.toTypedArray()) as T
79
+ override fun callByCollection (args : Collection <Any ?>): T = spreadWrapper.call( args.toTypedArray()) as T
74
80
75
81
@Suppress(" UNCHECKED_CAST" )
76
- override fun call (vararg args : Any? ): T = method.invoke( null , * args) as T
82
+ override fun call (vararg args : Any? ): T = spreadWrapper.call( args) as T
77
83
}
78
84
85
+ // NOTE: トップレベル拡張関数に関してはスプレッド演算子抹消対応が難しい(Bucket関連を弄らなきゃ無理)ため、一旦手を付けていない
79
86
internal class TopLevelExtensionFunction <T >(
80
87
private val function : KFunction <T >,
81
88
private val method : Method ,
82
- private val extensionReceiver : Any? ,
89
+ private val extensionReceiver : Any ,
83
90
override val bucketGenerator : BucketGenerator ,
84
91
override val valueParameters : List <KParameter >
85
92
) : FastKFunction<T>() {
@@ -100,23 +107,25 @@ sealed class FastKFunction<T> {
100
107
101
108
internal class InstanceFunction <T >(
102
109
private val function : KFunction <T >,
103
- private val method : Method ,
104
- private val instance : Any ,
110
+ method : Method ,
111
+ instance : Any ,
105
112
override val bucketGenerator : BucketGenerator ,
106
113
override val valueParameters : List <KParameter >
107
114
) : FastKFunction<T>() {
115
+ private val spreadWrapper = ForMethod (method, instance)
116
+
108
117
@Suppress(" UNCHECKED_CAST" )
109
118
override fun callBy (bucket : ArgumentBucket ): T = if (bucket.isFullInitialized()) {
110
- method.invoke(instance, * bucket.getValueArray()) as T
119
+ spreadWrapper.call( bucket.getValueArray()) as T
111
120
} else {
112
121
function.callBy(bucket)
113
122
}
114
123
115
124
@Suppress(" UNCHECKED_CAST" )
116
- override fun callByCollection (args : Collection <Any ?>): T = method.invoke(instance, * args.toTypedArray()) as T
125
+ override fun callByCollection (args : Collection <Any ?>): T = spreadWrapper.call( args.toTypedArray()) as T
117
126
118
127
@Suppress(" UNCHECKED_CAST" )
119
- override fun call (vararg args : Any? ): T = method.invoke(instance, * args) as T
128
+ override fun call (vararg args : Any? ): T = spreadWrapper.call( args) as T
120
129
}
121
130
122
131
companion object {
0 commit comments