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)
@@ -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) {
@@ -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
}
@@ -236,10 +236,13 @@ internal class BridgeScriptDefinition(
scriptCompilerConfiguration: ScriptCompileConfiguration,
scriptConfigurator: ScriptCompilationConfigurator?,
updateClasspath: (List<File>) -> Unit
) : KotlinScriptDefinition(scriptCompilerConfiguration[ScriptingEnvironmentProperties.baseClass] as KClass<out Any>) {
override val acceptedAnnotations =
scriptCompilerConfiguration.getOrNull(ScriptCompileConfigurationProperties.refineConfigurationOnAnnotations)?.toList()
) : KotlinScriptDefinition(scriptCompilerConfiguration.getScriptBaseClass(BridgeScriptDefinition::class)) {
override val acceptedAnnotations = run {
val cl = this::class.java.classLoader
scriptCompilerConfiguration.getOrNull(ScriptCompileConfigurationProperties.refineConfigurationOnAnnotations)
?.map { (cl.loadClass(it.typeName) as Class<out Annotation>).kotlin }
?: emptyList()
}
override val dependencyResolver: DependenciesResolver =
BridgeDependenciesResolver(scriptConfigurator, scriptCompilerConfiguration, updateClasspath)
@@ -0,0 +1,61 @@
/*
* 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.jvm
import java.net.URLClassLoader
import kotlin.reflect.KClass
import kotlin.script.experimental.api.*
class JvmGetScriptingClass : GetScriptingClass {
private var dependencies: List<ScriptDependency>? = null
private var classLoader: ClassLoader? = null
private var baseClassLoader: ClassLoader? = null
@Synchronized
override fun invoke(classType: KotlinType, contextClass: KClass<*>, environment: ScriptingEnvironment): KClass<*> {
// checking if class already loaded in the same context
val contextClassloader = contextClass.java.classLoader
if (classType.fromClass != null) {
if (classType.fromClass!!.java.classLoader == null) return classType.fromClass!! // root classloader
val actualClassLoadersChain = generateSequence(classType.fromClass!!.java.classLoader) { it.parent }
if (actualClassLoadersChain.any { it == contextClassloader }) return classType.fromClass!!
}
val newDeps = environment.getOrNull(ScriptingEnvironmentProperties.configurationDependencies)
if (dependencies == null) {
dependencies = newDeps
} else {
if (newDeps != dependencies) throw IllegalArgumentException("scripting environment dependencies changed")
}
if (baseClassLoader == null) {
baseClassLoader = contextClassloader
} else {
val baseClassLoadersChain = generateSequence(baseClassLoader) { it.parent }
if (baseClassLoadersChain.none { it == contextClassloader }) throw IllegalArgumentException("scripting class instantiation context changed")
}
if (classLoader == null) {
val classpath = dependencies?.flatMap {
when(it) {
is JvmDependency -> it.classpath.map { it.toURI().toURL() }
else -> throw IllegalArgumentException("unknown dependency type $it")
}
}
classLoader =
if (classpath == null || classpath.isEmpty()) baseClassLoader
else URLClassLoader(classpath.toTypedArray(), baseClassLoader)
}
return try {
classLoader!!.loadClass(classType.typeName).kotlin
} catch (e: Throwable) {
throw IllegalArgumentException("unable to load class $classType", e)
}
}
}
@@ -6,15 +6,48 @@
package kotlin.script.experimental.misc
import kotlin.reflect.KClass
import kotlin.reflect.KType
import kotlin.script.experimental.api.KotlinType
import kotlin.script.experimental.util.TypedKey
inline operator fun <reified T> TypedKey<T>.invoke(v: T): Pair<TypedKey<T>, T> = this to v
inline operator fun <reified K> TypedKey<KClass<*>>.invoke(): Pair<TypedKey<KClass<*>>, KClass<*>> = this to K::class
inline operator fun <reified K> TypedKey<KotlinType>.invoke(): Pair<TypedKey<KotlinType>, KotlinType> =
this to KotlinType(K::class)
operator fun TypedKey<KotlinType>.invoke(kclass: KClass<*>): Pair<TypedKey<KotlinType>, KotlinType> =
this to KotlinType(kclass)
operator fun TypedKey<KotlinType>.invoke(ktype: KType): Pair<TypedKey<KotlinType>, KotlinType> =
this to KotlinType(ktype)
operator fun TypedKey<KotlinType>.invoke(fqname: String): Pair<TypedKey<KotlinType>, KotlinType> =
this to KotlinType(fqname)
operator fun TypedKey<List<KotlinType>>.invoke(vararg classes: KClass<*>): Pair<TypedKey<List<KotlinType>>, List<KotlinType>> =
this to classes.map { KotlinType(it) }
operator fun TypedKey<List<KotlinType>>.invoke(vararg types: KType): Pair<TypedKey<List<KotlinType>>, List<KotlinType>> =
this to types.map { KotlinType(it) }
operator fun TypedKey<List<KotlinType>>.invoke(vararg fqnames: String): Pair<TypedKey<List<KotlinType>>, List<KotlinType>> =
this to fqnames.map { KotlinType(it) }
inline operator fun <reified E> TypedKey<List<E>>.invoke(vararg vs: E): Pair<TypedKey<List<E>>, List<E>> = this to vs.toList()
@JvmName("invoke_kotlintype_map_from_kclass")
inline operator fun <reified K> TypedKey<Map<K, KotlinType>>.invoke(vararg classes: Pair<K, KClass<*>>): Pair<TypedKey<Map<K, KotlinType>>, Map<K, KotlinType>> =
this to HashMap<K, KotlinType>().also { it.putAll(classes.asSequence().map { (k, v) -> k to KotlinType(v) }) }
@JvmName("invoke_kotlintype_map_from_ktype")
inline operator fun <reified K> TypedKey<Map<K, KotlinType>>.invoke(vararg types: Pair<K, KType>): Pair<TypedKey<Map<K, KotlinType>>, Map<K, KotlinType>> =
this to HashMap<K, KotlinType>().also { it.putAll(types.asSequence().map { (k, v) -> k to KotlinType(v) }) }
@JvmName("invoke_kotlintype_map_from_fqname")
inline operator fun <reified K> TypedKey<Map<K, KotlinType>>.invoke(vararg fqnames: Pair<K, String>): Pair<TypedKey<Map<K, KotlinType>>, Map<K, KotlinType>> =
this to HashMap<K, KotlinType>().also { it.putAll(fqnames.asSequence().map { (k, v) -> k to KotlinType(v) }) }
inline operator fun <reified K, reified V> TypedKey<Map<K, V>>.invoke(vararg vs: Pair<K, V>): Pair<TypedKey<Map<K, V>>, Map<K, V>> =
this to hashMapOf(*vs)