Fixes after review

This commit is contained in:
Ilya Chernikov
2018-05-28 15:55:33 +02:00
parent 6218b2bcf6
commit 6fdb8746de
13 changed files with 63 additions and 68 deletions
@@ -26,7 +26,7 @@ abstract class KotlinScriptDefinitionAdapterFromNewAPIBase : KotlinScriptDefinit
protected abstract val scriptFileExtensionWithDot: String
open val baseClass: KClass<*> by lazy {
open val baseClass: KClass<*> by lazy(LazyThreadSafetyMode.PUBLICATION) {
getScriptingClass(scriptDefinition.compilationConfigurator.defaultConfiguration[ScriptingEnvironmentProperties.baseClass])
}
@@ -48,33 +48,30 @@ abstract class KotlinScriptDefinitionAdapterFromNewAPIBase : KotlinScriptDefinit
override val annotationsForSamWithReceivers: List<String>
get() = emptyList()
override val dependencyResolver: DependenciesResolver by lazy {
override val dependencyResolver: DependenciesResolver by lazy(LazyThreadSafetyMode.PUBLICATION) {
BridgeDependenciesResolver(scriptDefinition.compilationConfigurator)
}
override val acceptedAnnotations: List<KClass<out Annotation>> by lazy {
val annNames =
scriptDefinition.compilationConfigurator.defaultConfiguration.getOrNull(ScriptCompileConfigurationProperties.refineConfigurationOnAnnotations)
?: emptyList()
annNames.map { getScriptingClass(it) as KClass<out Annotation> }
override val acceptedAnnotations: List<KClass<out Annotation>> by lazy(LazyThreadSafetyMode.PUBLICATION) {
scriptDefinition.compilationConfigurator.defaultConfiguration.getOrNull(ScriptCompileConfigurationProperties.refineConfigurationOnAnnotations)
.orEmpty()
.map { getScriptingClass(it) as KClass<out Annotation> }
}
override val implicitReceivers: List<KType> by lazy {
val rcNames =
scriptDefinition.compilationConfigurator.defaultConfiguration.getOrNull(ScriptCompileConfigurationProperties.scriptImplicitReceivers)
?: emptyList()
rcNames.map { getScriptingClass(it).starProjectedType }
override val implicitReceivers: List<KType> by lazy(LazyThreadSafetyMode.PUBLICATION) {
scriptDefinition.compilationConfigurator.defaultConfiguration.getOrNull(ScriptCompileConfigurationProperties.scriptImplicitReceivers)
.orEmpty()
.map { getScriptingClass(it).starProjectedType }
}
override val environmentVariables: List<Pair<String, KType>> by lazy {
override val environmentVariables: List<Pair<String, KType>> by lazy(LazyThreadSafetyMode.PUBLICATION) {
scriptDefinition.compilationConfigurator.defaultConfiguration.getOrNull(ScriptCompileConfigurationProperties.contextVariables)
?.map { (k, v) -> k to getScriptingClass(v).starProjectedType }
?: emptyList()
?.map { (k, v) -> k to getScriptingClass(v).starProjectedType }.orEmpty()
}
override val additionalCompilerArguments: List<String>
get() = scriptDefinition.compilationConfigurator.defaultConfiguration.getOrNull(ScriptCompileConfigurationProperties.compilerOptions)
?: emptyList()
.orEmpty()
override val scriptExpectedLocations: List<ScriptExpectedLocation> =
listOf(
@@ -82,7 +79,7 @@ abstract class KotlinScriptDefinitionAdapterFromNewAPIBase : KotlinScriptDefinit
ScriptExpectedLocation.TestsOnly
)
private val scriptingClassGetter by lazy {
private val scriptingClassGetter by lazy(LazyThreadSafetyMode.PUBLICATION) {
scriptDefinition.properties.getOrNull(ScriptingEnvironmentProperties.getScriptingClass)
?: throw IllegalArgumentException("Expecting 'getScriptingClass' property in the scripting environment")
}
@@ -34,7 +34,7 @@ class LazyScriptDefinitionFromDiscoveredClass internal constructor(
messageCollector: MessageCollector
) : this(loadAnnotationsFromClass(classBytes), className, classpath, messageCollector)
override val scriptDefinition: ScriptDefinition by lazy {
override val scriptDefinition: ScriptDefinition by lazy(LazyThreadSafetyMode.PUBLICATION) {
messageCollector.report(
CompilerMessageSeverity.LOGGING,
"Configure scripting: loading script definition class $className using classpath $classpath\n. ${Thread.currentThread().stackTrace}"
@@ -59,7 +59,7 @@ class LazyScriptDefinitionFromDiscoveredClass internal constructor(
}
}
override val scriptFileExtensionWithDot: String by lazy {
override val scriptFileExtensionWithDot: String by lazy(LazyThreadSafetyMode.PUBLICATION) {
val ext = annotationsFromAsm.find { it.name == KotlinScriptFileExtension::class.simpleName!! }?.args?.first()
?: scriptDefinition.properties.let {
it.getOrNull(ScriptDefinitionProperties.fileExtension) ?: "kts"
@@ -67,7 +67,7 @@ class LazyScriptDefinitionFromDiscoveredClass internal constructor(
".$ext"
}
override val name: String by lazy {
override val name: String by lazy(LazyThreadSafetyMode.PUBLICATION) {
annotationsFromAsm.find { it.name == KotlinScript::class.simpleName!! }?.args?.first()
?: super.name
}
@@ -52,7 +52,7 @@ internal fun discoverScriptTemplatesInClasspath(
messageCollector: MessageCollector
): Sequence<KotlinScriptDefinition> = buildSequence {
// TODO: try to find a way to reduce classpath (and classloader) to minimal one needed to load script definition and its dependencies
val classLoader by lazy {
val classLoader by lazy(LazyThreadSafetyMode.PUBLICATION) {
URLClassLoader(classpath.map { it.toURI().toURL() }.toTypedArray(), baseClassLoader)
}
for (dep in classpath) {
@@ -89,10 +89,10 @@ internal fun discoverScriptTemplatesInClasspath(
dep.isDirectory -> {
val dir = File(dep, SCRIPT_DEFINITION_MARKERS_PATH)
if (dir.isDirectory) {
val templateClasspath by lazy {
val templateClasspath by lazy(LazyThreadSafetyMode.PUBLICATION) {
listOf(dep) + defaultScriptDefinitionClasspath
}
val classLoader by lazy {
val classLoader by lazy(LazyThreadSafetyMode.PUBLICATION) {
URLClassLoader(templateClasspath.map { it.toURI().toURL() }.toTypedArray(), baseClassLoader)
}
dir.listFiles().forEach { templateClassNmae ->
@@ -154,10 +154,10 @@ internal fun loadScriptTemplatesFromClasspath(
}
// then searching the remaining templates in the supplied classpath
if (templatesLeftToFind.isNotEmpty()) {
val templateClasspath by lazy {
val templateClasspath by lazy(LazyThreadSafetyMode.PUBLICATION) {
classpath + dependenciesClasspath
}
val classLoader by lazy {
val classLoader by lazy(LazyThreadSafetyMode.PUBLICATION) {
URLClassLoader(templateClasspath.map { it.toURI().toURL() }.toTypedArray(), baseClassLoader)
}
for (dep in classpath) {
@@ -5,12 +5,7 @@
package org.jetbrains.kotlin.scripting.compiler.plugin
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.org.objectweb.asm.AnnotationVisitor
import org.jetbrains.org.objectweb.asm.ClassReader
import org.jetbrains.org.objectweb.asm.ClassVisitor
import org.jetbrains.org.objectweb.asm.Opcodes
import org.jetbrains.org.objectweb.asm.*
internal class BinAnnData(
val name: String,
@@ -25,7 +20,7 @@ private class TemplateAnnotationVisitor(val anns: ArrayList<BinAnnData> = arrayL
private class TemplateClassVisitor(val annVisitor: TemplateAnnotationVisitor) : ClassVisitor(Opcodes.ASM5) {
override fun visitAnnotation(desc: String, visible: Boolean): AnnotationVisitor {
val shortName = jvmDescToClassId(desc).shortClassName.asString()
val shortName = Type.getType(desc).internalName.substringAfterLast("/")
if (shortName.startsWith("KotlinScript")) {
annVisitor.anns.add(BinAnnData(shortName))
}
@@ -33,13 +28,6 @@ private class TemplateClassVisitor(val annVisitor: TemplateAnnotationVisitor) :
}
}
private fun jvmDescToClassId(desc: String): ClassId {
assert(desc.startsWith("L") && desc.endsWith(";")) { "Not a JVM descriptor: $desc" }
val name = desc.substring(1, desc.length - 1)
val cid = ClassId.topLevel(FqName(name.replace('/', '.')))
return cid
}
internal fun loadAnnotationsFromClass(fileContents: ByteArray): ArrayList<BinAnnData> {
val visitor =