New J2K: Inner class conversion
This commit is contained in:
committed by
Ilya Kirillov
parent
c87cd20a9d
commit
e83844e941
@@ -32,6 +32,7 @@ object ConversionsRunner {
|
|||||||
AssignmentStatementOperatorConversion().runConversion(it, context)
|
AssignmentStatementOperatorConversion().runConversion(it, context)
|
||||||
JavaMethodToKotlinFunctionConversion().runConversion(it, context)
|
JavaMethodToKotlinFunctionConversion().runConversion(it, context)
|
||||||
LiteralConversion().runConversion(it, context)
|
LiteralConversion().runConversion(it, context)
|
||||||
|
InnerClassConversion().runConversion(it, context)
|
||||||
ModifiersConversion().runConversion(it, context)
|
ModifiersConversion().runConversion(it, context)
|
||||||
PolyadicExpressionConversion().runConversion(it, context)
|
PolyadicExpressionConversion().runConversion(it, context)
|
||||||
BinaryExpressionConversion().runConversion(it, context)
|
BinaryExpressionConversion().runConversion(it, context)
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* 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.*
|
||||||
|
import org.jetbrains.kotlin.j2k.tree.JKKtModifier.KtModifierType.INNER
|
||||||
|
import org.jetbrains.kotlin.j2k.tree.impl.JKKtModifierImpl
|
||||||
|
|
||||||
|
class InnerClassConversion : RecursiveApplicableConversionBase() {
|
||||||
|
override fun applyToElement(element: JKTreeElement): JKTreeElement {
|
||||||
|
if (element !is JKClass) return recurse(element)
|
||||||
|
return recurseArmed(element, element)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun recurseArmed(element: JKTreeElement, outer: JKClass): JKTreeElement {
|
||||||
|
return applyRecursive(element, outer, ::applyArmed)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun applyArmed(element: JKTreeElement, outer: JKClass): JKTreeElement {
|
||||||
|
if (element !is JKClass) return recurseArmed(element, outer)
|
||||||
|
|
||||||
|
val static = element.modifierList.modifiers.find { it is JKJavaModifier && it.type == JKJavaModifier.JavaModifierType.STATIC }
|
||||||
|
if (static != null) {
|
||||||
|
element.modifierList.modifiers -= static
|
||||||
|
} else if (element.classKind != JKClass.ClassKind.INTERFACE && outer.classKind != JKClass.ClassKind.INTERFACE) {
|
||||||
|
element.modifierList.modifiers += JKKtModifierImpl(INNER)
|
||||||
|
}
|
||||||
|
return recurseArmed(element, element)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -10,16 +10,16 @@ import org.jetbrains.kotlin.j2k.tree.impl.JKBranchElementBase
|
|||||||
|
|
||||||
abstract class MatchBasedConversion : BaseConversion() {
|
abstract class MatchBasedConversion : BaseConversion() {
|
||||||
|
|
||||||
fun applyRecursive(element: JKTreeElement, func: (JKTreeElement) -> JKTreeElement): JKTreeElement {
|
fun <T> applyRecursive(element: JKTreeElement, data: T, func: (JKTreeElement, T) -> JKTreeElement): JKTreeElement {
|
||||||
if (element is JKBranchElementBase) {
|
if (element is JKBranchElementBase) {
|
||||||
val iter = element.children.listIterator()
|
val iter = element.children.listIterator()
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
val child = iter.next()
|
val child = iter.next()
|
||||||
|
|
||||||
if (child is List<*>) {
|
if (child is List<*>) {
|
||||||
iter.set(applyRecursiveToList(element, child as List<JKTreeElement>, iter, func))
|
iter.set(applyRecursiveToList(element, child as List<JKTreeElement>, iter, data, func))
|
||||||
} else if (child is JKTreeElement) {
|
} else if (child is JKTreeElement) {
|
||||||
val newChild = func(child)
|
val newChild = func(child, data)
|
||||||
if (child !== newChild) {
|
if (child !== newChild) {
|
||||||
child.detach(element)
|
child.detach(element)
|
||||||
iter.set(newChild)
|
iter.set(newChild)
|
||||||
@@ -34,15 +34,20 @@ abstract class MatchBasedConversion : BaseConversion() {
|
|||||||
return element
|
return element
|
||||||
}
|
}
|
||||||
|
|
||||||
private inline fun applyRecursiveToList(
|
inline fun applyRecursive(element: JKTreeElement, crossinline func: (JKTreeElement) -> JKTreeElement): JKTreeElement {
|
||||||
|
return applyRecursive(element, null) { it, _ -> func(it) }
|
||||||
|
}
|
||||||
|
|
||||||
|
private inline fun <T> applyRecursiveToList(
|
||||||
element: JKTreeElement,
|
element: JKTreeElement,
|
||||||
child: List<JKTreeElement>,
|
child: List<JKTreeElement>,
|
||||||
iter: MutableListIterator<Any>,
|
iter: MutableListIterator<Any>,
|
||||||
func: (JKTreeElement) -> JKTreeElement
|
data: T,
|
||||||
|
func: (JKTreeElement, T) -> JKTreeElement
|
||||||
): List<JKTreeElement> {
|
): List<JKTreeElement> {
|
||||||
|
|
||||||
val newChild = child.map {
|
val newChild = child.map {
|
||||||
func(it)
|
func(it, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
child.forEach { it.detach(element) }
|
child.forEach { it.detach(element) }
|
||||||
|
|||||||
Reference in New Issue
Block a user