From 3d62734cc6952f1d578aa3a9e05fb9a8b04a3a55 Mon Sep 17 00:00:00 2001 From: Ilya Kirillov Date: Mon, 24 Dec 2018 12:51:31 +0300 Subject: [PATCH] New J2K: Add types to JKExpression.type --- .../org/jetbrains/kotlin/j2k/tree/types.kt | 56 +++++++++++++++---- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/j2k/newSrc/org/jetbrains/kotlin/j2k/tree/types.kt b/j2k/newSrc/org/jetbrains/kotlin/j2k/tree/types.kt index 890871cbd26..5c51e7b9459 100644 --- a/j2k/newSrc/org/jetbrains/kotlin/j2k/tree/types.kt +++ b/j2k/newSrc/org/jetbrains/kotlin/j2k/tree/types.kt @@ -49,6 +49,21 @@ fun JKExpression.type(context: ConversionContext): JKType? = is JKTypeCastExpression -> type.type is JKThisExpression -> null// TODO return actual type is JKSuperExpression -> null// TODO return actual type + is JKStubExpression -> null + is JKIfElseExpression -> thenBranch.type(context)// TODO return actual type + is JKArrayAccessExpression -> + (expression.type(context) as? JKParametrizedType)?.parameters?.lastOrNull() + is JKClassLiteralExpression -> { + val symbol = when (literalType) { + JKClassLiteralExpression.LiteralType.KOTLIN_CLASS -> + context.symbolProvider.provideByFqName("kotlin.reflect.KClass") + JKClassLiteralExpression.LiteralType.JAVA_CLASS, + JKClassLiteralExpression.LiteralType.JAVA_PRIMITIVE_CLASS, JKClassLiteralExpression.LiteralType.JAVA_VOID_TYPE -> + context.symbolProvider.provideByFqName("java.lang.Class") + } + JKClassTypeImpl(symbol, listOf(classType.type), Nullability.NotNull) + } + else -> TODO(this::class.java.toString()) } @@ -108,21 +123,21 @@ fun JKType.isSubtypeOf(other: JKType, symbolProvider: JKSymbolProvider): Boolean ?.let { otherType -> this.toKtType(symbolProvider)?.isSubtypeOf(otherType) } == true -fun KtTypeElement.toJK(symbolProvider: JKSymbolProvider): JKType = - when (this) { +fun KtTypeElement.toJK(symbolProvider: JKSymbolProvider): JKType? { + return when (this) { is KtUserType -> { val qualifiedName = qualifier?.text?.let { it + "." }.orEmpty() + referencedName - val typeParameters = typeArguments.map { it.typeReference!!.typeElement!!.toJK(symbolProvider) } - val symbol = symbolProvider.provideDirectSymbol( - resolveFqName(ClassId.fromString(qualifiedName), this)!! - ) as JKClassSymbol - - JKClassTypeImpl(symbol, typeParameters) + val typeParameters = typeArguments.map { it.typeReference?.typeElement?.toJK(symbolProvider) } + if (typeParameters.any { it == null }) return null + val fqName = resolveFqName(ClassId.fromString(qualifiedName), this) ?: return null + val symbol = symbolProvider.provideDirectSymbol(fqName) as? JKClassSymbol ?: return null + JKClassTypeImpl(symbol, typeParameters as List) } is KtNullableType -> - innerType!!.toJK(symbolProvider).updateNullability(Nullability.Nullable) - else -> TODO(this::class.java.toString()) + innerType?.toJK(symbolProvider)?.updateNullability(Nullability.Nullable) + else -> null } +} fun JKType.toKtType(symbolProvider: JKSymbolProvider): KotlinType? = when (this) { @@ -161,6 +176,27 @@ inline fun T.updateNullability(newNullability: Nullability) is JKJavaVoidType -> this is JKJavaPrimitiveType -> this is JKJavaArrayType -> JKJavaArrayTypeImpl(type, newNullability) + is JKContextType -> JKContextType + is JKJavaDisjunctionType -> this + else -> TODO(this::class.toString()) + } as T + +fun T.updateNullabilityRecursively(newNullability: Nullability): T = + if (nullability == newNullability) this + else when (this) { + is JKTypeParameterTypeImpl -> JKTypeParameterTypeImpl(name, newNullability) + is JKClassTypeImpl -> + JKClassTypeImpl( + classReference, + parameters.map { it.updateNullabilityRecursively(newNullability) }, + newNullability + ) + is JKNoType -> this + is JKJavaVoidType -> this + is JKJavaPrimitiveType -> this + is JKJavaArrayType -> JKJavaArrayTypeImpl(type.updateNullabilityRecursively(newNullability), newNullability) + is JKContextType -> JKContextType + is JKJavaDisjunctionType -> this else -> TODO(this::class.toString()) } as T