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

Commit d85fc72

Browse files
committed
コンストラクタのスプレッド処理をラップ
1 parent d84ff04 commit d85fc72

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.mapk.fastkfunction.spreadwrapper;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.lang.reflect.Constructor;
6+
import java.lang.reflect.InvocationTargetException;
7+
8+
public class ForConstructor<T> {
9+
private final Constructor<T> constructor;
10+
11+
public ForConstructor(@NotNull Constructor<T> constructor) {
12+
this.constructor = constructor;
13+
}
14+
15+
public T call(Object[] args) throws IllegalAccessException, InvocationTargetException, InstantiationException {
16+
return constructor.newInstance(args);
17+
}
18+
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.mapk.fastkfunction
22

33
import com.mapk.fastkfunction.argumentbucket.ArgumentBucket
44
import com.mapk.fastkfunction.argumentbucket.BucketGenerator
5+
import com.mapk.fastkfunction.spreadwrapper.ForConstructor
56
import java.lang.reflect.Method
67
import java.lang.reflect.Modifier
78
import kotlin.reflect.KFunction
@@ -22,20 +23,21 @@ sealed class FastKFunction<T> {
2223

2324
internal class Constructor<T>(
2425
private val function: KFunction<T>,
25-
private val constructor: JavaConstructor<T>,
26+
constructor: JavaConstructor<T>,
2627
override val valueParameters: List<KParameter>
2728
) : FastKFunction<T>() {
29+
private val spreadWrapper = ForConstructor(constructor)
2830
override val bucketGenerator = BucketGenerator(valueParameters, null)
2931

3032
override fun callBy(bucket: ArgumentBucket): T = if (bucket.isFullInitialized()) {
31-
constructor.newInstance(*bucket.getValueArray())
33+
spreadWrapper.call(bucket.getValueArray())
3234
} else {
3335
function.callBy(bucket)
3436
}
3537

36-
override fun callByCollection(args: Collection<Any?>): T = constructor.newInstance(*args.toTypedArray())
38+
override fun callByCollection(args: Collection<Any?>): T = spreadWrapper.call(args.toTypedArray())
3739

38-
override fun call(vararg args: Any?): T = constructor.newInstance(*args)
40+
override fun call(vararg args: Any?): T = spreadWrapper.call(args)
3941
}
4042

4143
internal class Function<T>(

0 commit comments

Comments
 (0)