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
@@ -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)
}
@@ -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?
@@ -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)