Move java type enhancement stuff to :core:compiler.common.jvm

This commit is contained in:
Victor Petukhov
2021-07-09 10:50:06 +03:00
committed by teamcityserver
parent c112e768de
commit 1224d28deb
6 changed files with 1 additions and 3 deletions
@@ -0,0 +1,104 @@
/*
* 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.findValueForMostSpecificFqname
import org.jetbrains.kotlin.storage.LockBasedStorageManager
val JSPECIFY_ANNOTATIONS_PACKAGE = FqName("org.jspecify.nullness")
val CHECKER_FRAMEWORK_COMPATQUAL_ANNOTATIONS_PACKAGE = FqName("org.checkerframework.checker.nullness.compatqual")
val NULLABILITY_ANNOTATION_SETTINGS: NullabilityAnnotationStates<JavaNullabilityAnnotationsStatus> = NullabilityAnnotationStatesImpl(
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("androidx.annotation.RecentlyNullable") to JavaNullabilityAnnotationsStatus(
reportLevelBefore = ReportLevel.WARN,
sinceVersion = null
),
FqName("androidx.annotation.RecentlyNonNull") to JavaNullabilityAnnotationsStatus(
reportLevelBefore = ReportLevel.WARN,
sinceVersion = null
),
FqName("lombok") to JavaNullabilityAnnotationsStatus.DEFAULT,
JSPECIFY_ANNOTATIONS_PACKAGE to JavaNullabilityAnnotationsStatus(
reportLevelBefore = ReportLevel.WARN,
sinceVersion = KotlinVersion(1, 6),
reportLevelAfter = ReportLevel.STRICT
),
FqName("io.reactivex.rxjava3.annotations") to JavaNullabilityAnnotationsStatus(
reportLevelBefore = ReportLevel.WARN,
sinceVersion = KotlinVersion(1, 7),
reportLevelAfter = ReportLevel.STRICT
),
)
)
private val JSR_305_DEFAULT_SETTINGS = JavaNullabilityAnnotationsStatus(
reportLevelBefore = ReportLevel.WARN,
sinceVersion = null
)
fun getDefaultJsr305Settings(configuredKotlinVersion: KotlinVersion = KotlinVersion.CURRENT): Jsr305Settings {
val globalReportLevel =
if (JSR_305_DEFAULT_SETTINGS.sinceVersion != null && JSR_305_DEFAULT_SETTINGS.sinceVersion <= configuredKotlinVersion) {
JSR_305_DEFAULT_SETTINGS.reportLevelAfter
} else {
JSR_305_DEFAULT_SETTINGS.reportLevelBefore
}
val migrationLevel = getDefaultMigrationJsr305ReportLevelForGivenGlobal(globalReportLevel)
return Jsr305Settings(globalReportLevel, migrationLevel)
}
fun getDefaultMigrationJsr305ReportLevelForGivenGlobal(globalReportLevel: ReportLevel) =
if (globalReportLevel == ReportLevel.WARN) null else globalReportLevel
fun getDefaultReportLevelForAnnotation(annotationFqName: FqName) =
getReportLevelForAnnotation(annotationFqName, NullabilityAnnotationStates.EMPTY)
fun getReportLevelForAnnotation(
annotation: FqName,
configuredReportLevels: NullabilityAnnotationStates<ReportLevel>,
configuredKotlinVersion: KotlinVersion = KotlinVersion.CURRENT
): ReportLevel {
configuredReportLevels[annotation]?.let { return it }
val defaultStatus = NULLABILITY_ANNOTATION_SETTINGS[annotation] ?: return ReportLevel.IGNORE
return if (defaultStatus.sinceVersion != null && defaultStatus.sinceVersion <= configuredKotlinVersion) {
defaultStatus.reportLevelAfter
} else {
defaultStatus.reportLevelBefore
}
}
interface NullabilityAnnotationStates<out T : Any> {
operator fun get(fqName: FqName): T?
companion object {
val EMPTY: NullabilityAnnotationStates<Nothing> = NullabilityAnnotationStatesImpl(emptyMap())
}
}
class NullabilityAnnotationStatesImpl<T : Any>(val states: Map<FqName, T>) : NullabilityAnnotationStates<T> {
val storageManager = LockBasedStorageManager("Java nullability annotation states")
private val cache = storageManager.createMemoizedFunctionWithNullableValues<FqName, T> {
it.findValueForMostSpecificFqname(states)
}
override operator fun get(fqName: FqName) = cache(fqName)
}
@@ -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,19 @@
/*
* 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
class JavaTypeEnhancementState(
val jsr305: Jsr305Settings,
val getReportLevelForAnnotation: (FqName) -> ReportLevel
) {
val disabledDefaultAnnotations = jsr305.isDisabled || getReportLevelForAnnotation(JSPECIFY_ANNOTATIONS_PACKAGE) == ReportLevel.IGNORE
companion object {
val DEFAULT = JavaTypeEnhancementState(getDefaultJsr305Settings(), ::getDefaultReportLevelForAnnotation)
}
}
@@ -0,0 +1,27 @@
/*
* 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()
) {
@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()
}
@@ -0,0 +1,20 @@
/*
* 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
enum class ReportLevel(val description: String) {
IGNORE("ignore"),
WARN("warn"),
STRICT("strict"),
;
companion object {
fun findByDescription(description: String?): ReportLevel? = values().firstOrNull { it.description == description }
}
val isWarning: Boolean get() = this == WARN
val isIgnore: Boolean get() = this == IGNORE
}