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:
@@ -235,9 +235,11 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
|
|||||||
if (!explicitOrLoadedScriptingPlugin) {
|
if (!explicitOrLoadedScriptingPlugin) {
|
||||||
val libPath = paths?.libPath?.takeIf { it.exists() } ?: File(".")
|
val libPath = paths?.libPath?.takeIf { it.exists() } ?: File(".")
|
||||||
with(PathUtil) {
|
with(PathUtil) {
|
||||||
val jars = arrayOf(KOTLIN_SCRIPTING_COMPILER_PLUGIN_JAR, KOTLIN_SCRIPTING_COMMON_JAR, KOTLIN_SCRIPTING_JVM_JAR)
|
val jars = arrayOf(
|
||||||
.mapNotNull { File(libPath, it).takeIf { it.exists() }?.canonicalPath }
|
KOTLIN_SCRIPTING_COMPILER_PLUGIN_JAR, KOTLIN_SCRIPTING_COMMON_JAR,
|
||||||
if (jars.size == 3) {
|
KOTLIN_SCRIPTING_JVM_JAR, KOTLIN_SCRIPTING_MISC_JAR
|
||||||
|
).mapNotNull { File(libPath, it).takeIf { it.exists() }?.canonicalPath }
|
||||||
|
if (jars.size == 4) {
|
||||||
pluginClasspaths = jars + pluginClasspaths
|
pluginClasspaths = jars + pluginClasspaths
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.compiler.plugin.*
|
|||||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
import java.net.URLClassLoader
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
object PluginCliParser {
|
object PluginCliParser {
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ import org.jetbrains.kotlin.test.InTextDirectivesUtils
|
|||||||
import org.jetbrains.kotlin.test.TestJdkKind
|
import org.jetbrains.kotlin.test.TestJdkKind
|
||||||
import org.junit.Assert
|
import org.junit.Assert
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import kotlin.reflect.full.starProjectedType
|
|
||||||
import kotlin.script.experimental.annotations.KotlinScript
|
import kotlin.script.experimental.annotations.KotlinScript
|
||||||
import kotlin.script.experimental.annotations.KotlinScriptDefaultCompilationConfiguration
|
import kotlin.script.experimental.annotations.KotlinScriptDefaultCompilationConfiguration
|
||||||
|
import kotlin.script.experimental.api.KotlinType
|
||||||
import kotlin.script.experimental.api.ScriptCompileConfigurationProperties
|
import kotlin.script.experimental.api.ScriptCompileConfigurationProperties
|
||||||
import kotlin.script.experimental.util.TypedKey
|
import kotlin.script.experimental.util.TypedKey
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ abstract class AbstractCustomScriptCodegenTest : CodegenTestCase() {
|
|||||||
|
|
||||||
override fun updateConfiguration(configuration: CompilerConfiguration) {
|
override fun updateConfiguration(configuration: CompilerConfiguration) {
|
||||||
if (scriptDefinitions.isNotEmpty()) {
|
if (scriptDefinitions.isNotEmpty()) {
|
||||||
configureScriptDefinitions(scriptDefinitions, configuration, MessageCollector.NONE, emptyMap())
|
configureScriptDefinitions(scriptDefinitions, configuration, this::class.java.classLoader, MessageCollector.NONE, emptyMap())
|
||||||
}
|
}
|
||||||
|
|
||||||
configuration.addJvmClasspathRoots(additionalDependencies.orEmpty())
|
configuration.addJvmClasspathRoots(additionalDependencies.orEmpty())
|
||||||
@@ -116,7 +116,7 @@ abstract class AbstractCustomScriptCodegenTest : CodegenTestCase() {
|
|||||||
|
|
||||||
object TestScriptWithReceiversConfiguration : ArrayList<Pair<TypedKey<*>, Any?>>(
|
object TestScriptWithReceiversConfiguration : ArrayList<Pair<TypedKey<*>, Any?>>(
|
||||||
listOf(
|
listOf(
|
||||||
ScriptCompileConfigurationProperties.scriptImplicitReceivers to listOf(String::class.starProjectedType)
|
ScriptCompileConfigurationProperties.scriptImplicitReceivers to listOf(KotlinType(String::class))
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -127,7 +127,7 @@ abstract class TestScriptWithReceivers
|
|||||||
|
|
||||||
object TestScriptWithSimpleEnvVarsConfiguration : ArrayList<Pair<TypedKey<*>, Any?>>(
|
object TestScriptWithSimpleEnvVarsConfiguration : ArrayList<Pair<TypedKey<*>, Any?>>(
|
||||||
listOf(
|
listOf(
|
||||||
ScriptCompileConfigurationProperties.contextVariables to mapOf("stringVar1" to String::class.starProjectedType)
|
ScriptCompileConfigurationProperties.contextVariables to mapOf("stringVar1" to KotlinType(String::class))
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.cli.AbstractCliTest
|
|||||||
import org.jetbrains.kotlin.codegen.*
|
import org.jetbrains.kotlin.codegen.*
|
||||||
import org.jetbrains.kotlin.codegen.defaultConstructor.AbstractDefaultArgumentsReflectionTest
|
import org.jetbrains.kotlin.codegen.defaultConstructor.AbstractDefaultArgumentsReflectionTest
|
||||||
import org.jetbrains.kotlin.codegen.flags.AbstractWriteFlagsTest
|
import org.jetbrains.kotlin.codegen.flags.AbstractWriteFlagsTest
|
||||||
|
import org.jetbrains.kotlin.codegen.AbstractCustomScriptCodegenTest
|
||||||
import org.jetbrains.kotlin.codegen.ir.AbstractIrBlackBoxCodegenTest
|
import org.jetbrains.kotlin.codegen.ir.AbstractIrBlackBoxCodegenTest
|
||||||
import org.jetbrains.kotlin.codegen.ir.AbstractIrBlackBoxInlineCodegenTest
|
import org.jetbrains.kotlin.codegen.ir.AbstractIrBlackBoxInlineCodegenTest
|
||||||
import org.jetbrains.kotlin.codegen.ir.AbstractIrCompileKotlinAgainstInlineKotlinTest
|
import org.jetbrains.kotlin.codegen.ir.AbstractIrCompileKotlinAgainstInlineKotlinTest
|
||||||
|
|||||||
@@ -59,6 +59,7 @@ object PathUtil {
|
|||||||
const val KOTLIN_JAVA_SCRIPT_RUNTIME_JAR = "kotlin-script-runtime.jar"
|
const val KOTLIN_JAVA_SCRIPT_RUNTIME_JAR = "kotlin-script-runtime.jar"
|
||||||
const val KOTLIN_SCRIPTING_COMMON_JAR = "kotlin-scripting-common.jar"
|
const val KOTLIN_SCRIPTING_COMMON_JAR = "kotlin-scripting-common.jar"
|
||||||
const val KOTLIN_SCRIPTING_JVM_JAR = "kotlin-scripting-jvm.jar"
|
const val KOTLIN_SCRIPTING_JVM_JAR = "kotlin-scripting-jvm.jar"
|
||||||
|
const val KOTLIN_SCRIPTING_MISC_JAR = "kotlin-scripting-misc.jar"
|
||||||
const val KOTLIN_SCRIPTING_COMPILER_PLUGIN_JAR = "kotlin-scripting-compiler.jar"
|
const val KOTLIN_SCRIPTING_COMPILER_PLUGIN_JAR = "kotlin-scripting-compiler.jar"
|
||||||
|
|
||||||
const val KOTLIN_TEST_NAME = "kotlin-test"
|
const val KOTLIN_TEST_NAME = "kotlin-test"
|
||||||
|
|||||||
+18
-14
@@ -33,7 +33,6 @@ import org.jetbrains.kotlin.idea.caches.project.SdkInfo
|
|||||||
import org.jetbrains.kotlin.idea.caches.project.getScriptRelatedModuleInfo
|
import org.jetbrains.kotlin.idea.caches.project.getScriptRelatedModuleInfo
|
||||||
import org.jetbrains.kotlin.script.*
|
import org.jetbrains.kotlin.script.*
|
||||||
import org.jetbrains.kotlin.scripting.compiler.plugin.KotlinScriptDefinitionAdapterFromNewAPI
|
import org.jetbrains.kotlin.scripting.compiler.plugin.KotlinScriptDefinitionAdapterFromNewAPI
|
||||||
import org.jetbrains.kotlin.script.*
|
|
||||||
import org.jetbrains.kotlin.idea.util.ProjectRootsUtil.isInContent
|
import org.jetbrains.kotlin.idea.util.ProjectRootsUtil.isInContent
|
||||||
import org.jetbrains.kotlin.psi.KtFile
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
import org.jetbrains.kotlin.script.KotlinScriptDefinition
|
import org.jetbrains.kotlin.script.KotlinScriptDefinition
|
||||||
@@ -49,12 +48,15 @@ import java.net.URLClassLoader
|
|||||||
import kotlin.concurrent.write
|
import kotlin.concurrent.write
|
||||||
import kotlin.script.dependencies.Environment
|
import kotlin.script.dependencies.Environment
|
||||||
import kotlin.script.dependencies.ScriptContents
|
import kotlin.script.dependencies.ScriptContents
|
||||||
|
import kotlin.script.experimental.api.KotlinType
|
||||||
import kotlin.script.experimental.api.ScriptingEnvironment
|
import kotlin.script.experimental.api.ScriptingEnvironment
|
||||||
import kotlin.script.experimental.api.ScriptingEnvironmentProperties
|
import kotlin.script.experimental.api.ScriptingEnvironmentProperties
|
||||||
import kotlin.script.experimental.definitions.ScriptDefinitionFromAnnotatedBaseClass
|
import kotlin.script.experimental.definitions.ScriptDefinitionFromAnnotatedBaseClass
|
||||||
import kotlin.script.experimental.dependencies.DependenciesResolver
|
import kotlin.script.experimental.dependencies.DependenciesResolver
|
||||||
import kotlin.script.experimental.dependencies.ScriptDependencies
|
import kotlin.script.experimental.dependencies.ScriptDependencies
|
||||||
import kotlin.script.experimental.dependencies.asSuccess
|
import kotlin.script.experimental.dependencies.asSuccess
|
||||||
|
import kotlin.script.experimental.jvm.JvmDependency
|
||||||
|
import kotlin.script.experimental.jvm.JvmGetScriptingClass
|
||||||
import kotlin.script.experimental.location.ScriptExpectedLocation
|
import kotlin.script.experimental.location.ScriptExpectedLocation
|
||||||
import kotlin.script.templates.standard.ScriptTemplateWithArgs
|
import kotlin.script.templates.standard.ScriptTemplateWithArgs
|
||||||
|
|
||||||
@@ -161,15 +163,17 @@ fun loadDefinitionsFromTemplates(
|
|||||||
* i.e. gradle resolver may depend on some jars that 'built.gradle.kts' files should not depend on.
|
* i.e. gradle resolver may depend on some jars that 'built.gradle.kts' files should not depend on.
|
||||||
*/
|
*/
|
||||||
additionalResolverClasspath: List<File> = emptyList()
|
additionalResolverClasspath: List<File> = emptyList()
|
||||||
): List<KotlinScriptDefinition> = try {
|
): List<KotlinScriptDefinition> {
|
||||||
val classpath = templateClasspath + additionalResolverClasspath
|
val classpath = templateClasspath + additionalResolverClasspath
|
||||||
LOG.info("[kts] loading script definitions $templateClassNames using cp: ${classpath.joinToString(File.pathSeparator)}")
|
LOG.info("[kts] loading script definitions $templateClassNames using cp: ${classpath.joinToString(File.pathSeparator)}")
|
||||||
val baseLoader = ScriptDefinitionContributor::class.java.classLoader
|
val baseLoader = ScriptDefinitionContributor::class.java.classLoader
|
||||||
val loader = if (classpath.isEmpty()) baseLoader else URLClassLoader(classpath.map { it.toURI().toURL() }.toTypedArray(), baseLoader)
|
val loader = if (classpath.isEmpty()) baseLoader else URLClassLoader(classpath.map { it.toURI().toURL() }.toTypedArray(), baseLoader)
|
||||||
|
|
||||||
templateClassNames.mapNotNull {
|
return templateClassNames.mapNotNull { templateClassName ->
|
||||||
try {
|
try {
|
||||||
val template = loader.loadClass(it).kotlin
|
// TODO: drop class loading here - it should be handled downstream
|
||||||
|
// as a compatibility measure, the asm based reading of annotations should be implemented to filter classes before classloading
|
||||||
|
val template = loader.loadClass(templateClassName).kotlin
|
||||||
when {
|
when {
|
||||||
template.annotations.firstIsInstanceOrNull<org.jetbrains.kotlin.script.ScriptTemplateDefinition>() != null ||
|
template.annotations.firstIsInstanceOrNull<org.jetbrains.kotlin.script.ScriptTemplateDefinition>() != null ||
|
||||||
template.annotations.firstIsInstanceOrNull<kotlin.script.templates.ScriptTemplateDefinition>() != null -> {
|
template.annotations.firstIsInstanceOrNull<kotlin.script.templates.ScriptTemplateDefinition>() != null -> {
|
||||||
@@ -181,7 +185,13 @@ fun loadDefinitionsFromTemplates(
|
|||||||
}
|
}
|
||||||
template.annotations.firstIsInstanceOrNull<kotlin.script.experimental.annotations.KotlinScript>() != null -> {
|
template.annotations.firstIsInstanceOrNull<kotlin.script.experimental.annotations.KotlinScript>() != null -> {
|
||||||
KotlinScriptDefinitionAdapterFromNewAPI(
|
KotlinScriptDefinitionAdapterFromNewAPI(
|
||||||
ScriptDefinitionFromAnnotatedBaseClass(ScriptingEnvironment(ScriptingEnvironmentProperties.baseClass to template))
|
ScriptDefinitionFromAnnotatedBaseClass(
|
||||||
|
ScriptingEnvironment(
|
||||||
|
ScriptingEnvironmentProperties.baseClass to KotlinType(template),
|
||||||
|
ScriptingEnvironmentProperties.configurationDependencies to listOf(JvmDependency(classpath)),
|
||||||
|
ScriptingEnvironmentProperties.getScriptingClass to JvmGetScriptingClass()
|
||||||
|
)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
@@ -192,19 +202,13 @@ fun loadDefinitionsFromTemplates(
|
|||||||
} catch (e: ClassNotFoundException) {
|
} catch (e: ClassNotFoundException) {
|
||||||
// Assuming that direct ClassNotFoundException is the result of versions mismatch and missing subsystems, e.g. gradle
|
// Assuming that direct ClassNotFoundException is the result of versions mismatch and missing subsystems, e.g. gradle
|
||||||
// so, it only results in warning, while other errors are severe misconfigurations, resulting it user-visible error
|
// so, it only results in warning, while other errors are severe misconfigurations, resulting it user-visible error
|
||||||
LOG.warn("[kts] cannot load script definition class $it", e)
|
LOG.warn("[kts] cannot load script definition class $templateClassName", e)
|
||||||
null
|
null
|
||||||
} catch (e: NoClassDefFoundError) {
|
} catch (e: Throwable) {
|
||||||
LOG.error("[kts] cannot load script definition class $it", e)
|
LOG.error("[kts] cannot load script definition class $templateClassName", e)
|
||||||
null
|
|
||||||
} catch (e: InvocationTargetException) {
|
|
||||||
LOG.error("[kts] cannot load script definition class $it", e)
|
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (ex: Throwable) {
|
|
||||||
// TODO: review exception handling
|
|
||||||
emptyList()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ScriptDefinitionContributor {
|
interface ScriptDefinitionContributor {
|
||||||
|
|||||||
+6
-1
@@ -13,13 +13,18 @@ import kotlin.script.experimental.definitions.ScriptDefinitionFromAnnotatedBaseC
|
|||||||
import kotlin.script.experimental.host.toScriptSource
|
import kotlin.script.experimental.host.toScriptSource
|
||||||
import kotlin.script.experimental.jvm.DummyCompiledJvmScriptCache
|
import kotlin.script.experimental.jvm.DummyCompiledJvmScriptCache
|
||||||
import kotlin.script.experimental.jvm.JvmBasicScriptingHost
|
import kotlin.script.experimental.jvm.JvmBasicScriptingHost
|
||||||
|
import kotlin.script.experimental.jvm.JvmGetScriptingClass
|
||||||
import kotlin.script.experimental.jvm.JvmScriptCompiler
|
import kotlin.script.experimental.jvm.JvmScriptCompiler
|
||||||
import kotlin.script.experimental.jvmhost.impl.KJVMCompilerImpl
|
import kotlin.script.experimental.jvmhost.impl.KJVMCompilerImpl
|
||||||
|
import kotlin.script.experimental.misc.*
|
||||||
|
|
||||||
fun evalFile(scriptFile: File): ResultWithDiagnostics<EvaluationResult> {
|
fun evalFile(scriptFile: File): ResultWithDiagnostics<EvaluationResult> {
|
||||||
val scriptCompiler = JvmScriptCompiler(KJVMCompilerImpl(), DummyCompiledJvmScriptCache())
|
val scriptCompiler = JvmScriptCompiler(KJVMCompilerImpl(), DummyCompiledJvmScriptCache())
|
||||||
val scriptDefinition = ScriptDefinitionFromAnnotatedBaseClass(
|
val scriptDefinition = ScriptDefinitionFromAnnotatedBaseClass(
|
||||||
ScriptingEnvironment(ScriptingEnvironmentProperties.baseClass to MyScriptWithMavenDeps::class)
|
ScriptingEnvironment(
|
||||||
|
ScriptingEnvironmentProperties.baseClass<MyScriptWithMavenDeps>(),
|
||||||
|
ScriptingEnvironmentProperties.getScriptingClass(JvmGetScriptingClass())
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
val host = JvmBasicScriptingHost(
|
val host = JvmBasicScriptingHost(
|
||||||
|
|||||||
+4
-1
@@ -25,7 +25,10 @@ val myJvmConfigParams = jvmJavaHomeParams + with(ScriptCompileConfigurationPrope
|
|||||||
fun evalFile(scriptFile: File): ResultWithDiagnostics<EvaluationResult> {
|
fun evalFile(scriptFile: File): ResultWithDiagnostics<EvaluationResult> {
|
||||||
val scriptCompiler = JvmScriptCompiler(KJVMCompilerImpl(), DummyCompiledJvmScriptCache())
|
val scriptCompiler = JvmScriptCompiler(KJVMCompilerImpl(), DummyCompiledJvmScriptCache())
|
||||||
val scriptDefinition = ScriptDefinitionFromAnnotatedBaseClass(
|
val scriptDefinition = ScriptDefinitionFromAnnotatedBaseClass(
|
||||||
ScriptingEnvironment(ScriptingEnvironmentProperties.baseClass to MyScript::class)
|
ScriptingEnvironment(
|
||||||
|
ScriptingEnvironmentProperties.baseClass<MyScript>(),
|
||||||
|
ScriptingEnvironmentProperties.getScriptingClass(JvmGetScriptingClass())
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
val host = JvmBasicScriptingHost(
|
val host = JvmBasicScriptingHost(
|
||||||
|
|||||||
@@ -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
|
package kotlin.script.experimental.api
|
||||||
|
|
||||||
import kotlin.reflect.KClass
|
|
||||||
import kotlin.reflect.KType
|
|
||||||
import kotlin.script.experimental.util.typedKey
|
import kotlin.script.experimental.util.typedKey
|
||||||
|
|
||||||
object ScriptCompileConfigurationProperties {
|
object ScriptCompileConfigurationProperties {
|
||||||
@@ -19,9 +17,9 @@ object ScriptCompileConfigurationProperties {
|
|||||||
|
|
||||||
val scriptBodyTarget by typedKey<ScriptBodyTarget>()
|
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>>()
|
val defaultImports by typedKey<List<String>>()
|
||||||
|
|
||||||
@@ -31,15 +29,15 @@ object ScriptCompileConfigurationProperties {
|
|||||||
|
|
||||||
val dependencies by typedKey<List<ScriptDependency>>()
|
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 compilerOptions by typedKey<List<String>>() // Q: CommonCompilerOptions instead?
|
||||||
|
|
||||||
val refineBeforeParsing by typedKey<Boolean>() // default: false
|
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>>()
|
val refineConfigurationOnSections by typedKey<List<String>>()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,6 @@
|
|||||||
package kotlin.script.experimental.api
|
package kotlin.script.experimental.api
|
||||||
|
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
import kotlin.reflect.KClass
|
|
||||||
import kotlin.reflect.KType
|
|
||||||
|
|
||||||
interface ScriptSource {
|
interface ScriptSource {
|
||||||
val location: URL?
|
val location: URL?
|
||||||
|
|||||||
+29
-1
@@ -14,6 +14,34 @@ typealias ScriptingEnvironment = ChainedPropertyBag
|
|||||||
object ScriptingEnvironmentProperties {
|
object ScriptingEnvironmentProperties {
|
||||||
|
|
||||||
// required by definitions that extract data from script base class annotations
|
// 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.api.*
|
||||||
import kotlin.script.experimental.util.TypedKey
|
import kotlin.script.experimental.util.TypedKey
|
||||||
|
|
||||||
|
|
||||||
private const val ILLEGAL_CONFIG_ANN_ARG =
|
private const val ILLEGAL_CONFIG_ANN_ARG =
|
||||||
"Illegal argument to KotlinScriptDefaultCompilationConfiguration annotation: expecting List-derived object or default-constructed class of configuration parameters"
|
"Illegal argument to KotlinScriptDefaultCompilationConfiguration annotation: expecting List-derived object or default-constructed class of configuration parameters"
|
||||||
|
|
||||||
open class AnnotationsBasedCompilationConfigurator(val environment: ScriptingEnvironment) : ScriptCompilationConfigurator {
|
open class AnnotationsBasedCompilationConfigurator(val environment: ScriptingEnvironment) : ScriptCompilationConfigurator {
|
||||||
|
|
||||||
override val defaultConfiguration = run {
|
override val defaultConfiguration by lazy {
|
||||||
val base = environment[ScriptingEnvironmentProperties.baseClass]
|
val baseClass = environment.getScriptBaseClass(this)
|
||||||
val cfg = base.annotations.filterIsInstance(KotlinScriptDefaultCompilationConfiguration::class.java).flatMap { ann ->
|
val cfg = baseClass.annotations.filterIsInstance(KotlinScriptDefaultCompilationConfiguration::class.java).flatMap { ann ->
|
||||||
val params = try {
|
val params = try {
|
||||||
ann.compilationConfiguration.objectInstance ?: ann.compilationConfiguration.createInstance()
|
ann.compilationConfiguration.objectInstance ?: ann.compilationConfiguration.createInstance()
|
||||||
} catch (e: Throwable) {
|
} 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 {
|
open class ScriptDefinitionFromAnnotatedBaseClass(val environment: ScriptingEnvironment) : ScriptDefinition {
|
||||||
|
|
||||||
private val baseClass: KClass<*> = environment.getOrNull(ScriptingEnvironmentProperties.baseClass)
|
private val getScriptingClass = environment.getOrNull(ScriptingEnvironmentProperties.getScriptingClass)
|
||||||
?: throw IllegalArgumentException("${ERROR_MSG_PREFIX}Expecting baseClass parameter in the scripting environment")
|
?: 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>()
|
private val mainAnnotation = baseClass.findAnnotation<KotlinScript>()
|
||||||
?: throw IllegalArgumentException("${ERROR_MSG_PREFIX}Expecting KotlinScript annotation on the $baseClass")
|
?: 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? =
|
private val explicitDefinition: ScriptDefinition? =
|
||||||
baseClass.findAnnotation<KotlinScriptDefinition>()?.definition.takeIf { it != this::class }?.let { it.instantiateScriptHandler() }
|
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>>()
|
val toAdd = arrayListOf<Pair<TypedKey<*>, Any>>()
|
||||||
baseClass.findAnnotation<KotlinScriptFileExtension>()?.let { toAdd += ScriptDefinitionProperties.fileExtension to it }
|
baseClass.findAnnotation<KotlinScriptFileExtension>()?.let {
|
||||||
if (properties.getOrNull(ScriptDefinitionProperties.name) == null) {
|
toAdd += ScriptDefinitionProperties.fileExtension to it.extension
|
||||||
|
}
|
||||||
|
if (baseProperties.getOrNull(ScriptDefinitionProperties.name) == null) {
|
||||||
toAdd += ScriptDefinitionProperties.name to mainAnnotation.name
|
toAdd += ScriptDefinitionProperties.name to mainAnnotation.name
|
||||||
}
|
}
|
||||||
ScriptingEnvironment(properties, toAdd)
|
if (toAdd.isEmpty()) baseProperties
|
||||||
|
else ScriptingEnvironment(baseProperties, toAdd)
|
||||||
}
|
}
|
||||||
|
|
||||||
override val compilationConfigurator =
|
override val compilationConfigurator =
|
||||||
@@ -47,10 +61,17 @@ open class ScriptDefinitionFromAnnotatedBaseClass(val environment: ScriptingEnvi
|
|||||||
?: DummyEvaluator::class.instantiateScriptHandler()
|
?: DummyEvaluator::class.instantiateScriptHandler()
|
||||||
|
|
||||||
private fun <T : Any> KClass<T>.instantiateScriptHandler(): T {
|
private fun <T : Any> KClass<T>.instantiateScriptHandler(): T {
|
||||||
val fqn = this.qualifiedName!!
|
val klass: KClass<T> = try {
|
||||||
val klass: KClass<T> = (baseClass.java.classLoader.loadClass(fqn) as Class<T>).kotlin
|
getScriptingClass(KotlinType(this), this@ScriptDefinitionFromAnnotatedBaseClass::class, environment) as KClass<T>
|
||||||
// TODO: fix call after deciding on constructor parameters
|
} catch (e: Throwable) {
|
||||||
return klass.objectInstance ?: klass.primaryConstructor!!.call(environment)
|
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
|
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> {
|
class TypedKeyDelegate<T>(val defaultValue: T? = null) {
|
||||||
operator fun getValue(thisRef: Any?, property: KProperty<*>): TypedKey<T> = TypedKey(property.name)
|
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?>) {
|
open class ChainedPropertyBag private constructor(private val parent: ChainedPropertyBag?, private val data: Map<TypedKey<*>, Any?>) {
|
||||||
constructor(parent: ChainedPropertyBag? = null, pairs: Iterable<Pair<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)
|
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 {
|
when {
|
||||||
data.containsKey(key) -> data[key]
|
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")
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+6
-3
@@ -236,10 +236,13 @@ internal class BridgeScriptDefinition(
|
|||||||
scriptCompilerConfiguration: ScriptCompileConfiguration,
|
scriptCompilerConfiguration: ScriptCompileConfiguration,
|
||||||
scriptConfigurator: ScriptCompilationConfigurator?,
|
scriptConfigurator: ScriptCompilationConfigurator?,
|
||||||
updateClasspath: (List<File>) -> Unit
|
updateClasspath: (List<File>) -> Unit
|
||||||
) : KotlinScriptDefinition(scriptCompilerConfiguration[ScriptingEnvironmentProperties.baseClass] as KClass<out Any>) {
|
) : KotlinScriptDefinition(scriptCompilerConfiguration.getScriptBaseClass(BridgeScriptDefinition::class)) {
|
||||||
override val acceptedAnnotations =
|
override val acceptedAnnotations = run {
|
||||||
scriptCompilerConfiguration.getOrNull(ScriptCompileConfigurationProperties.refineConfigurationOnAnnotations)?.toList()
|
val cl = this::class.java.classLoader
|
||||||
|
scriptCompilerConfiguration.getOrNull(ScriptCompileConfigurationProperties.refineConfigurationOnAnnotations)
|
||||||
|
?.map { (cl.loadClass(it.typeName) as Class<out Annotation>).kotlin }
|
||||||
?: emptyList()
|
?: emptyList()
|
||||||
|
}
|
||||||
|
|
||||||
override val dependencyResolver: DependenciesResolver =
|
override val dependencyResolver: DependenciesResolver =
|
||||||
BridgeDependenciesResolver(scriptConfigurator, scriptCompilerConfiguration, updateClasspath)
|
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
|
package kotlin.script.experimental.misc
|
||||||
|
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
|
import kotlin.reflect.KType
|
||||||
|
import kotlin.script.experimental.api.KotlinType
|
||||||
import kotlin.script.experimental.util.TypedKey
|
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 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()
|
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>> =
|
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)
|
this to hashMapOf(*vs)
|
||||||
|
|
||||||
|
|||||||
@@ -65,7 +65,6 @@ dependencies {
|
|||||||
runtime project(':kotlin-reflect')
|
runtime project(':kotlin-reflect')
|
||||||
runtime project(':kotlin-scripting-common')
|
runtime project(':kotlin-scripting-common')
|
||||||
runtime project(':kotlin-scripting-compiler')
|
runtime project(':kotlin-scripting-compiler')
|
||||||
runtime project(':kotlin-scripting-jvm')
|
|
||||||
runtime project(path: ':kotlin-scripting-gradle', configuration: 'runtimeJar')
|
runtime project(path: ':kotlin-scripting-gradle', configuration: 'runtimeJar')
|
||||||
|
|
||||||
// com.android.tools.build:gradle has ~50 unneeded transitive dependencies
|
// com.android.tools.build:gradle has ~50 unneeded transitive dependencies
|
||||||
|
|||||||
+1
-1
@@ -185,7 +185,7 @@ public class ExecuteKotlinScriptMojo extends AbstractMojo {
|
|||||||
configuration.put(CommonConfigurationKeys.MODULE_NAME, JvmAbi.DEFAULT_MODULE_NAME);
|
configuration.put(CommonConfigurationKeys.MODULE_NAME, JvmAbi.DEFAULT_MODULE_NAME);
|
||||||
|
|
||||||
ScriptingCompilerConfigurationExtensionKt.configureScriptDefinitions(
|
ScriptingCompilerConfigurationExtensionKt.configureScriptDefinitions(
|
||||||
scriptTemplates, configuration, messageCollector, new HashMap<>()
|
scriptTemplates, configuration, this.getClass().getClassLoader(), messageCollector, new HashMap<>()
|
||||||
);
|
);
|
||||||
|
|
||||||
KotlinCoreEnvironment environment = KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES);
|
KotlinCoreEnvironment environment = KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES);
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ dependencies {
|
|||||||
compileOnly(project(":compiler:cli"))
|
compileOnly(project(":compiler:cli"))
|
||||||
compile(project(":kotlin-scripting-common"))
|
compile(project(":kotlin-scripting-common"))
|
||||||
compile(project(":kotlin-scripting-jvm"))
|
compile(project(":kotlin-scripting-jvm"))
|
||||||
|
compile(project(":kotlin-scripting-misc"))
|
||||||
|
compileOnly(project(":kotlin-reflect-api"))
|
||||||
compileOnly(intellijCoreDep()) { includeJars("intellij-core") }
|
compileOnly(intellijCoreDep()) { includeJars("intellij-core") }
|
||||||
compileOnly(intellijDep()) { includeJars("asm-all") }
|
compileOnly(intellijDep()) { includeJars("asm-all") }
|
||||||
|
|
||||||
|
|||||||
+28
-12
@@ -10,13 +10,11 @@ import org.jetbrains.kotlin.idea.KotlinFileType
|
|||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
import org.jetbrains.kotlin.name.NameUtils
|
import org.jetbrains.kotlin.name.NameUtils
|
||||||
import org.jetbrains.kotlin.psi.KtScript
|
import org.jetbrains.kotlin.psi.KtScript
|
||||||
import org.jetbrains.kotlin.script.*
|
import org.jetbrains.kotlin.script.KotlinScriptDefinition
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
import kotlin.reflect.KType
|
import kotlin.reflect.KType
|
||||||
import kotlin.script.experimental.api.ScriptCompileConfigurationProperties
|
import kotlin.reflect.full.starProjectedType
|
||||||
import kotlin.script.experimental.api.ScriptDefinition
|
import kotlin.script.experimental.api.*
|
||||||
import kotlin.script.experimental.api.ScriptDefinitionProperties
|
|
||||||
import kotlin.script.experimental.api.ScriptingEnvironmentProperties
|
|
||||||
import kotlin.script.experimental.dependencies.DependenciesResolver
|
import kotlin.script.experimental.dependencies.DependenciesResolver
|
||||||
import kotlin.script.experimental.jvm.impl.BridgeDependenciesResolver
|
import kotlin.script.experimental.jvm.impl.BridgeDependenciesResolver
|
||||||
import kotlin.script.experimental.location.ScriptExpectedLocation
|
import kotlin.script.experimental.location.ScriptExpectedLocation
|
||||||
@@ -28,8 +26,9 @@ abstract class KotlinScriptDefinitionAdapterFromNewAPIBase : KotlinScriptDefinit
|
|||||||
|
|
||||||
protected abstract val scriptFileExtensionWithDot: String
|
protected abstract val scriptFileExtensionWithDot: String
|
||||||
|
|
||||||
open val baseClass: KClass<*>
|
open val baseClass: KClass<*> by lazy {
|
||||||
get() = scriptDefinition.compilationConfigurator.defaultConfiguration[ScriptingEnvironmentProperties.baseClass]
|
getScriptingClass(scriptDefinition.compilationConfigurator.defaultConfiguration[ScriptingEnvironmentProperties.baseClass])
|
||||||
|
}
|
||||||
|
|
||||||
override val template: KClass<*> get() = baseClass
|
override val template: KClass<*> get() = baseClass
|
||||||
|
|
||||||
@@ -54,17 +53,22 @@ abstract class KotlinScriptDefinitionAdapterFromNewAPIBase : KotlinScriptDefinit
|
|||||||
}
|
}
|
||||||
|
|
||||||
override val acceptedAnnotations: List<KClass<out Annotation>> by lazy {
|
override val acceptedAnnotations: List<KClass<out Annotation>> by lazy {
|
||||||
scriptDefinition.compilationConfigurator.defaultConfiguration.getOrNull(ScriptCompileConfigurationProperties.refineConfigurationOnAnnotations)
|
val annNames =
|
||||||
?: emptyList()
|
scriptDefinition.compilationConfigurator.defaultConfiguration.getOrNull(ScriptCompileConfigurationProperties.refineConfigurationOnAnnotations)
|
||||||
|
?: emptyList()
|
||||||
|
annNames.map { getScriptingClass(it) as KClass<out Annotation> }
|
||||||
}
|
}
|
||||||
|
|
||||||
override val implicitReceivers: List<KType> by lazy {
|
override val implicitReceivers: List<KType> by lazy {
|
||||||
scriptDefinition.compilationConfigurator.defaultConfiguration.getOrNull(ScriptCompileConfigurationProperties.scriptImplicitReceivers)
|
val rcNames =
|
||||||
?: emptyList()
|
scriptDefinition.compilationConfigurator.defaultConfiguration.getOrNull(ScriptCompileConfigurationProperties.scriptImplicitReceivers)
|
||||||
|
?: emptyList()
|
||||||
|
rcNames.map { getScriptingClass(it).starProjectedType }
|
||||||
}
|
}
|
||||||
|
|
||||||
override val environmentVariables: List<Pair<String, KType>> by lazy {
|
override val environmentVariables: List<Pair<String, KType>> by lazy {
|
||||||
scriptDefinition.compilationConfigurator.defaultConfiguration.getOrNull(ScriptCompileConfigurationProperties.contextVariables)?.map { (k, v) -> k to v }
|
scriptDefinition.compilationConfigurator.defaultConfiguration.getOrNull(ScriptCompileConfigurationProperties.contextVariables)
|
||||||
|
?.map { (k, v) -> k to getScriptingClass(v).starProjectedType }
|
||||||
?: emptyList()
|
?: emptyList()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,6 +81,18 @@ abstract class KotlinScriptDefinitionAdapterFromNewAPIBase : KotlinScriptDefinit
|
|||||||
ScriptExpectedLocation.SourcesOnly,
|
ScriptExpectedLocation.SourcesOnly,
|
||||||
ScriptExpectedLocation.TestsOnly
|
ScriptExpectedLocation.TestsOnly
|
||||||
)
|
)
|
||||||
|
|
||||||
|
private val scriptingClassGetter by lazy {
|
||||||
|
scriptDefinition.properties.getOrNull(ScriptingEnvironmentProperties.getScriptingClass)
|
||||||
|
?: throw IllegalArgumentException("Expecting 'getScriptingClass' property in the scripting environment")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getScriptingClass(type: KotlinType) =
|
||||||
|
scriptingClassGetter(
|
||||||
|
type,
|
||||||
|
KotlinScriptDefinition::class, // Assuming that the KotlinScriptDefinition class is loaded in the proper classloader
|
||||||
|
scriptDefinition.properties
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+13
-56
@@ -12,33 +12,27 @@ package org.jetbrains.kotlin.scripting.compiler.plugin
|
|||||||
|
|
||||||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
|
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
|
||||||
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
|
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
|
||||||
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 java.io.File
|
import java.io.File
|
||||||
import java.net.URLClassLoader
|
|
||||||
import kotlin.script.experimental.annotations.KotlinScript
|
import kotlin.script.experimental.annotations.KotlinScript
|
||||||
import kotlin.script.experimental.annotations.KotlinScriptFileExtension
|
import kotlin.script.experimental.annotations.KotlinScriptFileExtension
|
||||||
import kotlin.script.experimental.api.*
|
import kotlin.script.experimental.api.*
|
||||||
import kotlin.script.experimental.definitions.ScriptDefinitionFromAnnotatedBaseClass
|
import kotlin.script.experimental.definitions.ScriptDefinitionFromAnnotatedBaseClass
|
||||||
|
import kotlin.script.experimental.jvm.JvmDependency
|
||||||
|
import kotlin.script.experimental.jvm.JvmGetScriptingClass
|
||||||
|
|
||||||
class LazyScriptDefinitionFromDiscoveredClass(
|
class LazyScriptDefinitionFromDiscoveredClass internal constructor(
|
||||||
classBytes: ByteArray,
|
private val annotationsFromAsm: ArrayList<BinAnnData>,
|
||||||
private val className: String,
|
private val className: String,
|
||||||
private val classpath: List<File>,
|
private val classpath: List<File>,
|
||||||
private val messageCollector: MessageCollector
|
private val messageCollector: MessageCollector
|
||||||
) : KotlinScriptDefinitionAdapterFromNewAPIBase() {
|
) : KotlinScriptDefinitionAdapterFromNewAPIBase() {
|
||||||
private val annotationsFromAsm = loadAnnotationsFromClass(classBytes)
|
|
||||||
|
|
||||||
private val classloader by lazy {
|
constructor(
|
||||||
// should use this cl to allow smooth interop with classes explicitly mentioned here, see e.g. scriptDefinition body
|
classBytes: ByteArray,
|
||||||
val parentClassloader = LazyScriptDefinitionFromDiscoveredClass::class.java.classLoader
|
className: String,
|
||||||
if (classpath.isEmpty()) parentClassloader
|
classpath: List<File>,
|
||||||
else URLClassLoader(classpath.map { it.toURI().toURL() }.toTypedArray(), parentClassloader)
|
messageCollector: MessageCollector
|
||||||
}
|
) : this(loadAnnotationsFromClass(classBytes), className, classpath, messageCollector)
|
||||||
|
|
||||||
override val scriptDefinition: ScriptDefinition by lazy {
|
override val scriptDefinition: ScriptDefinition by lazy {
|
||||||
messageCollector.report(
|
messageCollector.report(
|
||||||
@@ -46,10 +40,11 @@ class LazyScriptDefinitionFromDiscoveredClass(
|
|||||||
"Configure scripting: loading script definition class $className using classpath $classpath\n. ${Thread.currentThread().stackTrace}"
|
"Configure scripting: loading script definition class $className using classpath $classpath\n. ${Thread.currentThread().stackTrace}"
|
||||||
)
|
)
|
||||||
try {
|
try {
|
||||||
val cls = classloader.loadClass(className).kotlin
|
|
||||||
ScriptDefinitionFromAnnotatedBaseClass(
|
ScriptDefinitionFromAnnotatedBaseClass(
|
||||||
ScriptingEnvironment(
|
ScriptingEnvironment(
|
||||||
ScriptingEnvironmentProperties.baseClass to cls
|
ScriptingEnvironmentProperties.baseClass to KotlinType(className),
|
||||||
|
ScriptingEnvironmentProperties.configurationDependencies to listOf(JvmDependency(classpath)),
|
||||||
|
ScriptingEnvironmentProperties.getScriptingClass to JvmGetScriptingClass()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
} catch (ex: ClassNotFoundException) {
|
} catch (ex: ClassNotFoundException) {
|
||||||
@@ -86,41 +81,3 @@ object InvalidScriptDefinition : ScriptDefinition {
|
|||||||
override val evaluator: ScriptEvaluator<*>? = null
|
override val evaluator: ScriptEvaluator<*>? = null
|
||||||
}
|
}
|
||||||
|
|
||||||
private class BinAnnData(
|
|
||||||
val name: String,
|
|
||||||
val args: ArrayList<String> = arrayListOf()
|
|
||||||
)
|
|
||||||
|
|
||||||
private class TemplateAnnotationVisitor(val anns: ArrayList<BinAnnData> = arrayListOf()) : AnnotationVisitor(Opcodes.ASM5) {
|
|
||||||
override fun visit(name: String?, value: Any?) {
|
|
||||||
anns.last().args.add(value.toString())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class TemplateClassVisitor(val annVisitor: TemplateAnnotationVisitor) : ClassVisitor(Opcodes.ASM5) {
|
|
||||||
override fun visitAnnotation(desc: String, visible: Boolean): AnnotationVisitor {
|
|
||||||
val shortName = jvmDescToClassId(desc).shortClassName.asString()
|
|
||||||
if (shortName.startsWith("KotlinScript")) {
|
|
||||||
annVisitor.anns.add(BinAnnData(shortName))
|
|
||||||
}
|
|
||||||
return annVisitor
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun loadAnnotationsFromClass(fileContents: ByteArray): ArrayList<BinAnnData> {
|
|
||||||
|
|
||||||
val visitor =
|
|
||||||
TemplateClassVisitor(TemplateAnnotationVisitor())
|
|
||||||
|
|
||||||
ClassReader(fileContents).accept(visitor, ClassReader.SKIP_CODE or ClassReader.SKIP_DEBUG or ClassReader.SKIP_FRAMES)
|
|
||||||
|
|
||||||
return visitor.annVisitor.anns
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
+163
-33
@@ -17,15 +17,19 @@ import java.net.URLClassLoader
|
|||||||
import java.util.jar.JarFile
|
import java.util.jar.JarFile
|
||||||
import kotlin.coroutines.experimental.buildSequence
|
import kotlin.coroutines.experimental.buildSequence
|
||||||
import kotlin.script.experimental.annotations.KotlinScript
|
import kotlin.script.experimental.annotations.KotlinScript
|
||||||
|
import kotlin.script.experimental.api.KotlinType
|
||||||
import kotlin.script.experimental.api.ScriptingEnvironment
|
import kotlin.script.experimental.api.ScriptingEnvironment
|
||||||
import kotlin.script.experimental.api.ScriptingEnvironmentProperties
|
import kotlin.script.experimental.api.ScriptingEnvironmentProperties
|
||||||
import kotlin.script.experimental.definitions.ScriptDefinitionFromAnnotatedBaseClass
|
import kotlin.script.experimental.definitions.ScriptDefinitionFromAnnotatedBaseClass
|
||||||
|
import kotlin.script.experimental.jvm.JvmGetScriptingClass
|
||||||
|
import kotlin.script.templates.ScriptTemplateDefinition
|
||||||
|
|
||||||
internal const val SCRIPT_DEFINITION_MARKERS_PATH = "META-INF/kotlin/script/templates/"
|
internal const val SCRIPT_DEFINITION_MARKERS_PATH = "META-INF/kotlin/script/templates/"
|
||||||
|
|
||||||
class ScriptDefinitionsFromClasspathDiscoverySource(
|
class ScriptDefinitionsFromClasspathDiscoverySource(
|
||||||
private val classpath: List<File>,
|
private val classpath: List<File>,
|
||||||
private val defaultScriptDefinitionClasspath: List<File>,
|
private val defaultScriptDefinitionClasspath: List<File>,
|
||||||
|
private val scriptResolverEnv: Map<String, Any?>,
|
||||||
private val messageCollector: MessageCollector
|
private val messageCollector: MessageCollector
|
||||||
) : ScriptDefinitionsSource {
|
) : ScriptDefinitionsSource {
|
||||||
|
|
||||||
@@ -33,21 +37,28 @@ class ScriptDefinitionsFromClasspathDiscoverySource(
|
|||||||
discoverScriptTemplatesInClasspath(
|
discoverScriptTemplatesInClasspath(
|
||||||
classpath,
|
classpath,
|
||||||
defaultScriptDefinitionClasspath,
|
defaultScriptDefinitionClasspath,
|
||||||
|
this::class.java.classLoader,
|
||||||
|
scriptResolverEnv,
|
||||||
messageCollector
|
messageCollector
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun discoverScriptTemplatesInClasspath(
|
internal fun discoverScriptTemplatesInClasspath(
|
||||||
classpath: Iterable<File>,
|
classpath: List<File>,
|
||||||
defaultScriptDefinitionClasspath: List<File>,
|
defaultScriptDefinitionClasspath: List<File>,
|
||||||
|
baseClassLoader: ClassLoader,
|
||||||
|
scriptResolverEnv: Map<String, Any?>,
|
||||||
messageCollector: MessageCollector
|
messageCollector: MessageCollector
|
||||||
): Sequence<LazyScriptDefinitionFromDiscoveredClass> = buildSequence {
|
): 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 {
|
||||||
|
URLClassLoader(classpath.map { it.toURI().toURL() }.toTypedArray(), baseClassLoader)
|
||||||
|
}
|
||||||
for (dep in classpath) {
|
for (dep in classpath) {
|
||||||
try {
|
try {
|
||||||
when {
|
when {
|
||||||
// checking for extension is the compiler current behaviour, so the same logic is implemented here
|
dep.isFile && dep.extension == "jar" -> { // checking for extension is the compiler current behaviour, so the same logic is implemented here
|
||||||
dep.isFile && dep.extension == "jar" -> {
|
|
||||||
val jar = JarFile(dep)
|
val jar = JarFile(dep)
|
||||||
if (jar.getJarEntry(SCRIPT_DEFINITION_MARKERS_PATH) != null) {
|
if (jar.getJarEntry(SCRIPT_DEFINITION_MARKERS_PATH) != null) {
|
||||||
for (template in jar.entries()) {
|
for (template in jar.entries()) {
|
||||||
@@ -60,17 +71,16 @@ internal fun discoverScriptTemplatesInClasspath(
|
|||||||
"Configure scripting: class not found $templateClassName"
|
"Configure scripting: class not found $templateClassName"
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
messageCollector.report(
|
loadScriptDefinition(
|
||||||
CompilerMessageSeverity.LOGGING,
|
jar.getInputStream(templateClass).readBytes(),
|
||||||
"Configure scripting: Added template $templateClassName from $dep"
|
templateClassName, classpath, { classLoader }, scriptResolverEnv, messageCollector
|
||||||
)
|
)?.let {
|
||||||
yield(
|
messageCollector.report(
|
||||||
LazyScriptDefinitionFromDiscoveredClass(
|
CompilerMessageSeverity.LOGGING,
|
||||||
jar.getInputStream(templateClass).readBytes(),
|
"Configure scripting: Added template $templateClassName from $dep"
|
||||||
templateClassName, listOf(dep) + jar.extractClasspath(defaultScriptDefinitionClasspath),
|
|
||||||
messageCollector
|
|
||||||
)
|
)
|
||||||
)
|
yield(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -79,25 +89,30 @@ internal fun discoverScriptTemplatesInClasspath(
|
|||||||
dep.isDirectory -> {
|
dep.isDirectory -> {
|
||||||
val dir = File(dep, SCRIPT_DEFINITION_MARKERS_PATH)
|
val dir = File(dep, SCRIPT_DEFINITION_MARKERS_PATH)
|
||||||
if (dir.isDirectory) {
|
if (dir.isDirectory) {
|
||||||
dir.listFiles().forEach {
|
val templateClasspath by lazy {
|
||||||
val templateClass = File(dep, "${it.name.replace('.', '/')}.class")
|
listOf(dep) + defaultScriptDefinitionClasspath
|
||||||
if (!templateClass.exists() || !templateClass.isFile) {
|
}
|
||||||
|
val classLoader by lazy {
|
||||||
|
URLClassLoader(templateClasspath.map { it.toURI().toURL() }.toTypedArray(), baseClassLoader)
|
||||||
|
}
|
||||||
|
dir.listFiles().forEach { templateClassNmae ->
|
||||||
|
val templateClassFile = File(dep, "${templateClassNmae.name.replace('.', '/')}.class")
|
||||||
|
if (!templateClassFile.exists() || !templateClassFile.isFile) {
|
||||||
messageCollector.report(
|
messageCollector.report(
|
||||||
CompilerMessageSeverity.WARNING,
|
CompilerMessageSeverity.WARNING,
|
||||||
"Configure scripting: class not found ${it.name}"
|
"Configure scripting: class not found ${templateClassNmae.name}"
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
messageCollector.report(
|
loadScriptDefinition(
|
||||||
CompilerMessageSeverity.LOGGING,
|
templateClassFile.readBytes(),
|
||||||
"Configure scripting: Added template ${it.name} from $dep"
|
templateClassNmae.name, templateClasspath, { classLoader }, scriptResolverEnv, messageCollector
|
||||||
)
|
)?.let {
|
||||||
yield(
|
messageCollector.report(
|
||||||
LazyScriptDefinitionFromDiscoveredClass(
|
CompilerMessageSeverity.LOGGING,
|
||||||
templateClass.readBytes(),
|
"Configure scripting: Added template ${templateClassNmae.name} from $dep"
|
||||||
it.name, listOf(dep) + defaultScriptDefinitionClasspath,
|
|
||||||
messageCollector
|
|
||||||
)
|
)
|
||||||
)
|
yield(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -119,23 +134,138 @@ internal fun discoverScriptTemplatesInClasspath(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal fun loadScriptTemplatesFromClasspath(
|
||||||
|
scriptTemplates: List<String>,
|
||||||
|
classpath: List<File>,
|
||||||
|
dependenciesClasspath: List<File>,
|
||||||
|
baseClassLoader: ClassLoader,
|
||||||
|
scriptResolverEnv: Map<String, Any?>,
|
||||||
|
messageCollector: MessageCollector
|
||||||
|
): Sequence<KotlinScriptDefinition> = buildSequence {
|
||||||
|
val templatesLeftToFind = ArrayList<String>()
|
||||||
|
// trying the direct classloading from baseClassloader first, since this is the most performant variant
|
||||||
|
for (template in scriptTemplates) {
|
||||||
|
val def = loadScriptDefinition(baseClassLoader, template, scriptResolverEnv, messageCollector)
|
||||||
|
if (def == null) {
|
||||||
|
templatesLeftToFind.add(template)
|
||||||
|
} else {
|
||||||
|
yield(def!!)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// then searching the remaining templates in the supplied classpath
|
||||||
|
if (templatesLeftToFind.isNotEmpty()) {
|
||||||
|
val templateClasspath by lazy {
|
||||||
|
classpath + dependenciesClasspath
|
||||||
|
}
|
||||||
|
val classLoader by lazy {
|
||||||
|
URLClassLoader(templateClasspath.map { it.toURI().toURL() }.toTypedArray(), baseClassLoader)
|
||||||
|
}
|
||||||
|
for (dep in classpath) {
|
||||||
|
try {
|
||||||
|
when {
|
||||||
|
dep.isFile && dep.extension == "jar" -> { // checking for extension is the compiler current behaviour, so the same logic is implemented here
|
||||||
|
val jar = JarFile(dep)
|
||||||
|
for (templateClassName in templatesLeftToFind) {
|
||||||
|
val templateClassEntry = jar.getJarEntry("${templateClassName.replace('.', '/')}.class")
|
||||||
|
if (templateClassEntry != null) {
|
||||||
|
loadScriptDefinition(
|
||||||
|
jar.getInputStream(templateClassEntry).readBytes(),
|
||||||
|
templateClassName, templateClasspath, { classLoader }, scriptResolverEnv, messageCollector
|
||||||
|
)?.let {
|
||||||
|
templatesLeftToFind.remove(templateClassName)
|
||||||
|
yield(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dep.isDirectory -> {
|
||||||
|
for (templateClassName in scriptTemplates) {
|
||||||
|
val templateClassFile = File(dep, "${templateClassName.replace('.', '/')}.class")
|
||||||
|
if (templateClassFile.exists()) {
|
||||||
|
loadScriptDefinition(
|
||||||
|
templateClassFile.readBytes(),
|
||||||
|
templateClassName, templateClasspath, { classLoader }, scriptResolverEnv, messageCollector
|
||||||
|
)?.let {
|
||||||
|
templatesLeftToFind.remove(templateClassName)
|
||||||
|
yield(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
// assuming that invalid classpath entries will be reported elsewhere anyway, so do not spam user with additional warnings here
|
||||||
|
messageCollector.report(
|
||||||
|
CompilerMessageSeverity.LOGGING,
|
||||||
|
"Configure scripting: Unknown classpath entry $dep"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: IOException) {
|
||||||
|
messageCollector.report(
|
||||||
|
CompilerMessageSeverity.WARNING,
|
||||||
|
"Configure scripting: unable to process classpath entry $dep: $e"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (templatesLeftToFind.isNotEmpty()) {
|
||||||
|
messageCollector.report(
|
||||||
|
CompilerMessageSeverity.WARNING,
|
||||||
|
"Configure scripting: unable to find script definition classes: $templatesLeftToFind"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun loadScriptDefinition(
|
||||||
|
templateClassBytes: ByteArray,
|
||||||
|
templateClassName: String,
|
||||||
|
templateClasspath: List<File>,
|
||||||
|
getClassLoader: () -> ClassLoader,
|
||||||
|
scriptResolverEnv: Map<String, Any?>,
|
||||||
|
messageCollector: MessageCollector
|
||||||
|
): KotlinScriptDefinition? {
|
||||||
|
val anns = loadAnnotationsFromClass(templateClassBytes)
|
||||||
|
for (ann in anns) {
|
||||||
|
var def: KotlinScriptDefinition? = null
|
||||||
|
if (ann.name == KotlinScript::class.simpleName) {
|
||||||
|
def = LazyScriptDefinitionFromDiscoveredClass(anns, templateClassName, templateClasspath, messageCollector)
|
||||||
|
} else if (ann.name == ScriptTemplateDefinition::class.simpleName) {
|
||||||
|
val templateClass = getClassLoader().loadClass(templateClassName).kotlin
|
||||||
|
def = KotlinScriptDefinitionFromAnnotatedTemplate(templateClass, scriptResolverEnv, templateClasspath)
|
||||||
|
}
|
||||||
|
if (def != null) {
|
||||||
|
messageCollector.report(
|
||||||
|
CompilerMessageSeverity.LOGGING,
|
||||||
|
"Configure scripting: Added template $templateClassName from $templateClasspath"
|
||||||
|
)
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
}
|
||||||
|
messageCollector.report(
|
||||||
|
CompilerMessageSeverity.WARNING,
|
||||||
|
"Configure scripting: $templateClassName is not marked with any known kotlin script annotation"
|
||||||
|
)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
private fun JarFile.extractClasspath(defaultClasspath: List<File>): List<File> =
|
private fun JarFile.extractClasspath(defaultClasspath: List<File>): List<File> =
|
||||||
manifest.mainAttributes.getValue("Class-Path")?.split(" ")?.map(::File) ?: defaultClasspath
|
manifest.mainAttributes.getValue("Class-Path")?.split(" ")?.map(::File) ?: defaultClasspath
|
||||||
|
|
||||||
internal fun loadScriptDefinition(
|
private fun loadScriptDefinition(
|
||||||
classloader: URLClassLoader,
|
classLoader: ClassLoader,
|
||||||
template: String,
|
template: String,
|
||||||
scriptResolverEnv: Map<String, Any?>,
|
scriptResolverEnv: Map<String, Any?>,
|
||||||
messageCollector: MessageCollector
|
messageCollector: MessageCollector
|
||||||
): KotlinScriptDefinition? {
|
): KotlinScriptDefinition? {
|
||||||
try {
|
try {
|
||||||
val cls = classloader.loadClass(template)
|
val cls = classLoader.loadClass(template)
|
||||||
val def =
|
val def =
|
||||||
if (cls.annotations.firstIsInstanceOrNull<KotlinScript>() != null) {
|
if (cls.annotations.firstIsInstanceOrNull<KotlinScript>() != null) {
|
||||||
KotlinScriptDefinitionAdapterFromNewAPI(
|
KotlinScriptDefinitionAdapterFromNewAPI(
|
||||||
ScriptDefinitionFromAnnotatedBaseClass(
|
ScriptDefinitionFromAnnotatedBaseClass(
|
||||||
ScriptingEnvironment(
|
ScriptingEnvironment(
|
||||||
ScriptingEnvironmentProperties.baseClass to cls.kotlin
|
ScriptingEnvironmentProperties.baseClass to KotlinType(cls.kotlin),
|
||||||
|
ScriptingEnvironmentProperties.getScriptingClass to JvmGetScriptingClass()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@@ -149,7 +279,7 @@ internal fun loadScriptDefinition(
|
|||||||
)
|
)
|
||||||
return def
|
return def
|
||||||
} catch (ex: ClassNotFoundException) {
|
} catch (ex: ClassNotFoundException) {
|
||||||
messageCollector.report(CompilerMessageSeverity.ERROR, "Cannot find script definition template class $template")
|
// return null
|
||||||
} catch (ex: Exception) {
|
} catch (ex: Exception) {
|
||||||
messageCollector.report(
|
messageCollector.report(
|
||||||
CompilerMessageSeverity.ERROR,
|
CompilerMessageSeverity.ERROR,
|
||||||
|
|||||||
+6
-20
@@ -7,7 +7,6 @@ package org.jetbrains.kotlin.scripting.compiler.plugin
|
|||||||
|
|
||||||
import com.intellij.mock.MockProject
|
import com.intellij.mock.MockProject
|
||||||
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
|
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
|
||||||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
|
|
||||||
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
|
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
|
||||||
import org.jetbrains.kotlin.cli.jvm.config.jvmClasspathRoots
|
import org.jetbrains.kotlin.cli.jvm.config.jvmClasspathRoots
|
||||||
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
|
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
|
||||||
@@ -16,7 +15,6 @@ import org.jetbrains.kotlin.config.JVMConfigurationKeys
|
|||||||
import org.jetbrains.kotlin.extensions.CompilerConfigurationExtension
|
import org.jetbrains.kotlin.extensions.CompilerConfigurationExtension
|
||||||
import org.jetbrains.kotlin.script.StandardScriptDefinition
|
import org.jetbrains.kotlin.script.StandardScriptDefinition
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.net.URLClassLoader
|
|
||||||
|
|
||||||
class ScriptingCompilerConfigurationExtension(val project: MockProject) : CompilerConfigurationExtension {
|
class ScriptingCompilerConfigurationExtension(val project: MockProject) : CompilerConfigurationExtension {
|
||||||
|
|
||||||
@@ -41,6 +39,7 @@ class ScriptingCompilerConfigurationExtension(val project: MockProject) : Compil
|
|||||||
configureScriptDefinitions(
|
configureScriptDefinitions(
|
||||||
explicitScriptDefinitions,
|
explicitScriptDefinitions,
|
||||||
configuration,
|
configuration,
|
||||||
|
this::class.java.classLoader,
|
||||||
messageCollector,
|
messageCollector,
|
||||||
scriptResolverEnv
|
scriptResolverEnv
|
||||||
)
|
)
|
||||||
@@ -57,6 +56,7 @@ class ScriptingCompilerConfigurationExtension(val project: MockProject) : Compil
|
|||||||
ScriptDefinitionsFromClasspathDiscoverySource(
|
ScriptDefinitionsFromClasspathDiscoverySource(
|
||||||
configuration.jvmClasspathRoots,
|
configuration.jvmClasspathRoots,
|
||||||
emptyList(),
|
emptyList(),
|
||||||
|
configuration.get(ScriptingConfigurationKeys.LEGACY_SCRIPT_RESOLVER_ENVIRONMENT_OPTION) ?: emptyMap(),
|
||||||
messageCollector
|
messageCollector
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@@ -74,31 +74,17 @@ class ScriptingCompilerConfigurationComponentRegistrar : ComponentRegistrar {
|
|||||||
fun configureScriptDefinitions(
|
fun configureScriptDefinitions(
|
||||||
scriptTemplates: List<String>,
|
scriptTemplates: List<String>,
|
||||||
configuration: CompilerConfiguration,
|
configuration: CompilerConfiguration,
|
||||||
|
baseClassloader: ClassLoader,
|
||||||
messageCollector: MessageCollector,
|
messageCollector: MessageCollector,
|
||||||
scriptResolverEnv: Map<String, Any?>
|
scriptResolverEnv: Map<String, Any?>
|
||||||
) {
|
) {
|
||||||
val classpath = configuration.jvmClasspathRoots
|
val classpath = configuration.jvmClasspathRoots
|
||||||
// TODO: consider using escaping to allow kotlin escaped names in class names
|
// TODO: consider using escaping to allow kotlin escaped names in class names
|
||||||
if (scriptTemplates.isNotEmpty()) {
|
if (scriptTemplates.isNotEmpty()) {
|
||||||
val classloader =
|
loadScriptTemplatesFromClasspath(scriptTemplates, classpath, emptyList(), baseClassloader, scriptResolverEnv, messageCollector)
|
||||||
URLClassLoader(classpath.map { it.toURI().toURL() }.toTypedArray(), Thread.currentThread().contextClassLoader)
|
.forEach {
|
||||||
var hasErrors = false
|
configuration.add(JVMConfigurationKeys.SCRIPT_DEFINITIONS, it)
|
||||||
for (template in scriptTemplates) {
|
|
||||||
val def = loadScriptDefinition(
|
|
||||||
classloader,
|
|
||||||
template,
|
|
||||||
scriptResolverEnv,
|
|
||||||
messageCollector
|
|
||||||
)
|
|
||||||
if (!hasErrors && def == null) hasErrors = true
|
|
||||||
if (def != null) {
|
|
||||||
configuration.add(JVMConfigurationKeys.SCRIPT_DEFINITIONS, def)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (hasErrors) {
|
|
||||||
messageCollector.report(CompilerMessageSeverity.LOGGING, "(Classpath used for templates loading: $classpath)")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+52
@@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* 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 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
|
||||||
|
|
||||||
|
internal class BinAnnData(
|
||||||
|
val name: String,
|
||||||
|
val args: ArrayList<String> = arrayListOf()
|
||||||
|
)
|
||||||
|
|
||||||
|
private class TemplateAnnotationVisitor(val anns: ArrayList<BinAnnData> = arrayListOf()) : AnnotationVisitor(Opcodes.ASM5) {
|
||||||
|
override fun visit(name: String?, value: Any?) {
|
||||||
|
anns.last().args.add(value.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class TemplateClassVisitor(val annVisitor: TemplateAnnotationVisitor) : ClassVisitor(Opcodes.ASM5) {
|
||||||
|
override fun visitAnnotation(desc: String, visible: Boolean): AnnotationVisitor {
|
||||||
|
val shortName = jvmDescToClassId(desc).shortClassName.asString()
|
||||||
|
if (shortName.startsWith("KotlinScript")) {
|
||||||
|
annVisitor.anns.add(BinAnnData(shortName))
|
||||||
|
}
|
||||||
|
return annVisitor
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 =
|
||||||
|
TemplateClassVisitor(TemplateAnnotationVisitor())
|
||||||
|
|
||||||
|
ClassReader(fileContents).accept(visitor, ClassReader.SKIP_CODE or ClassReader.SKIP_DEBUG or ClassReader.SKIP_FRAMES)
|
||||||
|
|
||||||
|
return visitor.annVisitor.anns
|
||||||
|
}
|
||||||
|
|
||||||
+1
-1
@@ -113,7 +113,7 @@ class ScriptingCompilerPluginTest : TestCaseWithTmpdir() {
|
|||||||
messageCollector.clear()
|
messageCollector.clear()
|
||||||
|
|
||||||
val lazyDefsSeq =
|
val lazyDefsSeq =
|
||||||
discoverScriptTemplatesInClasspath(listOf(defsOut), emptyList(), messageCollector)
|
discoverScriptTemplatesInClasspath(listOf(defsOut), emptyList(), this::class.java.classLoader, emptyMap(), messageCollector)
|
||||||
|
|
||||||
assertTrue(messageCollector.messages.isEmpty()) {
|
assertTrue(messageCollector.messages.isEmpty()) {
|
||||||
"Unexpected messages from discovery sequence (should be empty):\n$messageCollector"
|
"Unexpected messages from discovery sequence (should be empty):\n$messageCollector"
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ dependencies {
|
|||||||
packedJars(project(":kotlin-scripting-compiler")) { isTransitive = false }
|
packedJars(project(":kotlin-scripting-compiler")) { isTransitive = false }
|
||||||
runtime(project(":kotlin-scripting-common"))
|
runtime(project(":kotlin-scripting-common"))
|
||||||
runtime(project(":kotlin-scripting-jvm"))
|
runtime(project(":kotlin-scripting-jvm"))
|
||||||
|
runtime(project(":kotlin-scripting-misc"))
|
||||||
}
|
}
|
||||||
|
|
||||||
sourceSets {
|
sourceSets {
|
||||||
|
|||||||
Reference in New Issue
Block a user