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:
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package kotlin.script.experimental.api
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.KType
|
||||
|
||||
class KotlinType(
|
||||
val typeName: String,
|
||||
val fromClass: KClass<*>? = null
|
||||
// TODO: copy properties from KType
|
||||
) {
|
||||
// TODO: implement other approach for non-class types
|
||||
constructor(type: KType) : this((type.classifier as KClass<*>).qualifiedName!!, type.classifier as KClass<*>)
|
||||
|
||||
constructor(kclass: KClass<*>) : this(kclass.qualifiedName!!, kclass)
|
||||
}
|
||||
+5
-7
@@ -7,8 +7,6 @@
|
||||
|
||||
package kotlin.script.experimental.api
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.KType
|
||||
import kotlin.script.experimental.util.typedKey
|
||||
|
||||
object ScriptCompileConfigurationProperties {
|
||||
@@ -19,9 +17,9 @@ object ScriptCompileConfigurationProperties {
|
||||
|
||||
val scriptBodyTarget by typedKey<ScriptBodyTarget>()
|
||||
|
||||
val scriptImplicitReceivers by typedKey<List<KType>>() // in the order from outer to inner scope
|
||||
val scriptImplicitReceivers by typedKey<List<KotlinType>>() // in the order from outer to inner scope
|
||||
|
||||
val contextVariables by typedKey<Map<String, KType>>() // external variables
|
||||
val contextVariables by typedKey<Map<String, KotlinType>>() // external variables
|
||||
|
||||
val defaultImports by typedKey<List<String>>()
|
||||
|
||||
@@ -31,15 +29,15 @@ object ScriptCompileConfigurationProperties {
|
||||
|
||||
val dependencies by typedKey<List<ScriptDependency>>()
|
||||
|
||||
val generatedClassAnnotations by typedKey<List<KClass<out Annotation>>>()
|
||||
val generatedClassAnnotations by typedKey<List<Annotation>>()
|
||||
|
||||
val generatedMethodAnnotations by typedKey<List<KClass<out Annotation>>>()
|
||||
val generatedMethodAnnotations by typedKey<List<Annotation>>()
|
||||
|
||||
val compilerOptions by typedKey<List<String>>() // Q: CommonCompilerOptions instead?
|
||||
|
||||
val refineBeforeParsing by typedKey<Boolean>() // default: false
|
||||
|
||||
val refineConfigurationOnAnnotations by typedKey<List<KClass<out Annotation>>>()
|
||||
val refineConfigurationOnAnnotations by typedKey<List<KotlinType>>()
|
||||
|
||||
val refineConfigurationOnSections by typedKey<List<String>>()
|
||||
}
|
||||
|
||||
@@ -8,8 +8,6 @@
|
||||
package kotlin.script.experimental.api
|
||||
|
||||
import java.net.URL
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.KType
|
||||
|
||||
interface ScriptSource {
|
||||
val location: URL?
|
||||
|
||||
+29
-1
@@ -14,6 +14,34 @@ typealias ScriptingEnvironment = ChainedPropertyBag
|
||||
object ScriptingEnvironmentProperties {
|
||||
|
||||
// required by definitions that extract data from script base class annotations
|
||||
val baseClass by typedKey<KClass<*>>()
|
||||
val baseClass by typedKey<KotlinType>()
|
||||
|
||||
// should contain all dependencies needed for baseClass and compilationConfigurator
|
||||
val configurationDependencies by typedKey<List<ScriptDependency>>()
|
||||
|
||||
// do not use configurationDependencies as script dependencies, so only the dependencies defined by compilationConfigurator will be used
|
||||
// (NOTE: in this case they should include the dependencies for the base class anyway, since this class is needed for script
|
||||
// compilation and instantiation, but compilationConfigurator could be excluded)
|
||||
val isolatedDependencies by typedKey(false)
|
||||
|
||||
// a "class loader" for KotlinTypes
|
||||
val getScriptingClass by typedKey<GetScriptingClass>()
|
||||
}
|
||||
|
||||
interface GetScriptingClass {
|
||||
operator fun invoke(classType: KotlinType, contextClass: KClass<*>, environment: ScriptingEnvironment): KClass<*>
|
||||
}
|
||||
|
||||
fun ScriptingEnvironment.getScriptingClass(type: KotlinType, contextClass: KClass<*>): KClass<*> {
|
||||
val getClass = getOrNull(ScriptingEnvironmentProperties.getScriptingClass)
|
||||
?: throw IllegalArgumentException("Expecting 'getScriptingClass' property in the scripting environment: unable to load scripting class $type")
|
||||
return getClass(type, contextClass, this)
|
||||
}
|
||||
|
||||
fun ScriptingEnvironment.getScriptingClass(type: KotlinType, context: Any): KClass<*> = getScriptingClass(type, context::class)
|
||||
|
||||
fun ScriptingEnvironment.getScriptBaseClass(contextClass: KClass<*>): KClass<*> =
|
||||
getScriptingClass(get(ScriptingEnvironmentProperties.baseClass), contextClass)
|
||||
|
||||
fun ScriptingEnvironment.getScriptBaseClass(context: Any): KClass<*> =
|
||||
getScriptingClass(get(ScriptingEnvironmentProperties.baseClass), context::class)
|
||||
|
||||
@@ -10,15 +10,14 @@ import kotlin.script.experimental.annotations.KotlinScriptDefaultCompilationConf
|
||||
import kotlin.script.experimental.api.*
|
||||
import kotlin.script.experimental.util.TypedKey
|
||||
|
||||
|
||||
private const val ILLEGAL_CONFIG_ANN_ARG =
|
||||
"Illegal argument to KotlinScriptDefaultCompilationConfiguration annotation: expecting List-derived object or default-constructed class of configuration parameters"
|
||||
|
||||
open class AnnotationsBasedCompilationConfigurator(val environment: ScriptingEnvironment) : ScriptCompilationConfigurator {
|
||||
|
||||
override val defaultConfiguration = run {
|
||||
val base = environment[ScriptingEnvironmentProperties.baseClass]
|
||||
val cfg = base.annotations.filterIsInstance(KotlinScriptDefaultCompilationConfiguration::class.java).flatMap { ann ->
|
||||
override val defaultConfiguration by lazy {
|
||||
val baseClass = environment.getScriptBaseClass(this)
|
||||
val cfg = baseClass.annotations.filterIsInstance(KotlinScriptDefaultCompilationConfiguration::class.java).flatMap { ann ->
|
||||
val params = try {
|
||||
ann.compilationConfiguration.objectInstance ?: ann.compilationConfiguration.createInstance()
|
||||
} catch (e: Throwable) {
|
||||
|
||||
+31
-10
@@ -18,8 +18,18 @@ private const val ERROR_MSG_PREFIX = "Unable to construct script definition: "
|
||||
|
||||
open class ScriptDefinitionFromAnnotatedBaseClass(val environment: ScriptingEnvironment) : ScriptDefinition {
|
||||
|
||||
private val baseClass: KClass<*> = environment.getOrNull(ScriptingEnvironmentProperties.baseClass)
|
||||
?: throw IllegalArgumentException("${ERROR_MSG_PREFIX}Expecting baseClass parameter in the scripting environment")
|
||||
private val getScriptingClass = environment.getOrNull(ScriptingEnvironmentProperties.getScriptingClass)
|
||||
?: throw IllegalArgumentException("${ERROR_MSG_PREFIX}Expecting 'getClass' parameter in the scripting environment")
|
||||
|
||||
private val baseClass: KClass<*> = run {
|
||||
val baseClassType = environment.getOrNull(ScriptingEnvironmentProperties.baseClass)
|
||||
?: throw IllegalArgumentException("${ERROR_MSG_PREFIX}Expecting 'baseClass' parameter in the scripting environment")
|
||||
try {
|
||||
getScriptingClass(baseClassType, this::class, environment)
|
||||
} catch (e: Throwable) {
|
||||
throw IllegalArgumentException("${ERROR_MSG_PREFIX}Unable to load base class $baseClassType", e)
|
||||
}
|
||||
}
|
||||
|
||||
private val mainAnnotation = baseClass.findAnnotation<KotlinScript>()
|
||||
?: throw IllegalArgumentException("${ERROR_MSG_PREFIX}Expecting KotlinScript annotation on the $baseClass")
|
||||
@@ -27,13 +37,17 @@ open class ScriptDefinitionFromAnnotatedBaseClass(val environment: ScriptingEnvi
|
||||
private val explicitDefinition: ScriptDefinition? =
|
||||
baseClass.findAnnotation<KotlinScriptDefinition>()?.definition.takeIf { it != this::class }?.let { it.instantiateScriptHandler() }
|
||||
|
||||
override val properties = (explicitDefinition?.properties ?: ScriptingEnvironment()).also { properties ->
|
||||
override val properties = run {
|
||||
val baseProperties = explicitDefinition?.properties ?: environment
|
||||
val toAdd = arrayListOf<Pair<TypedKey<*>, Any>>()
|
||||
baseClass.findAnnotation<KotlinScriptFileExtension>()?.let { toAdd += ScriptDefinitionProperties.fileExtension to it }
|
||||
if (properties.getOrNull(ScriptDefinitionProperties.name) == null) {
|
||||
baseClass.findAnnotation<KotlinScriptFileExtension>()?.let {
|
||||
toAdd += ScriptDefinitionProperties.fileExtension to it.extension
|
||||
}
|
||||
if (baseProperties.getOrNull(ScriptDefinitionProperties.name) == null) {
|
||||
toAdd += ScriptDefinitionProperties.name to mainAnnotation.name
|
||||
}
|
||||
ScriptingEnvironment(properties, toAdd)
|
||||
if (toAdd.isEmpty()) baseProperties
|
||||
else ScriptingEnvironment(baseProperties, toAdd)
|
||||
}
|
||||
|
||||
override val compilationConfigurator =
|
||||
@@ -47,10 +61,17 @@ open class ScriptDefinitionFromAnnotatedBaseClass(val environment: ScriptingEnvi
|
||||
?: DummyEvaluator::class.instantiateScriptHandler()
|
||||
|
||||
private fun <T : Any> KClass<T>.instantiateScriptHandler(): T {
|
||||
val fqn = this.qualifiedName!!
|
||||
val klass: KClass<T> = (baseClass.java.classLoader.loadClass(fqn) as Class<T>).kotlin
|
||||
// TODO: fix call after deciding on constructor parameters
|
||||
return klass.objectInstance ?: klass.primaryConstructor!!.call(environment)
|
||||
val klass: KClass<T> = try {
|
||||
getScriptingClass(KotlinType(this), this@ScriptDefinitionFromAnnotatedBaseClass::class, environment) as KClass<T>
|
||||
} catch (e: Throwable) {
|
||||
throw IllegalArgumentException("${ERROR_MSG_PREFIX}Unable to load handler $this: $e", e)
|
||||
}
|
||||
try {
|
||||
// TODO: fix call after deciding on constructor parameters
|
||||
return klass.objectInstance ?: klass.primaryConstructor!!.call(environment)
|
||||
} catch (e: Throwable) {
|
||||
throw IllegalArgumentException("${ERROR_MSG_PREFIX}Unable to instantiate handler $this: $e", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user