Avoid using reflected types in the scripting API

since it causes numerous classloading issues. Using the wrapping types
and reload them in the proper context when needed.
Note: this version supports only classes, but the wrapping type could
be extended to support other types in the future.
+ numerous fixes related to proper loading and handling of the templates.
This commit is contained in:
Ilya Chernikov
2018-05-22 19:22:42 +02:00
parent 4d65f0478b
commit a46dd5b30e
28 changed files with 506 additions and 185 deletions
@@ -9,13 +9,13 @@ package kotlin.script.experimental.util
import kotlin.reflect.KProperty
data class TypedKey<T>(val name: String)
data class TypedKey<T>(val name: String, val defaultValue: T? = null)
class TypedKeyDelegate<T> {
operator fun getValue(thisRef: Any?, property: KProperty<*>): TypedKey<T> = TypedKey(property.name)
class TypedKeyDelegate<T>(val defaultValue: T? = null) {
operator fun getValue(thisRef: Any?, property: KProperty<*>): TypedKey<T> = TypedKey(property.name, defaultValue)
}
fun <T> typedKey() = TypedKeyDelegate<T>()
fun <T> typedKey(defaultValue: T? = null) = TypedKeyDelegate(defaultValue)
open class ChainedPropertyBag private constructor(private val parent: ChainedPropertyBag?, private val data: Map<TypedKey<*>, Any?>) {
constructor(parent: ChainedPropertyBag? = null, pairs: Iterable<Pair<TypedKey<*>, Any?>>) :
@@ -31,17 +31,17 @@ open class ChainedPropertyBag private constructor(private val parent: ChainedPro
else -> ChainedPropertyBag(parent.cloneWithNewParent(newParent), data)
}
inline operator fun <reified T> get(key: TypedKey<T>): T = getUnchecked(key) as T
inline operator fun <reified T> get(key: TypedKey<T>): T = getRaw(key) as T
fun <T> getUnchecked(key: TypedKey<T>): Any? =
fun <T> getRaw(key: TypedKey<T>): Any? =
when {
data.containsKey(key) -> data[key]
parent != null -> parent.getUnchecked(key)
parent != null -> parent.getRaw(key)
key.defaultValue != null -> key.defaultValue
else -> throw IllegalArgumentException("Unknown key $key")
}
inline fun <reified T> getOrNull(key: TypedKey<T>): T? = getOrNullUnchecked(key)?.let { it as T }
inline fun <reified T> getOrNull(key: TypedKey<T>): T? = getOrNullRaw(key)?.let { it as T }
fun <T> getOrNullUnchecked(key: TypedKey<T>): Any? = data[key] ?: parent?.getOrNullUnchecked(key)
fun <T> getOrNullRaw(key: TypedKey<T>): Any? = data[key] ?: parent?.getOrNullRaw(key) ?: key.defaultValue
}