New J2K: Add conversion to promote classes with only companions into objects
This commit is contained in:
committed by
Ilya Kirillov
parent
fba5ac4c1a
commit
2844f8d5d9
@@ -38,6 +38,7 @@ object ConversionsRunner {
|
||||
InnerClassConversion(),
|
||||
ModifiersConversion(context),
|
||||
StaticsToCompanionExtractConversion(),
|
||||
ClassToObjectPromotionConversion(),
|
||||
PolyadicExpressionConversion(),
|
||||
BinaryExpressionConversion()
|
||||
)
|
||||
|
||||
@@ -329,12 +329,6 @@ class NewCodeBuilder {
|
||||
printer.printWithNoIndent(type.name)
|
||||
else -> printer.printWithNoIndent("Unit /* TODO: ${type::class} */")
|
||||
}
|
||||
when (type.nullability) {
|
||||
Nullability.Nullable -> printer.printWithNoIndent("?")
|
||||
Nullability.Default -> printer.printWithNoIndent("?")// /* TODO: Default */")
|
||||
else -> {
|
||||
}
|
||||
}
|
||||
if (type is JKParametrizedType && type.parameters.isNotEmpty()) {
|
||||
printer.par(ANGLE) {
|
||||
renderList(type.parameters) {
|
||||
@@ -342,6 +336,12 @@ class NewCodeBuilder {
|
||||
}
|
||||
}
|
||||
}
|
||||
when (type.nullability) {
|
||||
Nullability.Nullable -> printer.printWithNoIndent("?")
|
||||
Nullability.Default -> printer.printWithNoIndent("?")// /* TODO: Default */")
|
||||
else -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitTypeElement(typeElement: JKTypeElement) {
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.j2k.conversions
|
||||
|
||||
import org.jetbrains.kotlin.j2k.tree.JKClass
|
||||
import org.jetbrains.kotlin.j2k.tree.JKKtPrimaryConstructor
|
||||
import org.jetbrains.kotlin.j2k.tree.JKTreeElement
|
||||
import org.jetbrains.kotlin.j2k.tree.impl.JKClassImpl
|
||||
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<JKKtPrimaryConstructor>()
|
||||
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)
|
||||
.apply {
|
||||
declarationList = companion.declarationList
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
return recurse(element)
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,7 @@ interface JKFile : JKTreeElement, JKBranchElement {
|
||||
var declarationList: List<JKDeclaration>
|
||||
}
|
||||
|
||||
interface JKClass : JKDeclaration, JKModifierListOwner {
|
||||
interface JKClass : JKDeclaration, JKModifierListOwner, JKBranchElement {
|
||||
val name: JKNameIdentifier
|
||||
|
||||
val inheritance: JKInheritanceInfo
|
||||
|
||||
Reference in New Issue
Block a user