New J2K: Add conversion of Java instanceof expression to Kotlin is expression

This commit is contained in:
Ilya Kirillov
2018-10-04 15:35:46 +03:00
committed by Ilya Kirillov
parent c3ffaa34f8
commit 19c569e8e2
9 changed files with 77 additions and 0 deletions
@@ -48,6 +48,7 @@ object ConversionsRunner {
+PolyadicExpressionConversion()
+BinaryExpressionConversion()
+SwitchStatementConversion(context)
+InstanceofConversion()
}
fun doApply(trees: List<JKTreeElement>, context: ConversionContext) {
@@ -173,6 +173,13 @@ class NewCodeBuilder {
printer.printlnWithNoIndent()
}
override fun visitKtIsExpression(ktIsExpression: JKKtIsExpression) {
ktIsExpression.expression.accept(this)
printer.printWithNoIndent(" is ")
ktIsExpression.type.accept(this)
}
override fun visitParameter(parameter: JKParameter) {
printer.printWithNoIndent(parameter.name.value, ": ")
parameter.type.accept(this)
@@ -339,6 +346,8 @@ class NewCodeBuilder {
is JKClassType -> type.classReference.fqName?.let { printer.printWithNoIndent(FqName(it).shortName().asString()) }
is JKUnresolvedClassType -> printer.printWithNoIndent(type.name)
is JKContextType -> return
is JKStarProjectionType ->
printer.printWithNoIndent("*")
else -> printer.printWithNoIndent("Unit /* TODO: ${type::class} */")
}
if (type is JKParametrizedType && type.parameters.isNotEmpty()) {
@@ -0,0 +1,45 @@
/*
* 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 com.intellij.psi.PsiClass
import org.jetbrains.kotlin.j2k.tree.JKBinaryExpression
import org.jetbrains.kotlin.j2k.tree.JKClassType
import org.jetbrains.kotlin.j2k.tree.JKJavaInstanceOfExpression
import org.jetbrains.kotlin.j2k.tree.JKTreeElement
import org.jetbrains.kotlin.j2k.tree.impl.*
import org.jetbrains.kotlin.psi.KtClass
class InstanceofConversion : RecursiveApplicableConversionBase() {
override fun applyToElement(element: JKTreeElement): JKTreeElement {
if (element is JKJavaInstanceOfExpression) {
val checkingType = element.type.type
val type =
when (checkingType) {
is JKClassType -> {
if (checkingType.parameters.isEmpty()) {
val resolvedClass = checkingType.classReference?.target
val parametersCount =
when (resolvedClass) {
is PsiClass -> resolvedClass.typeParameters.size
is KtClass -> resolvedClass.typeParameters.size
else -> 0
}
val typeParameters = List(parametersCount) { JKStarProjectionTypeImpl() }
JKClassTypeImpl(checkingType.classReference as JKClassSymbol,
typeParameters,
checkingType.nullability)
} else checkingType
}
else -> checkingType
}
return recurse(JKKtIsExpressionImpl(element.expression.also { it.detach(it.parent!!) }, JKTypeElementImpl(type)))
}
return recurse(element)
}
}
@@ -187,6 +187,7 @@ class JKClassTypeImpl(
override val nullability: Nullability = Nullability.Default
) : JKClassType
class JKStarProjectionTypeImpl : JKStarProjectionType
class JKUnresolvedClassType(
val name: String,
@@ -245,3 +245,11 @@ class JKKtValueWhenLabelImpl(expression: JKExpression) : JKKtValueWhenLabel, JKB
override var expression: JKExpression by child(expression)
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitKtValueWhenLabel(this, data)
}
class JKKtIsExpressionImpl(expression: JKExpression, type: JKTypeElement) : JKKtIsExpression, JKBranchElementBase() {
override var type by child(type)
override var expression by child(expression)
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitKtIsExpression(this, data)
}
@@ -74,4 +74,9 @@ interface JKKtElseWhenLabel : JKKtWhenLabel
interface JKKtValueWhenLabel : JKKtWhenLabel {
var expression: JKExpression
}
interface JKKtIsExpression : JKExpression {
var expression: JKExpression
var type: JKTypeElement
}
@@ -68,6 +68,11 @@ interface JKJavaArrayType : JKType {
val type: JKType
}
interface JKStarProjectionType : JKType {
override val nullability: Nullability
get() = Nullability.NotNull
}
inline fun <reified T> JKElement.getParentOfType(): T? {
var p = parent
while (true) {
@@ -88,4 +88,5 @@ interface JKVisitor<out R, in D> {
fun visitKtWhenLabel(ktWhenLabel: JKKtWhenLabel, data: D): R = visitTreeElement(ktWhenLabel, data)
fun visitKtElseWhenLabel(ktElseWhenLabel: JKKtElseWhenLabel, data: D): R = visitKtWhenLabel(ktElseWhenLabel, data)
fun visitKtValueWhenLabel(ktValueWhenLabel: JKKtValueWhenLabel, data: D): R = visitKtWhenLabel(ktValueWhenLabel, data)
fun visitKtIsExpression(ktIsExpression: JKKtIsExpression, data: D): R = visitExpression(ktIsExpression, data)
}
@@ -173,4 +173,6 @@ interface JKVisitorVoid : JKVisitor<Unit, Nothing?> {
override fun visitKtElseWhenLabel(ktElseWhenLabel: JKKtElseWhenLabel, data: Nothing?) = visitKtElseWhenLabel(ktElseWhenLabel)
fun visitKtValueWhenLabel(ktValueWhenLabel: JKKtValueWhenLabel) = visitKtWhenLabel(ktValueWhenLabel, null)
override fun visitKtValueWhenLabel(ktValueWhenLabel: JKKtValueWhenLabel, data: Nothing?) = visitKtValueWhenLabel(ktValueWhenLabel)
fun visitKtIsExpression(ktIsExpression: JKKtIsExpression) = visitExpression(ktIsExpression, null)
override fun visitKtIsExpression(ktIsExpression: JKKtIsExpression, data: Nothing?) = visitKtIsExpression(ktIsExpression)
}