New J2K: Added modifiers conversion and correct resolve of void type.
This commit is contained in:
@@ -29,6 +29,7 @@ object ConversionsRunner {
|
||||
AssignmentAsExpressionToAlsoConversion(context).runConversion(it, context)
|
||||
JavaMethodToKotlinFunctionConversion().runConversion(it, context)
|
||||
LiteralConversion().runConversion(it, context)
|
||||
ModifiersConversion().runConversion(it, context)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -57,6 +57,8 @@ class NewCodeBuilder {
|
||||
JKKtModifier.KtModifierType.ABSTRACT -> "abstract"
|
||||
JKKtModifier.KtModifierType.INNER -> "inner"
|
||||
JKKtModifier.KtModifierType.OPEN -> "open"
|
||||
JKKtModifier.KtModifierType.PRIVATE -> "private"
|
||||
JKKtModifier.KtModifierType.PROTECTED -> "protected"
|
||||
else -> ktModifier.type.toString()
|
||||
}
|
||||
)
|
||||
@@ -95,7 +97,7 @@ class NewCodeBuilder {
|
||||
}
|
||||
|
||||
override fun visitKtFunction(ktFunction: JKKtFunction) {
|
||||
printer.print("fun ", ktFunction.name.value, "(", "): ")
|
||||
printer.print("fun ", ktFunction.name.value, "(", ")")
|
||||
ktFunction.returnType.accept(this)
|
||||
if (ktFunction.block !== JKBodyStub) {
|
||||
printer.printlnWithNoIndent("{")
|
||||
@@ -156,7 +158,9 @@ class NewCodeBuilder {
|
||||
override fun visitTypeElement(typeElement: JKTypeElement) {
|
||||
val type = typeElement.type
|
||||
when (type) {
|
||||
is JKClassType -> (type.classReference as JKClassSymbol).fqName?.let { printer.printWithNoIndent(FqName(it).shortName().asString()) }
|
||||
is JKClassType -> if ((type.classReference as? JKClassSymbol)?.fqName != "kotlin.Unit") {
|
||||
(type.classReference as JKClassSymbol).fqName?.let { printer.printWithNoIndent(": " + FqName(it).shortName().asString()) }
|
||||
}
|
||||
else -> printer.printWithNoIndent("Unit /* TODO: ${type::class} */")
|
||||
}
|
||||
when (type.nullability) {
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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.impl.JKKtModifierImpl
|
||||
|
||||
class ModifiersConversion : RecursiveApplicableConversionBase() {
|
||||
|
||||
override fun applyToElement(element: JKTreeElement): JKTreeElement {
|
||||
return if (element is JKModifierList) element.also {
|
||||
var modifiers = it.modifiers
|
||||
if (element.parent !is JKLocalVariable && !modifiers.filter { it is JKJavaAccessModifier }.any()) {
|
||||
modifiers += JKKtModifierImpl(JKKtModifier.KtModifierType.INTERNAL)
|
||||
}
|
||||
it.modifiers = mapModifiers(modifiers)
|
||||
} else recurse(element)
|
||||
}
|
||||
|
||||
private fun mapModifiers(modifiers: List<JKModifier>): List<JKModifier> {
|
||||
return modifiers.mapNotNull {
|
||||
when (it) {
|
||||
is JKJavaAccessModifier -> when (it.type) {
|
||||
JKJavaAccessModifier.AccessModifierType.PUBLIC -> null
|
||||
JKJavaAccessModifier.AccessModifierType.PRIVATE -> JKKtModifierImpl(JKKtModifier.KtModifierType.PRIVATE)
|
||||
JKJavaAccessModifier.AccessModifierType.PROTECTED -> JKKtModifierImpl(JKKtModifier.KtModifierType.PROTECTED)
|
||||
}
|
||||
is JKJavaModifier -> when (it.type) {
|
||||
JKJavaModifier.JavaModifierType.ABSTRACT -> JKKtModifierImpl(JKKtModifier.KtModifierType.ABSTRACT)
|
||||
JKJavaModifier.JavaModifierType.FINAL -> null
|
||||
JKJavaModifier.JavaModifierType.NATIVE -> JKKtModifierImpl(JKKtModifier.KtModifierType.EXTERNAL)
|
||||
else -> it
|
||||
}
|
||||
else -> it
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,16 +11,12 @@ import org.jetbrains.kotlin.j2k.ast.Nullability
|
||||
import org.jetbrains.kotlin.j2k.tree.*
|
||||
import org.jetbrains.kotlin.j2k.tree.impl.JKClassSymbol
|
||||
import org.jetbrains.kotlin.j2k.tree.impl.JKClassTypeImpl
|
||||
import org.jetbrains.kotlin.j2k.tree.impl.JKJavaVoidType
|
||||
import org.jetbrains.kotlin.j2k.tree.impl.JKTypeElementImpl
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.platform.JavaToKotlinClassMap
|
||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
import org.jetbrains.kotlin.psi.analysisContext
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
|
||||
import org.jetbrains.kotlin.resolve.ImportPath
|
||||
|
||||
class TypeMappingConversion(val context: ConversionContext) : RecursiveApplicableConversionBase() {
|
||||
|
||||
@@ -30,6 +26,9 @@ class TypeMappingConversion(val context: ConversionContext) : RecursiveApplicabl
|
||||
when (type) {
|
||||
is JKJavaPrimitiveType -> mapPrimitiveType(type, element)
|
||||
is JKClassType -> mapClassType(type, element)
|
||||
is JKJavaVoidType -> classTypeByFqName(
|
||||
context.backAnnotator(element), ClassId.fromString("kotlin.Unit"), emptyList(), Nullability.NotNull
|
||||
)?.let { JKTypeElementImpl(it) } ?: element
|
||||
else -> applyRecursive(element, this::applyToElement)
|
||||
}
|
||||
} else applyRecursive(element, this::applyToElement)
|
||||
@@ -44,7 +43,6 @@ class TypeMappingConversion(val context: ConversionContext) : RecursiveApplicabl
|
||||
contextElement ?: return null
|
||||
val newTarget = resolveFqName(fqName, contextElement) as? KtClassOrObject ?: return null
|
||||
|
||||
|
||||
return JKClassTypeImpl(context.symbolProvider.provideSymbol(newTarget) as JKClassSymbol, parameters, nullability)
|
||||
}
|
||||
|
||||
|
||||
@@ -84,4 +84,8 @@ sealed class JKKtOperatorImpl : JKOperator, JKElementBase() {
|
||||
object MINUS : JKKtOperatorImpl()
|
||||
object EQEQ : JKKtOperatorImpl()
|
||||
object NE : JKKtOperatorImpl()
|
||||
}
|
||||
}
|
||||
|
||||
class JKKtModifierImpl(override val type: JKKtModifier.KtModifierType) : JKKtModifier, JKElementBase() {
|
||||
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitKtModifier(this, data)
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ interface JKKtModifier : JKModifier {
|
||||
|
||||
enum class KtModifierType {
|
||||
ACTUAL, ABSTRACT, ANNOTATION, COMPANION, CONST, CROSSINLINE, DATA, ENUM, EXPECT, EXTERNAL, FINAL, INFIX, INLINE, INNER,
|
||||
INTERNAL, LATEINIT, NOINLINE, OPEN, OPERATOR, OUT, OVERRIDE, REIFIED, SEALED, SUSPEND, TAILREC, VARARG
|
||||
INTERNAL, LATEINIT, NOINLINE, OPEN, OPERATOR, OUT, OVERRIDE, REIFIED, SEALED, SUSPEND, TAILREC, VARARG, PRIVATE, PROTECTED
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user