Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.

Commit 62ab2df

Browse files
committed
インスタンス/static関数のスプレッド処理をラップ
1 parent ece9081 commit 62ab2df

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.mapk.fastkfunction.spreadwrapper;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.Nullable;
5+
6+
import java.lang.reflect.InvocationTargetException;
7+
import java.lang.reflect.Method;
8+
9+
public class ForMethod {
10+
private final Method method;
11+
private final Object instance;
12+
13+
public ForMethod(@NotNull Method method, @Nullable Object instance) {
14+
this.method = method;
15+
this.instance = instance;
16+
}
17+
18+
public Object call(Object[] args) throws InvocationTargetException, IllegalAccessException {
19+
return method.invoke(instance, args);
20+
}
21+
}

src/main/kotlin/com/mapk/fastkfunction/FastKFunction.kt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.mapk.fastkfunction.argumentbucket.ArgumentBucket
44
import com.mapk.fastkfunction.argumentbucket.BucketGenerator
55
import com.mapk.fastkfunction.spreadwrapper.ForConstructor
66
import com.mapk.fastkfunction.spreadwrapper.ForKFunction
7+
import com.mapk.fastkfunction.spreadwrapper.ForMethod
78
import java.lang.reflect.Method
89
import java.lang.reflect.Modifier
910
import kotlin.reflect.KFunction
@@ -61,23 +62,24 @@ sealed class FastKFunction<T> {
6162

6263
internal class TopLevelFunction<T>(
6364
private val function: KFunction<T>,
64-
private val method: Method,
65+
method: Method,
6566
override val valueParameters: List<KParameter>
6667
) : FastKFunction<T>() {
68+
private val spreadWrapper = ForMethod(method, null)
6769
override val bucketGenerator = BucketGenerator(valueParameters, null)
6870

6971
@Suppress("UNCHECKED_CAST")
7072
override fun callBy(bucket: ArgumentBucket): T = if (bucket.isFullInitialized()) {
71-
method.invoke(null, *bucket.getValueArray()) as T
73+
spreadWrapper.call(bucket.getValueArray()) as T
7274
} else {
7375
function.callBy(bucket)
7476
}
7577

7678
@Suppress("UNCHECKED_CAST")
77-
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
7880

7981
@Suppress("UNCHECKED_CAST")
80-
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
8183
}
8284

8385
internal class TopLevelExtensionFunction<T>(
@@ -104,23 +106,25 @@ sealed class FastKFunction<T> {
104106

105107
internal class InstanceFunction<T>(
106108
private val function: KFunction<T>,
107-
private val method: Method,
108-
private val instance: Any,
109+
method: Method,
110+
instance: Any,
109111
override val bucketGenerator: BucketGenerator,
110112
override val valueParameters: List<KParameter>
111113
) : FastKFunction<T>() {
114+
private val spreadWrapper = ForMethod(method, instance)
115+
112116
@Suppress("UNCHECKED_CAST")
113117
override fun callBy(bucket: ArgumentBucket): T = if (bucket.isFullInitialized()) {
114-
method.invoke(instance, *bucket.getValueArray()) as T
118+
spreadWrapper.call(bucket.getValueArray()) as T
115119
} else {
116120
function.callBy(bucket)
117121
}
118122

119123
@Suppress("UNCHECKED_CAST")
120-
override fun callByCollection(args: Collection<Any?>): T = method.invoke(instance, *args.toTypedArray()) as T
124+
override fun callByCollection(args: Collection<Any?>): T = spreadWrapper.call(args.toTypedArray()) as T
121125

122126
@Suppress("UNCHECKED_CAST")
123-
override fun call(vararg args: Any?): T = method.invoke(instance, *args) as T
127+
override fun call(vararg args: Any?): T = spreadWrapper.call(args) as T
124128
}
125129

126130
companion object {

0 commit comments

Comments
 (0)