From 5f39f628c98230095998461ff00a72bf4c22266c Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 25 Feb 2016 12:06:41 +0300 Subject: [PATCH] Fix and test built-ins decompiler for incompatible version --- .../builtIns/KotlinBuiltInDecompiler.kt | 3 +- idea/testData/decompiler/builtins/test.stubs | 3 + idea/testData/decompiler/builtins/test.text | 4 ++ .../builtins/test/test.kotlin_builtins | Bin 0 -> 16 bytes .../stubBuilder/BuiltInDecompilerTest.kt | 52 ++++++++++++++---- 5 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 idea/testData/decompiler/builtins/test.stubs create mode 100644 idea/testData/decompiler/builtins/test.text create mode 100644 idea/testData/decompiler/builtins/test/test.kotlin_builtins diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/builtIns/KotlinBuiltInDecompiler.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/builtIns/KotlinBuiltInDecompiler.kt index 7a639dd86d1..175cbba37b4 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/builtIns/KotlinBuiltInDecompiler.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/builtIns/KotlinBuiltInDecompiler.kt @@ -70,7 +70,6 @@ fun buildDecompiledTextForBuiltIns(builtInFile: VirtualFile): DecompiledText { val file = BuiltInDefinitionFile.read(builtInFile) when (file) { is BuiltInDefinitionFile.Incompatible -> { - // TODO: test return createIncompatibleAbiVersionDecompiledText(BuiltInsBinaryVersion.INSTANCE, file.version) } is BuiltInDefinitionFile.Compatible -> { @@ -128,7 +127,7 @@ sealed class BuiltInDefinitionFile { internal fun isInternalBuiltInFile(file: BuiltInDefinitionFile): Boolean { when (file) { - is BuiltInDefinitionFile.Incompatible -> return true + is BuiltInDefinitionFile.Incompatible -> return false is BuiltInDefinitionFile.Compatible -> { return file.classesToDecompile.isEmpty() && file.proto.`package`.functionCount == 0 && diff --git a/idea/testData/decompiler/builtins/test.stubs b/idea/testData/decompiler/builtins/test.stubs new file mode 100644 index 00000000000..5ae72743d43 --- /dev/null +++ b/idea/testData/decompiler/builtins/test.stubs @@ -0,0 +1,3 @@ +PsiJetFileStubImpl[package=] + PACKAGE_DIRECTIVE: + IMPORT_LIST: diff --git a/idea/testData/decompiler/builtins/test.text b/idea/testData/decompiler/builtins/test.text new file mode 100644 index 00000000000..23ac4f269bc --- /dev/null +++ b/idea/testData/decompiler/builtins/test.text @@ -0,0 +1,4 @@ +// This class file was compiled with different version of Kotlin compiler and can't be decompiled. +// +// Current compiler ABI version is $VERSION$ +// File ABI version is 0.42.239 diff --git a/idea/testData/decompiler/builtins/test/test.kotlin_builtins b/idea/testData/decompiler/builtins/test/test.kotlin_builtins new file mode 100644 index 0000000000000000000000000000000000000000..22c5f4de7be842b1e71e52080ccc90eac162aad7 GIT binary patch literal 16 RcmZQzU|?o|04*T(9smLO0UZDU literal 0 HcmV?d00001 diff --git a/idea/tests/org/jetbrains/kotlin/idea/decompiler/stubBuilder/BuiltInDecompilerTest.kt b/idea/tests/org/jetbrains/kotlin/idea/decompiler/stubBuilder/BuiltInDecompilerTest.kt index c34c0d226d1..064ccf35cba 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/decompiler/stubBuilder/BuiltInDecompilerTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/decompiler/stubBuilder/BuiltInDecompilerTest.kt @@ -16,30 +16,62 @@ package org.jetbrains.kotlin.idea.decompiler.stubBuilder +import com.intellij.psi.stubs.PsiFileStub import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase import com.intellij.util.indexing.FileContentImpl import org.jetbrains.kotlin.builtins.BuiltInSerializerProtocol +import org.jetbrains.kotlin.builtins.BuiltInsBinaryVersion import org.jetbrains.kotlin.idea.decompiler.builtIns.KotlinBuiltInStubBuilder import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor +import org.jetbrains.kotlin.idea.test.PluginTestCaseBase +import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.psi.stubs.elements.KtFileStubBuilder +import org.jetbrains.kotlin.test.KotlinTestUtils import org.junit.Assert +import java.io.File + +abstract class AbstractBuiltInDecompilerTest : LightCodeInsightFixtureTestCase() { + protected fun doTest(packageFqName: String): String { + val stubTreeFromDecompiler = configureAndBuildFileStub(packageFqName) + val stubTreeFromDecompiledText = KtFileStubBuilder().buildStubTree(myFixture.file) + val expectedText = stubTreeFromDecompiledText.serializeToString() + Assert.assertEquals("Stub mismatch for package $packageFqName", expectedText, stubTreeFromDecompiler.serializeToString()) + return expectedText + } + + abstract fun configureAndBuildFileStub(packageFqName: String): PsiFileStub<*> + + override fun getProjectDescriptor() = KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE +} + +class BuiltInDecompilerTest : AbstractBuiltInDecompilerTest() { + override fun configureAndBuildFileStub(packageFqName: String): PsiFileStub<*> { + val dirInRuntime = findDir(packageFqName, project) + val kotlinBuiltInsVirtualFile = dirInRuntime.children.single { it.extension == BuiltInSerializerProtocol.BUILTINS_FILE_EXTENSION } + myFixture.configureFromExistingVirtualFile(kotlinBuiltInsVirtualFile) + return KotlinBuiltInStubBuilder().buildFileStub(FileContentImpl.createByFile(kotlinBuiltInsVirtualFile))!! + } -class BuiltInDecompilerTest : LightCodeInsightFixtureTestCase() { fun testBuiltInStubTreeEqualToStubTreeFromDecompiledText() { doTest("kotlin") doTest("kotlin.collections") } +} - private fun doTest(packageFqName: String) { - val dirInRuntime = findDir(packageFqName, project) - val kotlinBuiltInsVirtualFile = dirInRuntime.children.single { it.extension == BuiltInSerializerProtocol.BUILTINS_FILE_EXTENSION } - val stubTreeFromDecompiler = KotlinBuiltInStubBuilder().buildFileStub(FileContentImpl.createByFile(kotlinBuiltInsVirtualFile))!! - myFixture.configureFromExistingVirtualFile(kotlinBuiltInsVirtualFile) +class BuiltInDecompilerForWrongAbiVersionTest : AbstractBuiltInDecompilerTest() { + override fun getTestDataPath() = PluginTestCaseBase.getTestDataPathBase() + "/decompiler/builtins/" - val stubTreeFromDecompiledText = KtFileStubBuilder().buildStubTree(myFixture.file) - val expectedText = stubTreeFromDecompiledText.serializeToString() - Assert.assertEquals("Stub mismatch for package $packageFqName", expectedText, stubTreeFromDecompiler.serializeToString()) + override fun configureAndBuildFileStub(packageFqName: String): PsiFileStub<*> { + myFixture.configureByFile(testDataPath + BuiltInSerializerProtocol.getBuiltInsFilePath(FqName(packageFqName))) + return KotlinBuiltInStubBuilder().buildFileStub(FileContentImpl.createByFile(myFixture.file.virtualFile))!! } - override fun getProjectDescriptor() = KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE + fun testStubTreesEqualForIncompatibleAbiVersion() { + val serializedStub = doTest("test") + KotlinTestUtils.assertEqualsToFile( + File(testDataPath + "test.text"), + myFixture.file.text.replace(BuiltInsBinaryVersion.INSTANCE.toString(), "\$VERSION\$") + ) + KotlinTestUtils.assertEqualsToFile(File(testDataPath + "test.stubs"), serializedStub) + } }