New J2K: Convert Java method to Kotlin function independently of containing class
This commit is contained in:
committed by
Ilya Kirillov
parent
f28f340840
commit
86fa54fae9
+29
-40
@@ -23,46 +23,35 @@ import org.jetbrains.kotlin.nj2k.tree.*
|
|||||||
import org.jetbrains.kotlin.nj2k.tree.impl.JKKtFunctionImpl
|
import org.jetbrains.kotlin.nj2k.tree.impl.JKKtFunctionImpl
|
||||||
import org.jetbrains.kotlin.nj2k.tree.impl.psi
|
import org.jetbrains.kotlin.nj2k.tree.impl.psi
|
||||||
|
|
||||||
class JavaMethodToKotlinFunctionConversion(private val context: ConversionContext) : TransformerBasedConversion() {
|
class JavaMethodToKotlinFunctionConversion(private val context: ConversionContext) : RecursiveApplicableConversionBase() {
|
||||||
override fun visitTreeElement(element: JKTreeElement) {
|
override fun applyToElement(element: JKTreeElement): JKTreeElement {
|
||||||
element.acceptChildren(this, null)
|
if (element !is JKJavaMethod) return recurse(element)
|
||||||
}
|
|
||||||
|
|
||||||
override fun visitClassBody(classBody: JKClassBody) {
|
element.invalidate()
|
||||||
somethingChanged = true
|
val kotlinFunction = JKKtFunctionImpl(
|
||||||
|
element.returnType,
|
||||||
classBody.declarations = classBody.declarations.map { declaration ->
|
element.name,
|
||||||
if (declaration is JKJavaMethod) {
|
element.parameters,
|
||||||
declaration.invalidate()
|
element.block,
|
||||||
|
element.typeParameterList,
|
||||||
JKKtFunctionImpl(
|
element.annotationList.also {
|
||||||
declaration.returnType,
|
if (element.throwsList.isNotEmpty()) {
|
||||||
declaration.name,
|
it.annotations +=
|
||||||
declaration.parameters,
|
throwAnnotation(
|
||||||
declaration.block,
|
element.throwsList.map { it.type.updateNullabilityRecursively(Nullability.NotNull) },
|
||||||
declaration.typeParameterList,
|
context.symbolProvider
|
||||||
declaration.annotationList.also {
|
)
|
||||||
if (declaration.throwsList.isNotEmpty()) {
|
}
|
||||||
it.annotations +=
|
},
|
||||||
throwAnnotation(
|
element.extraModifierElements,
|
||||||
declaration.throwsList.map { it.type.updateNullabilityRecursively(Nullability.NotNull) },
|
element.visibilityElement,
|
||||||
context.symbolProvider
|
element.modalityElement
|
||||||
)
|
).also {
|
||||||
}
|
it.psi = element.psi
|
||||||
},
|
context.symbolProvider.transferSymbol(it, element)
|
||||||
declaration.extraModifierElements,
|
it.leftParen.takeNonCodeElementsFrom(element.leftParen)
|
||||||
declaration.visibilityElement,
|
it.rightParen.takeNonCodeElementsFrom(element.rightParen)
|
||||||
declaration.modalityElement
|
}.withNonCodeElementsFrom(element)
|
||||||
).also {
|
return recurse(kotlinFunction)
|
||||||
it.psi = declaration.psi
|
|
||||||
context.symbolProvider.transferSymbol(it, declaration)
|
|
||||||
it.leftParen.takeNonCodeElementsFrom(declaration.leftParen)
|
|
||||||
it.rightParen.takeNonCodeElementsFrom(declaration.rightParen)
|
|
||||||
}.withNonCodeElementsFrom(declaration)
|
|
||||||
} else {
|
|
||||||
declaration
|
|
||||||
}
|
|
||||||
}
|
|
||||||
classBody.acceptChildren(this)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user