New J2K: Add TypeParametersNullabilityConversion
This commit is contained in:
committed by
Ilya Kirillov
parent
6dae9da7e2
commit
9b73db4308
@@ -58,6 +58,7 @@ object ConversionsRunner {
|
||||
+ForConversion(context)
|
||||
+ForInConversion()
|
||||
+CollectionOperationsConversion(context)
|
||||
+TypeParametersNullabilityConversion()
|
||||
+ArrayOperationsConversion(context)
|
||||
+TypeMappingConversion(context)
|
||||
+ImplicitCastsConversion(context)
|
||||
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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.ast.Nullability
|
||||
import org.jetbrains.kotlin.j2k.tree.*
|
||||
import org.jetbrains.kotlin.j2k.tree.impl.*
|
||||
|
||||
|
||||
class TypeParametersNullabilityConversion : RecursiveApplicableConversionBase() {
|
||||
override fun applyToElement(element: JKTreeElement): JKTreeElement {
|
||||
if (element !is JKTypeParameterList) return recurse(element)
|
||||
val typeParametersNullabilityMap =
|
||||
element.typeParameters.map { typeParameter ->
|
||||
typeParameter.name.value to typeParameter.hasNullableUpperBounds()
|
||||
}.toMap()
|
||||
return JKTypeParameterListImpl(
|
||||
element.typeParameters.map { typeParameter ->
|
||||
val name = typeParameter.name.value
|
||||
JKTypeParameterImpl(
|
||||
JKNameIdentifierImpl(name),
|
||||
typeParameter.upperBounds.map { upperBoundTypeElement ->
|
||||
JKTypeElementImpl(
|
||||
upperBoundTypeElement.type.makeTypeParametersNotNull(typeParametersNullabilityMap)
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private fun JKType.makeTypeParametersNotNull(nullabilityMap: Map<String, Boolean>): JKType =
|
||||
when (this) {
|
||||
is JKClassType ->
|
||||
JKClassTypeImpl(
|
||||
classReference,
|
||||
parameters.map { it.makeTypeParametersNotNull(nullabilityMap) },
|
||||
nullability
|
||||
)
|
||||
is JKTypeParameterType ->
|
||||
nullabilityMap[name]
|
||||
?.takeIf { nullability == Nullability.Default }
|
||||
?.let { isNullable ->
|
||||
JKTypeParameterTypeImpl(
|
||||
name,
|
||||
if (isNullable) Nullability.NotNull else Nullability.Nullable
|
||||
)
|
||||
} ?: this
|
||||
|
||||
is JKVarianceTypeParameterType ->
|
||||
JKVarianceTypeParameterTypeImpl(
|
||||
variance,
|
||||
boundType.makeTypeParametersNotNull(nullabilityMap)
|
||||
)
|
||||
else -> this
|
||||
}
|
||||
|
||||
private fun JKTypeParameter.hasNullableUpperBounds(): Boolean =
|
||||
upperBounds.any { it.type.isNullable() }
|
||||
}
|
||||
Reference in New Issue
Block a user