New J2K: handle more types of nullability annotations
This commit is contained in:
+30
-30
@@ -6,6 +6,8 @@
|
|||||||
package org.jetbrains.kotlin.nj2k.conversions
|
package org.jetbrains.kotlin.nj2k.conversions
|
||||||
|
|
||||||
import org.jetbrains.kotlin.j2k.ast.Nullability
|
import org.jetbrains.kotlin.j2k.ast.Nullability
|
||||||
|
import org.jetbrains.kotlin.load.java.NOT_NULL_ANNOTATIONS
|
||||||
|
import org.jetbrains.kotlin.load.java.NULLABLE_ANNOTATIONS
|
||||||
import org.jetbrains.kotlin.nj2k.ConversionContext
|
import org.jetbrains.kotlin.nj2k.ConversionContext
|
||||||
import org.jetbrains.kotlin.nj2k.tree.*
|
import org.jetbrains.kotlin.nj2k.tree.*
|
||||||
import org.jetbrains.kotlin.nj2k.tree.impl.JKTypeElementImpl
|
import org.jetbrains.kotlin.nj2k.tree.impl.JKTypeElementImpl
|
||||||
@@ -14,40 +16,38 @@ import org.jetbrains.kotlin.nj2k.tree.impl.JKTypeElementImpl
|
|||||||
class JetbrainsNullableAnnotationsConverter(private val context: ConversionContext) : RecursiveApplicableConversionBase() {
|
class JetbrainsNullableAnnotationsConverter(private val context: ConversionContext) : RecursiveApplicableConversionBase() {
|
||||||
override fun applyToElement(element: JKTreeElement): JKTreeElement {
|
override fun applyToElement(element: JKTreeElement): JKTreeElement {
|
||||||
if (element !is JKAnnotationListOwner) return recurse(element)
|
if (element !is JKAnnotationListOwner) return recurse(element)
|
||||||
val nullableAnnotationSymbol =
|
|
||||||
context.symbolProvider.provideByFqName("org.jetbrains.annotations.Nullable")
|
val annotationsToRemove = mutableListOf<JKAnnotation>()
|
||||||
val notNullAnnotationSymbol =
|
for (annotation in element.annotationList.annotations) {
|
||||||
context.symbolProvider.provideByFqName("org.jetbrains.annotations.NotNull")
|
val nullability = annotation.annotationNullability() ?: continue
|
||||||
val nullableAnnotation =
|
when (element) {
|
||||||
element.annotationList.annotations.firstOrNull { it.classSymbol == nullableAnnotationSymbol }
|
is JKVariable -> {
|
||||||
val notNullAnnotation =
|
annotationsToRemove += annotation
|
||||||
element.annotationList.annotations.firstOrNull { it.classSymbol == notNullAnnotationSymbol }
|
element.type = JKTypeElementImpl(element.type.type.updateNullability(nullability))
|
||||||
when (element) {
|
|
||||||
is JKVariable -> {
|
|
||||||
if (nullableAnnotation != null) {
|
|
||||||
element.annotationList.annotations -= nullableAnnotation
|
|
||||||
element.type =
|
|
||||||
JKTypeElementImpl(element.type.type.updateNullability(Nullability.Nullable))
|
|
||||||
}
|
}
|
||||||
if (notNullAnnotation != null) {
|
is JKMethod -> {
|
||||||
element.annotationList.annotations -= notNullAnnotation
|
annotationsToRemove += annotation
|
||||||
element.type =
|
element.returnType = JKTypeElementImpl(element.returnType.type.updateNullability(nullability))
|
||||||
JKTypeElementImpl(element.type.type.updateNullability(Nullability.NotNull))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
is JKMethod -> {
|
|
||||||
if (nullableAnnotation != null) {
|
|
||||||
element.annotationList.annotations -= nullableAnnotation
|
|
||||||
element.returnType =
|
|
||||||
JKTypeElementImpl(element.returnType.type.updateNullability(Nullability.Nullable))
|
|
||||||
}
|
|
||||||
if (notNullAnnotation != null) {
|
|
||||||
element.annotationList.annotations -= notNullAnnotation
|
|
||||||
element.returnType =
|
|
||||||
JKTypeElementImpl(element.returnType.type.updateNullability(Nullability.NotNull))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
element.annotationList.annotations -= annotationsToRemove
|
||||||
|
|
||||||
return recurse(element)
|
return recurse(element)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun JKAnnotation.annotationNullability(): Nullability? =
|
||||||
|
when {
|
||||||
|
classSymbol.fqName in nullableAnnotationsFqNames -> Nullability.Nullable
|
||||||
|
classSymbol.fqName in notNullAnnotationsFqNames -> Nullability.NotNull
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
val nullableAnnotationsFqNames =
|
||||||
|
NULLABLE_ANNOTATIONS.map { it.asString() }.toSet()
|
||||||
|
val notNullAnnotationsFqNames =
|
||||||
|
NOT_NULL_ANNOTATIONS.map { it.asString() }.toSet()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user