diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/textBuilder/buildDecompiledText.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/textBuilder/buildDecompiledText.kt index 70eed77bbb849..acb73204f0f57 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/textBuilder/buildDecompiledText.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/textBuilder/buildDecompiledText.kt @@ -8,6 +8,7 @@ package org.jetbrains.kotlin.idea.decompiler.textBuilder import com.intellij.openapi.util.TextRange import org.jetbrains.kotlin.contracts.description.ContractProviderKey import org.jetbrains.kotlin.descriptors.* +import org.jetbrains.kotlin.idea.core.quoteIfNeeded import org.jetbrains.kotlin.idea.decompiler.navigation.ByDescriptorIndexer import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.psi.psiUtil.quoteIfNeeded @@ -46,7 +47,7 @@ fun buildDecompiledText( builder.append("// IntelliJ API Decompiler stub source generated from a class file\n" + "// Implementation of methods is not available") builder.append("\n\n") if (!packageFqName.isRoot) { - builder.append("package ").append(packageFqName).append("\n\n") + builder.append("package ").append(packageFqName.quoteIfNeeded()).append("\n\n") } } diff --git a/idea/testData/decompiler/decompiledTextJvm/PackageWithQuotes.expected.kt b/idea/testData/decompiler/decompiledTextJvm/PackageWithQuotes.expected.kt new file mode 100644 index 0000000000000..359cbaf79d56c --- /dev/null +++ b/idea/testData/decompiler/decompiledTextJvm/PackageWithQuotes.expected.kt @@ -0,0 +1,7 @@ +// IntelliJ API Decompiler stub source generated from a class file +// Implementation of methods is not available + +package `try`.`package` + +public final class PackageWithQuotes public constructor() { +} \ No newline at end of file diff --git a/idea/testData/decompiler/decompiledTextJvm/PackageWithQuotes/PackageWithQuotes.kt b/idea/testData/decompiler/decompiledTextJvm/PackageWithQuotes/PackageWithQuotes.kt new file mode 100644 index 0000000000000..c88bd2c46ba2c --- /dev/null +++ b/idea/testData/decompiler/decompiledTextJvm/PackageWithQuotes/PackageWithQuotes.kt @@ -0,0 +1,3 @@ +package `try`.`package` + +class PackageWithQuotes \ No newline at end of file diff --git a/idea/testData/decompiler/decompiledTextJvm/PackageWithQuotes/package.txt b/idea/testData/decompiler/decompiledTextJvm/PackageWithQuotes/package.txt new file mode 100644 index 0000000000000..8e87ddafd9675 --- /dev/null +++ b/idea/testData/decompiler/decompiledTextJvm/PackageWithQuotes/package.txt @@ -0,0 +1 @@ +try.package diff --git a/idea/tests/org/jetbrains/kotlin/idea/decompiler/textBuilder/AbstractDecompiledTextTest.kt b/idea/tests/org/jetbrains/kotlin/idea/decompiler/textBuilder/AbstractDecompiledTextTest.kt index ef3038ec7d765..2287f43f6f740 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/decompiler/textBuilder/AbstractDecompiledTextTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/decompiler/textBuilder/AbstractDecompiledTextTest.kt @@ -12,17 +12,30 @@ import com.intellij.openapi.roots.OrderRootType import com.intellij.openapi.vfs.VirtualFile import com.intellij.psi.PsiFile import com.intellij.util.indexing.FileContentImpl +import com.intellij.util.io.exists +import com.intellij.util.io.readText import org.jetbrains.kotlin.idea.decompiler.classFile.KotlinClsStubBuilder import org.jetbrains.kotlin.idea.decompiler.classFile.KtClsFile import org.jetbrains.kotlin.idea.decompiler.stubBuilder.serializeToString import org.jetbrains.kotlin.psi.KtPsiFactory import org.jetbrains.kotlin.psi.stubs.elements.KtFileStubBuilder import org.junit.Assert +import java.nio.file.Paths import kotlin.test.assertTrue abstract class AbstractDecompiledTextTest(baseDirectory: String, allowKotlinPackage: Boolean) : AbstractDecompiledTextBaseTest(baseDirectory, allowKotlinPackage = allowKotlinPackage) { - override fun getFileToDecompile(): VirtualFile = getClassFile(TEST_PACKAGE, getTestName(false), module!!) + + private val CUSTOM_PACKAGE_FILE = "package.txt" + + override fun getFileToDecompile(): VirtualFile { + val className = getTestName(false) + + val customPackageFile = Paths.get(TEST_DATA_PATH, className, CUSTOM_PACKAGE_FILE) + val testFilePackage = customPackageFile.takeIf { it.exists() }?.readText()?.trimEnd() ?: TEST_PACKAGE + + return getClassFile(testFilePackage, className, module!!) + } override fun checkStubConsistency(file: VirtualFile, decompiledText: String) { val fileWithDecompiledText = KtPsiFactory(project).createFile(decompiledText) diff --git a/idea/tests/org/jetbrains/kotlin/idea/decompiler/textBuilder/JvmDecompiledTextTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/decompiler/textBuilder/JvmDecompiledTextTestGenerated.java index 5cc8dff9d039d..206ca254483ee 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/decompiler/textBuilder/JvmDecompiledTextTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/decompiler/textBuilder/JvmDecompiledTextTestGenerated.java @@ -43,6 +43,11 @@ public void testMultifileClass() throws Exception { runTest("idea/testData/decompiler/decompiledTextJvm/MultifileClass/"); } + @TestMetadata("PackageWithQuotes") + public void testPackageWithQuotes() throws Exception { + runTest("idea/testData/decompiler/decompiledTextJvm/PackageWithQuotes/"); + } + @TestMetadata("TestKt") public void testTestKt() throws Exception { runTest("idea/testData/decompiler/decompiledTextJvm/TestKt/"); @@ -79,6 +84,19 @@ public void testAllFilesPresentInMultifileClass() throws Exception { } } + @TestMetadata("idea/testData/decompiler/decompiledTextJvm/PackageWithQuotes") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class PackageWithQuotes extends AbstractJvmDecompiledTextTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + public void testAllFilesPresentInPackageWithQuotes() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/decompiler/decompiledTextJvm/PackageWithQuotes"), Pattern.compile("^([^\\.]+)$"), null, true); + } + } + @TestMetadata("idea/testData/decompiler/decompiledTextJvm/TestKt") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)