diff --git a/compiler/frontend.script/src/org/jetbrains/kotlin/script/KotlinScriptDefinitionFromAnnotatedTemplate.kt b/compiler/frontend.script/src/org/jetbrains/kotlin/script/KotlinScriptDefinitionFromAnnotatedTemplate.kt index 4b8fd34e48e..dc8cd1561bf 100644 --- a/compiler/frontend.script/src/org/jetbrains/kotlin/script/KotlinScriptDefinitionFromAnnotatedTemplate.kt +++ b/compiler/frontend.script/src/org/jetbrains/kotlin/script/KotlinScriptDefinitionFromAnnotatedTemplate.kt @@ -49,14 +49,14 @@ open class KotlinScriptDefinitionFromAnnotatedTemplate( val scriptFilePattern by lazy { providedScriptFilePattern - ?: template.annotations.firstIsInstanceOrNull()?.scriptFilePattern - ?: template.annotations.firstIsInstanceOrNull()?.scriptFilePattern + ?: takeUnlessError { template.annotations.firstIsInstanceOrNull()?.scriptFilePattern } + ?: takeUnlessError { template.annotations.firstIsInstanceOrNull()?.scriptFilePattern } ?: DEFAULT_SCRIPT_FILE_PATTERN } val resolver: ScriptDependenciesResolver? by lazy { - val defAnn by lazy { template.annotations.firstIsInstanceOrNull() } - val legacyDefAnn by lazy { template.annotations.firstIsInstanceOrNull() } + val defAnn by lazy { takeUnlessError { template.annotations.firstIsInstanceOrNull() } } + val legacyDefAnn by lazy { takeUnlessError { template.annotations.firstIsInstanceOrNull() } } when { providedResolver != null -> providedResolver // TODO: logScriptDefMessage missing or invalid constructor @@ -89,8 +89,8 @@ open class KotlinScriptDefinitionFromAnnotatedTemplate( } val samWithReceiverAnnotations: List? by lazy { - template.annotations.firstIsInstanceOrNull()?.annotations?.toList() - ?: template.annotations.firstIsInstanceOrNull()?.annotations?.toList() + takeUnlessError { template.annotations.firstIsInstanceOrNull()?.annotations?.toList() } + ?: takeUnlessError { template.annotations.firstIsInstanceOrNull()?.annotations?.toList() } } private val acceptedAnnotations: List> by lazy { @@ -124,13 +124,9 @@ open class KotlinScriptDefinitionFromAnnotatedTemplate( override fun getDependenciesFor(file: TF, project: Project, previousDependencies: KotlinScriptExternalDependencies?): KotlinScriptExternalDependencies? { - fun logClassloadingError(ex: Throwable) { - logScriptDefMessage(ScriptDependenciesResolver.ReportSeverity.WARNING, ex.message ?: "Invalid script template: ${template.qualifiedName}", null) - } - fun makeScriptContents() = BasicScriptContents(file, getAnnotations = { val classLoader = template.java.classLoader - try { + takeUnlessError { getAnnotationEntries(file, project) .mapNotNull { psiAnn -> // TODO: consider advanced matching using semantic similar to actual resolving @@ -139,21 +135,14 @@ open class KotlinScriptDefinitionFromAnnotatedTemplate( }?.let { constructAnnotation(psiAnn, classLoader.loadClass(it.qualifiedName).kotlin as KClass) } } } - catch (ex: Throwable) { - logClassloadingError(ex) - emptyList() - } + ?: emptyList() }) - try { + return takeUnlessError { val fileDeps = resolver?.resolve(makeScriptContents(), environment, ::logScriptDefMessage, previousDependencies) // TODO: use it as a Future - return fileDeps?.get() + fileDeps?.get() } - catch (ex: Throwable) { - logClassloadingError(ex) - } - return null } private fun getAnnotationEntries(file: TF, project: Project): Iterable = when (file) { @@ -188,6 +177,15 @@ open class KotlinScriptDefinitionFromAnnotatedTemplate( override val annotationsForSamWithReceivers: List get() = samWithReceiverAnnotations ?: super.annotationsForSamWithReceivers + private inline fun takeUnlessError(body: () -> T?): T? = + try { + body() + } + catch (ex: Throwable) { + log.error("Invalid script template: ${template.qualifiedName}", ex) + null + } + companion object { internal val log = Logger.getInstance(KotlinScriptDefinitionFromAnnotatedTemplate::class.java) }