From 70def791c07c9b14891952d27b4933f014f33854 Mon Sep 17 00:00:00 2001 From: Ilya Kirillov Date: Thu, 8 Nov 2018 16:36:35 +0300 Subject: [PATCH] New J2K: Fix ClassToObjectPromotionConversion when empty constructor --- .../ClassToObjectPromotionConversion.kt | 47 +++++++++++-------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/j2k/newSrc/org/jetbrains/kotlin/j2k/conversions/ClassToObjectPromotionConversion.kt b/j2k/newSrc/org/jetbrains/kotlin/j2k/conversions/ClassToObjectPromotionConversion.kt index 87371494621..6192dda44e7 100644 --- a/j2k/newSrc/org/jetbrains/kotlin/j2k/conversions/ClassToObjectPromotionConversion.kt +++ b/j2k/newSrc/org/jetbrains/kotlin/j2k/conversions/ClassToObjectPromotionConversion.kt @@ -14,29 +14,36 @@ import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull class ClassToObjectPromotionConversion : RecursiveApplicableConversionBase() { override fun applyToElement(element: JKTreeElement): JKTreeElement { if (element is JKClass && element.classKind == JKClass.ClassKind.CLASS) { - val constructor = element.declarationList.firstIsInstanceOrNull() - val companion = element.declarationList.firstOrNull { it is JKClass && it.classKind == JKClass.ClassKind.COMPANION } - if (companion != null && (constructor == null || constructor.parameters.isEmpty())) { - val declarations = element.declarationList - companion - listOfNotNull(constructor) - if (declarations.isEmpty()) { - companion as JKClass - companion.invalidate() - element.invalidate() - return recurse( - JKClassImpl( - element.modifierList, - element.name, - element.inheritance, - JKClass.ClassKind.OBJECT, - element.typeParameterList - ) - .apply { - declarationList = companion.declarationList - } - ) + val companion = + element.declarationList.firstIsInstanceOrNull() + ?.takeIf { it.classKind == JKClass.ClassKind.COMPANION } + ?: return recurse(element) + + val allDeclarationsMatches = element.declarationList.all { + when (it) { + is JKKtPrimaryConstructor -> it.parameters.isEmpty() && it.block.statements.isEmpty() + is JKClass -> it.classKind == JKClass.ClassKind.COMPANION + else -> false } } + + if (allDeclarationsMatches) { + companion.invalidate() + element.invalidate() + return recurse( + JKClassImpl( + element.modifierList, + element.name, + element.inheritance, + JKClass.ClassKind.OBJECT, + element.typeParameterList + ).apply { + declarationList = companion.declarationList + } + ) + } } + return recurse(element) } } \ No newline at end of file