-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Support vararg and default parameters for filter runtime (#560)
- Loading branch information
Showing
2 changed files
with
103 additions
and
34 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
lib/sisyphus-common/src/main/kotlin/com/bybutter/sisyphus/reflect/Functions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.bybutter.sisyphus.reflect | ||
|
||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KFunction | ||
import kotlin.reflect.KParameter | ||
import kotlin.reflect.KType | ||
import kotlin.reflect.full.extensionReceiverParameter | ||
import kotlin.reflect.full.valueParameters | ||
|
||
fun KFunction<*>.resolveArguments(args: List<Any?>): Map<KParameter, Any?>? { | ||
val requiredParameters = mutableListOf<KParameter>() | ||
val optionalParameters = mutableListOf<KParameter>() | ||
val result = mutableMapOf<KParameter, Any?>() | ||
var index = 0 | ||
|
||
extensionReceiverParameter?.let { | ||
requiredParameters += it | ||
} | ||
|
||
valueParameters.forEach { | ||
if (it.isOptional || it.isVararg) { | ||
optionalParameters += it | ||
} else { | ||
requiredParameters += optionalParameters | ||
optionalParameters.clear() | ||
requiredParameters += it | ||
} | ||
} | ||
|
||
for (it in requiredParameters) { | ||
if (index >= args.size) return null | ||
|
||
if (!it.accept(args[index])) { | ||
return null | ||
} | ||
|
||
result[it] = args[index] | ||
index++ | ||
} | ||
|
||
for (it in optionalParameters) { | ||
if (index >= args.size) break | ||
|
||
if (it.isVararg) { | ||
val vararg = mutableListOf<Any?>() | ||
while (index < args.size) { | ||
if (!it.accept(args[index])) { | ||
continue | ||
} | ||
vararg += args[index] | ||
index++ | ||
} | ||
val varargCollection = vararg as java.util.Collection<Any?> | ||
result[it] = | ||
varargCollection.toArray( | ||
java.lang.reflect.Array.newInstance( | ||
(it.type.arguments.first().type!!.classifier as KClass<*>).java, | ||
0, | ||
) as Array<*>, | ||
) | ||
} else { | ||
if (!it.accept(args[index])) { | ||
break | ||
} | ||
result[it] = args[index] | ||
index++ | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
private fun KParameter.accept(value: Any?): Boolean = | ||
if (isVararg) { | ||
type.arguments.first().type?.accept(value)!! | ||
} else { | ||
type.accept(value) | ||
} | ||
|
||
private fun KType.accept(value: Any?): Boolean { | ||
if (value == null) { | ||
return this.isMarkedNullable | ||
} | ||
return (this.classifier as KClass<*>).isInstance(value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters