Reflection: add KClass.createInstance

This commit is contained in:
Alexander Udalov
2016-07-12 19:53:30 +03:00
parent 0525b1e6c1
commit 89d69bc7eb
3 changed files with 92 additions and 0 deletions
@@ -272,3 +272,16 @@ fun <T : Any> KClass<T>.cast(value: Any?): T {
fun <T : Any> KClass<T>.safeCast(value: Any?): T? {
return if (isInstance(value)) value as T else null
}
/**
* Creates a new instance of the class, calling a constructor which either has no parameters or all parameters of which are optional
* (see [KParameter.isOptional]). If there are no or many such constructors, an exception is thrown.
*/
fun <T : Any> KClass<T>.createInstance(): T {
// TODO: throw a meaningful exception
val noArgsConstructor = constructors.singleOrNull { it.parameters.all(KParameter::isOptional) }
?: throw IllegalArgumentException("Class should have a single no-arg constructor: $this")
return noArgsConstructor.callBy(emptyMap())
}