Implement caching states for nullability annotations

This commit is contained in:
Victor Petukhov
2021-06-11 10:28:51 +03:00
parent a82772f31a
commit b2dff10e32
4 changed files with 81 additions and 52 deletions
@@ -65,8 +65,9 @@ class JavaTypeEnhancementStateParser(
}
}
private fun parseNullabilityAnnotationReportLevels(nullabilityAnnotations: Array<String>?): Map<FqName, ReportLevel> {
if (nullabilityAnnotations.isNullOrEmpty()) return emptyMap()
private fun parseNullabilityAnnotationReportLevels(nullabilityAnnotations: Array<String>?): NullabilityAnnotationStates<out ReportLevel> {
if (nullabilityAnnotations.isNullOrEmpty())
return NullabilityAnnotationStates.EMPTY
val annotationsWithReportLevels = mutableMapOf<FqName, ReportLevel>()
val compilerOption = "-Xnullability-annotations"
@@ -87,7 +88,7 @@ class JavaTypeEnhancementStateParser(
}
}
return annotationsWithReportLevels
return NullabilityAnnotationStatesImpl(annotationsWithReportLevels)
}
private fun parseJspecifyReportLevel(
@@ -198,6 +199,6 @@ class JavaTypeEnhancementStateParser(
private val DEFAULT = JavaTypeEnhancementStateParser(MessageCollector.NONE, KotlinVersion.CURRENT)
fun parsePlainNullabilityAnnotationReportLevels(nullabilityAnnotations: String) =
DEFAULT.parseNullabilityAnnotationReportLevels(arrayOf(nullabilityAnnotations)).entries.singleOrNull()?.toPair()
DEFAULT.parseNullabilityAnnotationReportLevels(arrayOf(nullabilityAnnotations)).singleOrNull()
}
}
@@ -56,12 +56,14 @@ open class JvmForeignAnnotationsConfigurator(testServices: TestServices) : Envir
val state = ReportLevel.findByDescription(stateDescription) ?: return@mapNotNull null
FqName(name) to state
}.toMap()
val configuredReportLevels = buildMap<FqName, ReportLevel> {
directives.singleOrZeroValue(JSPECIFY_STATE)?.let { put(JSPECIFY_ANNOTATIONS_PACKAGE, it) }
for ((fqname, reportLevel) in directives[ForeignAnnotationsDirectives.NULLABILITY_ANNOTATIONS]) {
put(fqname, reportLevel)
val configuredReportLevels = NullabilityAnnotationStatesImpl(
buildMap<FqName, ReportLevel> {
directives.singleOrZeroValue(JSPECIFY_STATE)?.let { put(JSPECIFY_ANNOTATIONS_PACKAGE, it) }
for ((fqname, reportLevel) in directives[ForeignAnnotationsDirectives.NULLABILITY_ANNOTATIONS]) {
put(fqname, reportLevel)
}
}
}
)
return mapOf(
JvmAnalysisFlags.javaTypeEnhancementState to JavaTypeEnhancementState(
@@ -6,25 +6,28 @@
package org.jetbrains.kotlin.jvm.compiler
import org.jetbrains.kotlin.load.java.NULLABILITY_ANNOTATIONS
import org.jetbrains.kotlin.load.java.nullabilityAnnotationSettings
import org.jetbrains.kotlin.load.java.NULLABILITY_ANNOTATION_SETTINGS
import org.jetbrains.kotlin.load.java.NullabilityAnnotationStatesImpl
import org.jetbrains.kotlin.name.isChildOf
import org.jetbrains.kotlin.test.testFramework.KtUsefulTestCase
class AllNullabilityAnnotationsAreSetUpTest : KtUsefulTestCase() {
fun testAllAnnotationsAreSetUp() {
assert(NULLABILITY_ANNOTATIONS.all { annotation -> nullabilityAnnotationSettings.keys.any { annotation.isChildOf(it) } }) {
val annotationsRawMap = (NULLABILITY_ANNOTATION_SETTINGS as NullabilityAnnotationStatesImpl).states
assert(NULLABILITY_ANNOTATIONS.all { annotation -> annotationsRawMap.keys.any { annotation.isChildOf(it) } }) {
val missedAnnotations = NULLABILITY_ANNOTATIONS.filter { annotation ->
nullabilityAnnotationSettings.keys.none { annotation.isChildOf(it) }
annotationsRawMap.keys.none { annotation.isChildOf(it) }
}
"Not all nullability annotations are presented in `nullabilityAnnotationSettings`. Missed annotations: $missedAnnotations"
}
}
fun testAllSetUpAnnotationsArePresent() {
assert(nullabilityAnnotationSettings.keys.all { annotationsPackage ->
val annotationsRawMap = (NULLABILITY_ANNOTATION_SETTINGS as NullabilityAnnotationStatesImpl).states
assert(annotationsRawMap.keys.all { annotationsPackage ->
NULLABILITY_ANNOTATIONS.any { it.isChildOf(annotationsPackage) }
}) {
val missedAnnotations = nullabilityAnnotationSettings.keys.filter { annotationsPackage ->
val missedAnnotations = annotationsRawMap.keys.filter { annotationsPackage ->
NULLABILITY_ANNOTATIONS.none { it.isChildOf(annotationsPackage) }
}
"Not all set up nullability annotations are presented in `NULLABILITY_ANNOTATIONS`. Missed annotations: $missedAnnotations"