diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtPsiFactory.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/KtPsiFactory.kt index ed866258f64..5c7f0206c55 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KtPsiFactory.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/KtPsiFactory.kt @@ -22,6 +22,7 @@ import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.psiUtil.isIdentifier import org.jetbrains.kotlin.resolve.ImportPath import org.jetbrains.kotlin.utils.checkWithAttachment +import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments @JvmOverloads @JvmName("KtPsiFactory") diff --git a/compiler/util/src/org/jetbrains/kotlin/utils/KotlinExceptionWithAttachments.kt b/compiler/util/src/org/jetbrains/kotlin/utils/KotlinExceptionWithAttachments.kt index 6db3c0b66d9..6ed73cd3b35 100644 --- a/compiler/util/src/org/jetbrains/kotlin/utils/KotlinExceptionWithAttachments.kt +++ b/compiler/util/src/org/jetbrains/kotlin/utils/KotlinExceptionWithAttachments.kt @@ -7,38 +7,23 @@ package org.jetbrains.kotlin.utils import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.diagnostic.Attachment -import com.intellij.openapi.diagnostic.ExceptionWithAttachments -import java.nio.charset.StandardCharsets -import com.intellij.psi.* +import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.utils.exceptions.KotlinExceptionWithAttachments as KotlinExceptionWithAttachmentsBase +import org.jetbrains.kotlin.utils.exceptions.KotlinExceptionWithAttachments.Companion.withAttachmentsFrom import kotlin.contracts.ExperimentalContracts import kotlin.contracts.contract -open class KotlinExceptionWithAttachments : RuntimeException, ExceptionWithAttachments { - private val attachments = mutableListOf() +open class KotlinExceptionWithAttachments : RuntimeException, KotlinExceptionWithAttachmentsBase { + override val mutableAttachments = mutableListOf() + + override fun withAttachment(name: String, content: Any?): KotlinExceptionWithAttachments { + return super.withAttachment(name, content) as KotlinExceptionWithAttachments + } constructor(message: String) : super(message) constructor(message: String?, cause: Throwable?) : super(message, cause) { - if (cause is KotlinExceptionWithAttachments) { - cause.attachments.mapTo(attachments) { attachment -> - attachment.copyWithNewName("case_${attachment.path}") - } - } - if (cause != null) { - withAttachment("causeThrowable", cause.stackTraceToString()) - } - } - - private fun Attachment.copyWithNewName(newName: String): Attachment { - val content = String(bytes, StandardCharsets.UTF_8) - return Attachment(newName, content) - } - - override fun getAttachments(): Array = attachments.toTypedArray() - - fun withAttachment(name: String, content: Any?): KotlinExceptionWithAttachments { - attachments.add(Attachment(name, content?.toString() ?: "")) - return this + withAttachmentsFrom(cause) } fun withPsiAttachment(name: String, element: PsiElement?): KotlinExceptionWithAttachments { @@ -50,7 +35,7 @@ open class KotlinExceptionWithAttachments : RuntimeException, ExceptionWithAttac @OptIn(ExperimentalContracts::class) -inline fun checkWithAttachment(value: Boolean, lazyMessage: () -> String, attachments: (KotlinExceptionWithAttachments) -> Unit = {}) { +inline fun checkWithAttachment(value: Boolean, lazyMessage: () -> String, attachments: (org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments) -> Unit = {}) { contract { returns() implies (value) } if (!value) { diff --git a/compiler/util/src/org/jetbrains/kotlin/utils/exceptions/KotlinExceptionWithAttachments.kt b/compiler/util/src/org/jetbrains/kotlin/utils/exceptions/KotlinExceptionWithAttachments.kt new file mode 100644 index 00000000000..122527fafa4 --- /dev/null +++ b/compiler/util/src/org/jetbrains/kotlin/utils/exceptions/KotlinExceptionWithAttachments.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * 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.utils.exceptions + +import com.intellij.openapi.diagnostic.Attachment +import com.intellij.openapi.diagnostic.ExceptionWithAttachments +import java.nio.charset.StandardCharsets + +interface KotlinExceptionWithAttachments : ExceptionWithAttachments { + val mutableAttachments: MutableList + + override fun getAttachments(): Array = mutableAttachments.toTypedArray() + + fun withAttachment(name: String, content: Any?): KotlinExceptionWithAttachments { + mutableAttachments.add(Attachment(name, content?.toString() ?: "")) + return this + } + + companion object { + internal fun KotlinExceptionWithAttachments.withAttachmentsFrom(from: Throwable?) { + if (from is KotlinExceptionWithAttachments) { + from.mutableAttachments.mapTo(mutableAttachments) { attachment -> + attachment.copyWithNewName("case_${attachment.path}") + } + } + if (from != null) { + withAttachment("causeThrowable", from.stackTraceToString()) + } + } + + private fun Attachment.copyWithNewName(newName: String): Attachment { + val content = String(bytes, StandardCharsets.UTF_8) + return Attachment(newName, content) + } + } +} \ No newline at end of file