New J2K: Promete class to object when init declaration present
This commit is contained in:
committed by
Ilya Kirillov
parent
b94ff8af36
commit
cc3564395c
@@ -6,22 +6,28 @@
|
|||||||
package org.jetbrains.kotlin.nj2k.conversions
|
package org.jetbrains.kotlin.nj2k.conversions
|
||||||
|
|
||||||
import org.jetbrains.kotlin.nj2k.ConversionContext
|
import org.jetbrains.kotlin.nj2k.ConversionContext
|
||||||
|
import org.jetbrains.kotlin.nj2k.getCompanion
|
||||||
import org.jetbrains.kotlin.nj2k.tree.*
|
import org.jetbrains.kotlin.nj2k.tree.*
|
||||||
import org.jetbrains.kotlin.nj2k.tree.impl.*
|
import org.jetbrains.kotlin.nj2k.tree.impl.JKAnnotationListImpl
|
||||||
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
|
import org.jetbrains.kotlin.nj2k.tree.impl.JKClassImpl
|
||||||
|
import org.jetbrains.kotlin.nj2k.tree.impl.psi
|
||||||
|
|
||||||
class ClassToObjectPromotionConversion(private val context: ConversionContext) : RecursiveApplicableConversionBase() {
|
class ClassToObjectPromotionConversion(private val context: ConversionContext) : RecursiveApplicableConversionBase() {
|
||||||
override fun applyToElement(element: JKTreeElement): JKTreeElement {
|
override fun applyToElement(element: JKTreeElement): JKTreeElement {
|
||||||
if (element is JKClass && element.classKind == JKClass.ClassKind.CLASS) {
|
if (element is JKClass && element.classKind == JKClass.ClassKind.CLASS) {
|
||||||
val companion =
|
val companion = element.getCompanion() ?: return recurse(element)
|
||||||
element.declarationList.firstIsInstanceOrNull<JKClass>()
|
|
||||||
?.takeIf { it.classKind == JKClass.ClassKind.COMPANION }
|
|
||||||
?: return recurse(element)
|
|
||||||
|
|
||||||
val allDeclarationsMatches = element.declarationList.all {
|
val allDeclarationsMatches = element.declarationList.all {
|
||||||
when (it) {
|
when (it) {
|
||||||
is JKKtPrimaryConstructor -> it.parameters.isEmpty() && it.block.statements.isEmpty()
|
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
|
else -> false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -35,8 +41,12 @@ class ClassToObjectPromotionConversion(private val context: ConversionContext) :
|
|||||||
element.inheritance,
|
element.inheritance,
|
||||||
JKClass.ClassKind.OBJECT,
|
JKClass.ClassKind.OBJECT,
|
||||||
element.typeParameterList,
|
element.typeParameterList,
|
||||||
companion.classBody.also {
|
companion.classBody.also { body ->
|
||||||
it.handleDeclarationsModifiers()
|
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(),
|
JKAnnotationListImpl(),
|
||||||
element.extraModifiers,
|
element.extraModifiers,
|
||||||
|
|||||||
@@ -5,7 +5,10 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.nj2k.conversions
|
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() {
|
class InnerClassConversion : RecursiveApplicableConversionBase() {
|
||||||
override fun applyToElement(element: JKTreeElement): JKTreeElement {
|
override fun applyToElement(element: JKTreeElement): JKTreeElement {
|
||||||
@@ -19,6 +22,7 @@ class InnerClassConversion : RecursiveApplicableConversionBase() {
|
|||||||
|
|
||||||
private fun applyArmed(element: JKTreeElement, outer: JKClass): JKTreeElement {
|
private fun applyArmed(element: JKTreeElement, outer: JKClass): JKTreeElement {
|
||||||
if (element !is JKClass) return recurseArmed(element, outer)
|
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)
|
if (element.isLocalClass()) return recurseArmed(element, outer)
|
||||||
|
|
||||||
val static = element.extraModifiers.find { it == ExtraModifier.STATIC }
|
val static = element.extraModifiers.find { it == ExtraModifier.STATIC }
|
||||||
|
|||||||
@@ -482,8 +482,24 @@ fun equalsExpression(left: JKExpression, right: JKExpression, symbolProvider: JK
|
|||||||
symbolProvider
|
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 =
|
fun JKClass.getOrCreateCompainonObject(): JKClass =
|
||||||
(declarationList.firstOrNull { it is JKClass && it.classKind == JKClass.ClassKind.COMPANION } as? JKClass)
|
getCompanion()
|
||||||
?: JKClassImpl(
|
?: JKClassImpl(
|
||||||
JKNameIdentifierImpl(""),
|
JKNameIdentifierImpl(""),
|
||||||
JKInheritanceInfoImpl(emptyList(), emptyList()),
|
JKInheritanceInfoImpl(emptyList(), emptyList()),
|
||||||
|
|||||||
Reference in New Issue
Block a user