diff --git a/core/util.runtime/src/org/jetbrains/kotlin/load/java/JavaNullabilityAnnotationSettings.kt b/core/util.runtime/src/org/jetbrains/kotlin/load/java/JavaNullabilityAnnotationSettings.kt new file mode 100644 index 00000000000..1d4fb453367 --- /dev/null +++ b/core/util.runtime/src/org/jetbrains/kotlin/load/java/JavaNullabilityAnnotationSettings.kt @@ -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): 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 + } +} + + diff --git a/core/util.runtime/src/org/jetbrains/kotlin/load/java/JavaNullabilityAnnotationsStatus.kt b/core/util.runtime/src/org/jetbrains/kotlin/load/java/JavaNullabilityAnnotationsStatus.kt new file mode 100644 index 00000000000..c935182db4a --- /dev/null +++ b/core/util.runtime/src/org/jetbrains/kotlin/load/java/JavaNullabilityAnnotationsStatus.kt @@ -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) + } +} diff --git a/core/util.runtime/src/org/jetbrains/kotlin/load/java/Jsr305Settings.kt b/core/util.runtime/src/org/jetbrains/kotlin/load/java/Jsr305Settings.kt new file mode 100644 index 00000000000..d58c2006809 --- /dev/null +++ b/core/util.runtime/src/org/jetbrains/kotlin/load/java/Jsr305Settings.kt @@ -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 = 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() +} \ No newline at end of file