New J2K: Promete class to object when init declaration present

This commit is contained in:
Ilya Kirillov
2019-02-13 13:11:05 +03:00
committed by Ilya Kirillov
parent b94ff8af36
commit cc3564395c
3 changed files with 41 additions and 11 deletions
@@ -6,22 +6,28 @@
package org.jetbrains.kotlin.nj2k.conversions
import org.jetbrains.kotlin.nj2k.ConversionContext
import org.jetbrains.kotlin.nj2k.getCompanion
import org.jetbrains.kotlin.nj2k.tree.*
import org.jetbrains.kotlin.nj2k.tree.impl.*
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import org.jetbrains.kotlin.nj2k.tree.impl.JKAnnotationListImpl
import org.jetbrains.kotlin.nj2k.tree.impl.JKClassImpl
import org.jetbrains.kotlin.nj2k.tree.impl.psi
class ClassToObjectPromotionConversion(private val context: ConversionContext) : RecursiveApplicableConversionBase() {
override fun applyToElement(element: JKTreeElement): JKTreeElement {
if (element is JKClass && element.classKind == JKClass.ClassKind.CLASS) {
val companion =
element.declarationList.firstIsInstanceOrNull<JKClass>()
?.takeIf { it.classKind == JKClass.ClassKind.COMPANION }
?: return recurse(element)
val companion = element.getCompanion() ?: 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
is JKKtInitDeclaration ->
it.block.statements.all { statement ->
when (statement) {
is JKDeclarationStatement -> statement.declaredStatements.isEmpty()
else -> false
}
}
is JKClass -> true
else -> false
}
}
@@ -35,8 +41,12 @@ class ClassToObjectPromotionConversion(private val context: ConversionContext) :
element.inheritance,
JKClass.ClassKind.OBJECT,
element.typeParameterList,
companion.classBody.also {
it.handleDeclarationsModifiers()
companion.classBody.also { body ->
body.handleDeclarationsModifiers()
body.declarations += element.classBody.declarations.filter {
//TODO preseve order
it is JKClass && it.classKind != JKClass.ClassKind.COMPANION
}.map { it.detached(element.classBody) }
},
JKAnnotationListImpl(),
element.extraModifiers,
@@ -5,7 +5,10 @@
package org.jetbrains.kotlin.nj2k.conversions
import org.jetbrains.kotlin.nj2k.tree.*
import org.jetbrains.kotlin.nj2k.tree.ExtraModifier
import org.jetbrains.kotlin.nj2k.tree.JKClass
import org.jetbrains.kotlin.nj2k.tree.JKTreeElement
import org.jetbrains.kotlin.nj2k.tree.isLocalClass
class InnerClassConversion : RecursiveApplicableConversionBase() {
override fun applyToElement(element: JKTreeElement): JKTreeElement {
@@ -19,6 +22,7 @@ class InnerClassConversion : RecursiveApplicableConversionBase() {
private fun applyArmed(element: JKTreeElement, outer: JKClass): JKTreeElement {
if (element !is JKClass) return recurseArmed(element, outer)
if (element.classKind == JKClass.ClassKind.COMPANION) return recurseArmed(element, outer)
if (element.isLocalClass()) return recurseArmed(element, outer)
val static = element.extraModifiers.find { it == ExtraModifier.STATIC }
@@ -482,8 +482,24 @@ fun equalsExpression(left: JKExpression, right: JKExpression, symbolProvider: JK
symbolProvider
)
fun createCompanion(declarations: List<JKDeclaration>): JKClass =
JKClassImpl(
JKNameIdentifierImpl(""),
JKInheritanceInfoImpl(emptyList(), emptyList()),
JKClass.ClassKind.COMPANION,
JKTypeParameterListImpl(),
JKClassBodyImpl(declarations),
JKAnnotationListImpl(),
emptyList(),
Visibility.PUBLIC,
Modality.FINAL
)
fun JKClass.getCompanion(): JKClass? =
declarationList.firstOrNull { it is JKClass && it.classKind == JKClass.ClassKind.COMPANION } as? JKClass
fun JKClass.getOrCreateCompainonObject(): JKClass =
(declarationList.firstOrNull { it is JKClass && it.classKind == JKClass.ClassKind.COMPANION } as? JKClass)
getCompanion()
?: JKClassImpl(
JKNameIdentifierImpl(""),
JKInheritanceInfoImpl(emptyList(), emptyList()),