New J2K: Don't move initilizer to constructor if types are not the same

This commit is contained in:
Ilya Kirillov
2018-10-09 16:34:18 +03:00
committed by Ilya Kirillov
parent baa9b714d1
commit 07f99fe0d2
2 changed files with 22 additions and 3 deletions
@@ -5,7 +5,6 @@
package org.jetbrains.kotlin.j2k.conversions
import org.jetbrains.kotlin.j2k.copyTree
import org.jetbrains.kotlin.j2k.tree.*
import org.jetbrains.kotlin.j2k.tree.impl.*
@@ -29,8 +28,9 @@ class FieldInitializersInPrimaryFromParamsConversion : TransformerBasedConversio
if (ktPrimaryConstructor.parameters.contains(parameter)) {
val fieldDeclaration = containingClass.declarationList.find {
(it as? JKField)?.name?.value == fieldTarget.name.value
} ?: continue
parameter.modifierList = (fieldDeclaration as JKField).modifierList.also { it.detach(it.parent!!) }
} as? JKField ?: continue
if (!fieldDeclaration.type.type.equalsByName(parameter.type.type)) continue//TODO better way to compare types??
parameter.modifierList = fieldDeclaration.modifierList.also { it.detach(it.parent!!) }
containingClass.declarationList -= fieldDeclaration
if (parameter.name.value != fieldTarget.name.value) {
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.j2k.tree.impl
import com.intellij.psi.PsiClass
import org.jetbrains.kotlin.j2k.ast.Mutability
import org.jetbrains.kotlin.j2k.ast.Nullability
import org.jetbrains.kotlin.j2k.tree.*
@@ -23,6 +24,7 @@ import org.jetbrains.kotlin.j2k.tree.JKLiteralExpression.LiteralType
import org.jetbrains.kotlin.j2k.tree.JKLiteralExpression.LiteralType.BOOLEAN
import org.jetbrains.kotlin.j2k.tree.JKLiteralExpression.LiteralType.NULL
import org.jetbrains.kotlin.j2k.tree.visitors.JKVisitor
import org.jetbrains.kotlin.psi.KtClass
class JKFileImpl : JKFile, JKBranchElementBase() {
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitFile(this, data)
@@ -195,6 +197,23 @@ class JKUnresolvedClassType(
override val nullability: Nullability = Nullability.Default
) : JKParametrizedType
fun JKType.fqName(): String =
when (this) {
is JKClassType -> {
val target = classReference?.target
when (target) {
is KtClass -> target.fqName?.asString() ?: throw RuntimeException("FqName can not be calculated")
is PsiClass -> target.qualifiedName ?: throw RuntimeException("FqName can not be calculated")
else -> TODO(target.toString())
}
}
is JKUnresolvedClassType -> name
is JKJavaPrimitiveType -> jvmPrimitiveType.name
else -> TODO(toString())
}
fun JKType.equalsByName(other: JKType) = fqName() == other.fqName()
class JKNullLiteral : JKLiteralExpression, JKElementBase() {
override val literal: String