Describe default settings for java nullability annotations depending on current kotlin version

This commit is contained in:
Victor Petukhov
2021-06-01 12:11:43 +03:00
parent 205087cae3
commit 8e7c0e8c61
3 changed files with 117 additions and 0 deletions
@@ -0,0 +1,64 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.load.java
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.isChildOf
val JSPECIFY_ANNOTATIONS_PACKAGE = FqName("org.jspecify.nullness")
val CHECKER_FRAMEWORK_COMPATQUAL_ANNOTATIONS_PACKAGE = FqName("org.checkerframework.checker.nullness.compatqual")
val nullabilityAnnotationSettings = mapOf(
FqName("org.jetbrains.annotations") to JavaNullabilityAnnotationsStatus.DEFAULT,
FqName("androidx.annotation") to JavaNullabilityAnnotationsStatus.DEFAULT,
FqName("android.support.annotation") to JavaNullabilityAnnotationsStatus.DEFAULT,
FqName("android.annotation") to JavaNullabilityAnnotationsStatus.DEFAULT,
FqName("com.android.annotations") to JavaNullabilityAnnotationsStatus.DEFAULT,
FqName("org.eclipse.jdt.annotation") to JavaNullabilityAnnotationsStatus.DEFAULT,
FqName("org.checkerframework.checker.nullness.qual") to JavaNullabilityAnnotationsStatus.DEFAULT,
CHECKER_FRAMEWORK_COMPATQUAL_ANNOTATIONS_PACKAGE to JavaNullabilityAnnotationsStatus.DEFAULT,
FqName("javax.annotation") to JavaNullabilityAnnotationsStatus.DEFAULT,
FqName("edu.umd.cs.findbugs.annotations") to JavaNullabilityAnnotationsStatus.DEFAULT,
FqName("io.reactivex.annotations") to JavaNullabilityAnnotationsStatus.DEFAULT,
FqName("lombok") to JavaNullabilityAnnotationsStatus.DEFAULT,
JSPECIFY_ANNOTATIONS_PACKAGE to JavaNullabilityAnnotationsStatus(
reportLevelBefore = ReportLevel.WARN,
sinceVersion = KotlinVersion(1, 6),
reportLevelAfter = ReportLevel.STRICT
),
)
val jsr305Settings = JavaNullabilityAnnotationsStatus(
reportLevelBefore = ReportLevel.WARN,
sinceVersion = null
)
fun getDefaultReportLevelForAnnotation(annotationFqName: FqName) = getReportLevelForAnnotation(annotationFqName, emptyMap())
fun getReportLevelForAnnotation(annotationFqName: FqName, configuredReportLevels: Map<FqName, ReportLevel>): ReportLevel {
val configuredReportLevel = configuredReportLevels.entries.find { (fqName, _) ->
annotationFqName == fqName || annotationFqName.isChildOf(fqName)
}?.value
if (configuredReportLevel != null) return configuredReportLevel
val defaultReportLevel = nullabilityAnnotationSettings.entries.find { (fqName, _) ->
annotationFqName == fqName || annotationFqName.isChildOf(fqName)
}?.value
require(defaultReportLevel != null) {
println(nullabilityAnnotationSettings)
"Default settings for nullability annotation ${annotationFqName.asString()} must be described"
}
return if (defaultReportLevel.sinceVersion != null && defaultReportLevel.sinceVersion <= KotlinVersion.CURRENT) {
defaultReportLevel.reportLevelAfter
} else {
defaultReportLevel.reportLevelBefore
}
}
@@ -0,0 +1,16 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.load.java
data class JavaNullabilityAnnotationsStatus(
val reportLevelBefore: ReportLevel,
val sinceVersion: KotlinVersion? = KotlinVersion(1, 0),
val reportLevelAfter: ReportLevel = reportLevelBefore,
) {
companion object {
val DEFAULT = JavaNullabilityAnnotationsStatus(ReportLevel.STRICT)
}
}
@@ -0,0 +1,37 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.load.java
import org.jetbrains.kotlin.name.FqName
data class Jsr305Settings(
val globalLevel: ReportLevel,
val migrationLevel: ReportLevel? = null,
val userDefinedLevelForSpecificAnnotation: Map<FqName, ReportLevel> = emptyMap()
) {
companion object {
val DEFAULT by lazy {
val reportLevelBefore = if (jsr305Settings.sinceVersion != null && jsr305Settings.sinceVersion <= KotlinVersion.CURRENT) {
jsr305Settings.reportLevelBefore
} else jsr305Settings.reportLevelAfter
Jsr305Settings(reportLevelBefore, if (reportLevelBefore == ReportLevel.WARN) null else reportLevelBefore)
}
}
@OptIn(ExperimentalStdlibApi::class)
val description by lazy {
buildList {
add(globalLevel.description)
migrationLevel?.let { add("under-migration:${it.description}") }
userDefinedLevelForSpecificAnnotation.forEach { add("@${it.key}:${it.value.description}") }
}.toTypedArray()
}
val isDisabled = globalLevel == ReportLevel.IGNORE
&& migrationLevel == ReportLevel.IGNORE
&& userDefinedLevelForSpecificAnnotation.isEmpty()
}