New J2K: Add InterfaceWithFieldConversion
This commit is contained in:
committed by
Ilya Kirillov
parent
1d846085de
commit
d09e72656a
@@ -72,6 +72,7 @@ object ConversionsRunner {
|
||||
//Kotlin --> Kotlin conversions
|
||||
+InnerClassConversion()
|
||||
+StaticsToCompanionExtractConversion()
|
||||
+InterfaceWithFieldConversion()
|
||||
+ClassToObjectPromotionConversion(context)
|
||||
+ImportStatementConversion()
|
||||
+SortClassMembersConversion()
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2010-2019 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.*
|
||||
import org.jetbrains.kotlin.j2k.tree.*
|
||||
|
||||
|
||||
class InterfaceWithFieldConversion : RecursiveApplicableConversionBase() {
|
||||
override fun applyToElement(element: JKTreeElement): JKTreeElement {
|
||||
if (element !is JKClass) return recurse(element)
|
||||
if (element.classKind != JKClass.ClassKind.INTERFACE) return recurse(element)
|
||||
val finalFields = element.declarationList
|
||||
.filterIsInstance<JKField>()
|
||||
.filter { it.modality == Modality.FINAL }
|
||||
if (finalFields.isNotEmpty()) {
|
||||
element.classBody.declarations -= finalFields
|
||||
val companion = element.getOrCreateCompainonObject()
|
||||
companion.classBody.declarations += finalFields
|
||||
}
|
||||
return recurse(element)
|
||||
}
|
||||
}
|
||||
+2
-25
@@ -5,6 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.j2k.conversions
|
||||
|
||||
import org.jetbrains.kotlin.j2k.getOrCreateCompainonObject
|
||||
import org.jetbrains.kotlin.j2k.tree.*
|
||||
import org.jetbrains.kotlin.j2k.tree.impl.*
|
||||
|
||||
@@ -16,38 +17,14 @@ class StaticsToCompanionExtractConversion : RecursiveApplicableConversionBase()
|
||||
declaration is JKExtraModifiersOwner &&
|
||||
declaration.extraModifiers.any { it == ExtraModifier.STATIC }
|
||||
}
|
||||
|
||||
if (statics.isEmpty()) return recurse(element)
|
||||
val companion = findOrCreateCompanion(element)
|
||||
|
||||
val companion = element.getOrCreateCompainonObject()
|
||||
|
||||
element.classBody.declarations -= statics
|
||||
companion.classBody.declarations += statics.onEach { declaration ->
|
||||
(declaration as JKExtraModifiersOwner)
|
||||
declaration.extraModifiers -= ExtraModifier.STATIC
|
||||
}
|
||||
|
||||
return recurse(element)
|
||||
}
|
||||
|
||||
fun findOrCreateCompanion(element: JKClass): JKClass {
|
||||
val companion = element.declarationList
|
||||
.asSequence()
|
||||
.filterIsInstance<JKClass>()
|
||||
.firstOrNull { it.classKind == JKClass.ClassKind.COMPANION }
|
||||
|
||||
if (companion != null) return companion
|
||||
|
||||
return JKClassImpl(
|
||||
JKNameIdentifierImpl(""),
|
||||
JKInheritanceInfoImpl(emptyList(), emptyList()),
|
||||
JKClass.ClassKind.COMPANION,
|
||||
JKTypeParameterListImpl(),
|
||||
JKClassBodyImpl(),
|
||||
JKAnnotationListImpl(),
|
||||
emptyList(),
|
||||
Visibility.PUBLIC,
|
||||
Modality.FINAL
|
||||
).also { element.classBody.declarations += it }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -449,3 +449,17 @@ fun equalsExpression(left: JKExpression, right: JKExpression, symbolProvider: JK
|
||||
KtTokens.EQEQ,
|
||||
symbolProvider
|
||||
)
|
||||
|
||||
fun JKClass.getOrCreateCompainonObject(): JKClass =
|
||||
(declarationList.firstOrNull { it is JKClass && it.classKind == JKClass.ClassKind.COMPANION } as? JKClass)
|
||||
?: JKClassImpl(
|
||||
JKNameIdentifierImpl(""),
|
||||
JKInheritanceInfoImpl(emptyList(), emptyList()),
|
||||
JKClass.ClassKind.COMPANION,
|
||||
JKTypeParameterListImpl(),
|
||||
JKClassBodyImpl(),
|
||||
JKAnnotationListImpl(),
|
||||
emptyList(),
|
||||
Visibility.PUBLIC,
|
||||
Modality.FINAL
|
||||
).also { classBody.declarations += it }
|
||||
Reference in New Issue
Block a user