New J2K: Fix nullability calculation

This commit is contained in:
Ilya Kirillov
2019-01-17 14:47:47 +03:00
committed by Ilya Kirillov
parent 8aa28c0d50
commit 50278058ae
2 changed files with 34 additions and 6 deletions
@@ -116,6 +116,7 @@ class TypeMappingConversion(val context: ConversionContext) : RecursiveApplicabl
} }
private fun JKClassSymbol.mapClassSymbol(typeElement: JKTypeElement?): JKClassSymbol { private fun JKClassSymbol.mapClassSymbol(typeElement: JKTypeElement?): JKClassSymbol {
if (this is JKUniverseClassSymbol) return this
val newFqName = typeElement?.let { kotlinCollectionClassName(it) } val newFqName = typeElement?.let { kotlinCollectionClassName(it) }
?: kotlinStandardType() ?: kotlinStandardType()
?: fqName ?: fqName
@@ -131,7 +132,7 @@ class TypeMappingConversion(val context: ConversionContext) : RecursiveApplicabl
) )
private fun JKClassSymbol.kotlinCollectionClassName(typeElement: JKTypeElement): String? { private fun JKClassSymbol.kotlinCollectionClassName(typeElement: JKTypeElement?): String? {
val isStructureMutable = calculateStructureMutability(typeElement) val isStructureMutable = calculateStructureMutability(typeElement)
return if (isStructureMutable) toKotlinMutableTypesMap[fqName] return if (isStructureMutable) toKotlinMutableTypesMap[fqName]
else toKotlinTypesMap[fqName] else toKotlinTypesMap[fqName]
@@ -152,15 +153,27 @@ class TypeMappingConversion(val context: ConversionContext) : RecursiveApplicabl
private fun calculateNullability(typeElement: JKTypeElement?): Nullability { private fun calculateNullability(typeElement: JKTypeElement?): Nullability {
val parent = typeElement?.parent ?: return Nullability.Default val parent = typeElement?.parent ?: return Nullability.Default
val psi = parent.psi ?: return Nullability.Default val psi = parent.psi
return when (parent) { return when (parent) {
is JKJavaMethod -> typeFlavorCalculator.methodNullability(psi as PsiMethod) is JKMethod ->
is JKJavaField -> typeFlavorCalculator.variableNullability(psi as PsiVariable) parent.nullabilityBySuperMethod(context.symbolProvider).defaultToNull()
is JKVariable -> typeFlavorCalculator.variableNullability(psi as PsiVariable) ?: psi?.let { typeFlavorCalculator.methodNullability(it as PsiMethod) }
.nullToDefault()
is JKVariable -> psi?.let {
typeFlavorCalculator.variableNullability(psi as PsiVariable)
}.nullToDefault()
else -> Nullability.Default else -> Nullability.Default
} }
} }
private fun Nullability.defaultToNull() =
if (this == Nullability.Default) null else this
private fun Nullability?.nullToDefault() =
this ?: Nullability.Default
private fun calculateStructureMutability(typeElement: JKTypeElement?): Boolean { private fun calculateStructureMutability(typeElement: JKTypeElement?): Boolean {
val parent = typeElement?.parent ?: return false val parent = typeElement?.parent ?: return false
val psi = parent.psi ?: return false val psi = parent.psi ?: return false
@@ -316,3 +316,18 @@ fun JKClassSymbol.isInterface(): Boolean {
fun JKType.isInterface(): Boolean = fun JKType.isInterface(): Boolean =
(this as? JKClassType)?.classReference?.isInterface() ?: false (this as? JKClassType)?.classReference?.isInterface() ?: false
fun JKMethod.nullabilityBySuperMethod(symbolProvider: JKSymbolProvider): Nullability {
if (modality != Modality.OVERRIDE) return Nullability.Default
val superMethodSymbol = findSuperMethodSymbol(symbolProvider) ?: return Nullability.Default
return superMethodSymbol.returnType?.nullability ?: return Nullability.Default
}
private fun JKMethod.findSuperMethodSymbol(symbolProvider: JKSymbolProvider): JKMethodSymbol? =
psi<PsiMethod>()
?.findSuperMethods()
?.firstOrNull()
?.let {
symbolProvider.provideDirectSymbol(it) as? JKMethodSymbol
}