Fixes after review
This commit is contained in:
@@ -8,13 +8,27 @@ package kotlin.script.experimental.api
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.KType
|
||||
|
||||
class KotlinType(
|
||||
/**
|
||||
* A Kotlin type representation for using in the scripting API
|
||||
*/
|
||||
class KotlinType private constructor(
|
||||
val typeName: String,
|
||||
val fromClass: KClass<*>? = null
|
||||
val fromClass: KClass<*>?
|
||||
// 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<*>)
|
||||
/**
|
||||
* Constructs KotlinType from fully-qualified [qualifiedTypeName] in a dot-separated form, e.g. "org.acme.Outer.Inner"
|
||||
*/
|
||||
constructor(qualifiedTypeName: String) : this(qualifiedTypeName, null)
|
||||
|
||||
/**
|
||||
* Constructs KotlinType from reflected [kclass]
|
||||
*/
|
||||
constructor(kclass: KClass<*>) : this(kclass.qualifiedName!!, kclass)
|
||||
|
||||
// TODO: implement other approach for non-class types
|
||||
/**
|
||||
* Constructs KotlinType from reflected [ktype]
|
||||
*/
|
||||
constructor(type: KType) : this(type.classifier as KClass<*>)
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ private const val ILLEGAL_CONFIG_ANN_ARG =
|
||||
|
||||
open class AnnotationsBasedCompilationConfigurator(val environment: ScriptingEnvironment) : ScriptCompilationConfigurator {
|
||||
|
||||
override val defaultConfiguration by lazy {
|
||||
override val defaultConfiguration by lazy(LazyThreadSafetyMode.PUBLICATION) {
|
||||
val baseClass = environment.getScriptBaseClass(this)
|
||||
val cfg = baseClass.annotations.filterIsInstance(KotlinScriptDefaultCompilationConfiguration::class.java).flatMap { ann ->
|
||||
val params = try {
|
||||
|
||||
+1
-1
@@ -19,7 +19,7 @@ private const val ERROR_MSG_PREFIX = "Unable to construct script definition: "
|
||||
open class ScriptDefinitionFromAnnotatedBaseClass(val environment: ScriptingEnvironment) : ScriptDefinition {
|
||||
|
||||
private val getScriptingClass = environment.getOrNull(ScriptingEnvironmentProperties.getScriptingClass)
|
||||
?: throw IllegalArgumentException("${ERROR_MSG_PREFIX}Expecting 'getClass' parameter in the scripting environment")
|
||||
?: throw IllegalArgumentException("${ERROR_MSG_PREFIX}Expecting 'getScriptingClass' parameter in the scripting environment")
|
||||
|
||||
private val baseClass: KClass<*> = run {
|
||||
val baseClassType = environment.getOrNull(ScriptingEnvironmentProperties.baseClass)
|
||||
|
||||
@@ -33,13 +33,7 @@ open class ChainedPropertyBag private constructor(private val parent: ChainedPro
|
||||
|
||||
inline operator fun <reified T> get(key: TypedKey<T>): T = getRaw(key) as T
|
||||
|
||||
fun <T> getRaw(key: TypedKey<T>): Any? =
|
||||
when {
|
||||
data.containsKey(key) -> data[key]
|
||||
parent != null -> parent.getRaw(key)
|
||||
key.defaultValue != null -> key.defaultValue
|
||||
else -> throw IllegalArgumentException("Unknown key $key")
|
||||
}
|
||||
fun <T> getRaw(key: TypedKey<T>): Any? = getOrNullRaw(key) ?: throw IllegalArgumentException("Unknown key $key")
|
||||
|
||||
inline fun <reified T> getOrNull(key: TypedKey<T>): T? = getOrNullRaw(key)?.let { it as T }
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ class JvmGetScriptingClass : GetScriptingClass {
|
||||
|
||||
private var dependencies: List<ScriptDependency>? = null
|
||||
private var classLoader: ClassLoader? = null
|
||||
private var baseClassLoaderIsInitialized = false
|
||||
private var baseClassLoader: ClassLoader? = null
|
||||
|
||||
@Synchronized
|
||||
@@ -20,10 +21,11 @@ class JvmGetScriptingClass : GetScriptingClass {
|
||||
|
||||
// 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 fromClass = classType.fromClass
|
||||
if (fromClass != null) {
|
||||
if (fromClass.java.classLoader == null) return fromClass // root classloader
|
||||
val actualClassLoadersChain = generateSequence(contextClassloader) { it.parent }
|
||||
if (actualClassLoadersChain.any { it == fromClass.java.classLoader }) return fromClass
|
||||
}
|
||||
|
||||
val newDeps = environment.getOrNull(ScriptingEnvironmentProperties.configurationDependencies)
|
||||
@@ -33,9 +35,10 @@ class JvmGetScriptingClass : GetScriptingClass {
|
||||
if (newDeps != dependencies) throw IllegalArgumentException("scripting environment dependencies changed")
|
||||
}
|
||||
|
||||
if (baseClassLoader == null) {
|
||||
if (!baseClassLoaderIsInitialized) {
|
||||
baseClassLoader = contextClassloader
|
||||
} else {
|
||||
baseClassLoaderIsInitialized = true
|
||||
} else if (baseClassLoader != null) {
|
||||
val baseClassLoadersChain = generateSequence(baseClassLoader) { it.parent }
|
||||
if (baseClassLoadersChain.none { it == contextClassloader }) throw IllegalArgumentException("scripting class instantiation context changed")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user