Use new default settings for java nullability annotations in JavaTypeEnhancementState and get rid of all hardcoded defaults
This commit is contained in:
+8
-9
@@ -27,8 +27,6 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.annotationClass
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.firstArgument
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||
import org.jetbrains.kotlin.storage.StorageManager
|
||||
import org.jetbrains.kotlin.utils.JavaTypeEnhancementState
|
||||
import org.jetbrains.kotlin.utils.ReportLevel
|
||||
|
||||
class AnnotationTypeQualifierResolver(storageManager: StorageManager, private val javaTypeEnhancementState: JavaTypeEnhancementState) {
|
||||
class TypeQualifierWithApplicability(
|
||||
@@ -68,7 +66,7 @@ class AnnotationTypeQualifierResolver(storageManager: StorageManager, private va
|
||||
}
|
||||
|
||||
fun resolveTypeQualifierAnnotation(annotationDescriptor: AnnotationDescriptor): AnnotationDescriptor? {
|
||||
if (javaTypeEnhancementState.disabledJsr305) {
|
||||
if (javaTypeEnhancementState.jsr305.isDisabled) {
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -92,15 +90,16 @@ class AnnotationTypeQualifierResolver(storageManager: StorageManager, private va
|
||||
}
|
||||
|
||||
private fun resolveDefaultAnnotationState(annotationDescriptor: AnnotationDescriptor): ReportLevel {
|
||||
if (annotationDescriptor.fqName in JSPECIFY_DEFAULT_ANNOTATIONS) {
|
||||
return javaTypeEnhancementState.jspecifyReportLevel
|
||||
val annotationFqname = annotationDescriptor.fqName
|
||||
if (annotationFqname != null && annotationFqname in JSPECIFY_DEFAULT_ANNOTATIONS) {
|
||||
return javaTypeEnhancementState.getReportLevelForAnnotation(annotationFqname)
|
||||
}
|
||||
|
||||
return resolveJsr305AnnotationState(annotationDescriptor)
|
||||
}
|
||||
|
||||
fun resolveTypeQualifierDefaultAnnotation(annotationDescriptor: AnnotationDescriptor): TypeQualifierWithApplicability? {
|
||||
if (javaTypeEnhancementState.disabledJsr305) {
|
||||
if (javaTypeEnhancementState.jsr305.isDisabled) {
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -138,11 +137,11 @@ class AnnotationTypeQualifierResolver(storageManager: StorageManager, private va
|
||||
|
||||
fun resolveJsr305AnnotationState(annotationDescriptor: AnnotationDescriptor): ReportLevel {
|
||||
resolveJsr305CustomState(annotationDescriptor)?.let { return it }
|
||||
return javaTypeEnhancementState.globalJsr305Level
|
||||
return javaTypeEnhancementState.jsr305.globalLevel
|
||||
}
|
||||
|
||||
fun resolveJsr305CustomState(annotationDescriptor: AnnotationDescriptor): ReportLevel? {
|
||||
javaTypeEnhancementState.userDefinedLevelForSpecificJsr305Annotation[annotationDescriptor.fqName?.asString()]?.let { return it }
|
||||
javaTypeEnhancementState.jsr305.userDefinedLevelForSpecificAnnotation[annotationDescriptor.fqName]?.let { return it }
|
||||
return annotationDescriptor.annotationClass?.migrationAnnotationStatus()
|
||||
}
|
||||
|
||||
@@ -150,7 +149,7 @@ class AnnotationTypeQualifierResolver(storageManager: StorageManager, private va
|
||||
val enumValue = annotations.findAnnotation(MIGRATION_ANNOTATION_FQNAME)?.firstArgument() as? EnumValue
|
||||
?: return null
|
||||
|
||||
javaTypeEnhancementState.migrationLevelForJsr305?.let { return it }
|
||||
javaTypeEnhancementState.jsr305.migrationLevel?.let { return it }
|
||||
|
||||
return when (enumValue.enumEntryName.asString()) {
|
||||
"STRICT" -> ReportLevel.STRICT
|
||||
|
||||
@@ -40,7 +40,6 @@ import org.jetbrains.kotlin.resolve.sam.SamConversionResolver
|
||||
import org.jetbrains.kotlin.serialization.deserialization.ErrorReporter
|
||||
import org.jetbrains.kotlin.storage.StorageManager
|
||||
import org.jetbrains.kotlin.types.checker.NewKotlinTypeChecker
|
||||
import org.jetbrains.kotlin.utils.JavaTypeEnhancementState
|
||||
|
||||
class JavaResolverComponents(
|
||||
val storageManager: StorageManager,
|
||||
|
||||
+22
-36
@@ -39,8 +39,6 @@ import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
|
||||
import org.jetbrains.kotlin.types.typeUtil.contains
|
||||
import org.jetbrains.kotlin.types.typeUtil.isTypeParameter
|
||||
import org.jetbrains.kotlin.utils.JavaTypeEnhancementState
|
||||
import org.jetbrains.kotlin.utils.ReportLevel
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
|
||||
|
||||
class SignatureEnhancement(
|
||||
@@ -89,10 +87,7 @@ class SignatureEnhancement(
|
||||
val isForWarningOnly = annotationDescriptor is LazyJavaAnnotationDescriptor
|
||||
&& (annotationDescriptor.isFreshlySupportedTypeUseAnnotation || typeParameterBounds)
|
||||
&& !areImprovementsEnabled
|
||||
|
||||
val migrationStatus = jspecifyMigrationStatus(annotationFqName)
|
||||
?: commonMigrationStatus(annotationFqName, annotationDescriptor, isForWarningOnly)
|
||||
?: return null
|
||||
val migrationStatus = commonMigrationStatus(annotationFqName, annotationDescriptor, isForWarningOnly) ?: return null
|
||||
|
||||
return if (!migrationStatus.isForWarningOnly
|
||||
&& annotationDescriptor is PossiblyExternalAnnotationDescriptor
|
||||
@@ -102,44 +97,35 @@ class SignatureEnhancement(
|
||||
} else migrationStatus
|
||||
}
|
||||
|
||||
private fun jspecifyMigrationStatus(
|
||||
annotationFqName: FqName
|
||||
): NullabilityQualifierWithMigrationStatus? {
|
||||
if (javaTypeEnhancementState.jspecifyReportLevel == ReportLevel.IGNORE) return null
|
||||
val isForWarningOnly = javaTypeEnhancementState.jspecifyReportLevel == ReportLevel.WARN
|
||||
return when (annotationFqName) {
|
||||
JSPECIFY_NULLABLE -> NullabilityQualifierWithMigrationStatus(NullabilityQualifier.NULLABLE, isForWarningOnly)
|
||||
JSPECIFY_NULLNESS_UNKNOWN ->
|
||||
NullabilityQualifierWithMigrationStatus(NullabilityQualifier.FORCE_FLEXIBILITY, isForWarningOnly)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
private fun getReportLevel(annotationFqName: FqName) = javaTypeEnhancementState.getReportLevelForAnnotation(annotationFqName)
|
||||
|
||||
private fun commonMigrationStatus(
|
||||
annotationFqName: FqName,
|
||||
annotationDescriptor: AnnotationDescriptor,
|
||||
isForWarningOnly: Boolean = false
|
||||
): NullabilityQualifierWithMigrationStatus? = when {
|
||||
annotationFqName in NULLABLE_ANNOTATIONS -> NullabilityQualifierWithMigrationStatus(NullabilityQualifier.NULLABLE, isForWarningOnly)
|
||||
annotationFqName in NOT_NULL_ANNOTATIONS -> NullabilityQualifierWithMigrationStatus(NullabilityQualifier.NOT_NULL, isForWarningOnly)
|
||||
annotationFqName == JAVAX_NONNULL_ANNOTATION -> annotationDescriptor.extractNullabilityTypeFromArgument(isForWarningOnly)
|
||||
): NullabilityQualifierWithMigrationStatus? {
|
||||
val reportLevel = getReportLevel(annotationFqName)
|
||||
|
||||
annotationFqName == COMPATQUAL_NULLABLE_ANNOTATION && javaTypeEnhancementState.enableCompatqualCheckerFrameworkAnnotations ->
|
||||
NullabilityQualifierWithMigrationStatus(NullabilityQualifier.NULLABLE, isForWarningOnly)
|
||||
if (reportLevel.isIgnore) return null
|
||||
|
||||
annotationFqName == COMPATQUAL_NONNULL_ANNOTATION && javaTypeEnhancementState.enableCompatqualCheckerFrameworkAnnotations ->
|
||||
NullabilityQualifierWithMigrationStatus(NullabilityQualifier.NOT_NULL, isForWarningOnly)
|
||||
val isForWarning = reportLevel.isWarning || isForWarningOnly
|
||||
|
||||
annotationFqName == ANDROIDX_RECENTLY_NON_NULL_ANNOTATION -> NullabilityQualifierWithMigrationStatus(
|
||||
NullabilityQualifier.NOT_NULL,
|
||||
isForWarningOnly = true
|
||||
)
|
||||
|
||||
annotationFqName == ANDROIDX_RECENTLY_NULLABLE_ANNOTATION -> NullabilityQualifierWithMigrationStatus(
|
||||
NullabilityQualifier.NULLABLE,
|
||||
isForWarningOnly = true
|
||||
)
|
||||
else -> null
|
||||
return when (annotationFqName) {
|
||||
in NULLABLE_ANNOTATIONS -> NullabilityQualifierWithMigrationStatus(NullabilityQualifier.NULLABLE, isForWarning)
|
||||
in NOT_NULL_ANNOTATIONS -> NullabilityQualifierWithMigrationStatus(NullabilityQualifier.NOT_NULL, isForWarning)
|
||||
JAVAX_NONNULL_ANNOTATION -> annotationDescriptor.extractNullabilityTypeFromArgument(isForWarning)
|
||||
COMPATQUAL_NULLABLE_ANNOTATION ->
|
||||
NullabilityQualifierWithMigrationStatus(NullabilityQualifier.NULLABLE, isForWarning)
|
||||
COMPATQUAL_NONNULL_ANNOTATION ->
|
||||
NullabilityQualifierWithMigrationStatus(NullabilityQualifier.NOT_NULL, isForWarning)
|
||||
ANDROIDX_RECENTLY_NON_NULL_ANNOTATION -> NullabilityQualifierWithMigrationStatus(
|
||||
NullabilityQualifier.NOT_NULL, isForWarning
|
||||
)
|
||||
ANDROIDX_RECENTLY_NULLABLE_ANNOTATION -> NullabilityQualifierWithMigrationStatus(
|
||||
NullabilityQualifier.NULLABLE, isForWarning
|
||||
)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
fun <D : CallableMemberDescriptor> enhanceSignatures(c: LazyJavaResolverContext, platformSignatures: Collection<D>): Collection<D> {
|
||||
|
||||
+8
-4
@@ -45,7 +45,7 @@ import org.jetbrains.kotlin.serialization.deserialization.DeserializationConfigu
|
||||
import org.jetbrains.kotlin.storage.LockBasedStorageManager
|
||||
import org.jetbrains.kotlin.storage.StorageManager
|
||||
import org.jetbrains.kotlin.types.checker.NewKotlinTypeChecker
|
||||
import org.jetbrains.kotlin.utils.JavaTypeEnhancementState
|
||||
import org.jetbrains.kotlin.load.java.JavaTypeEnhancementState
|
||||
|
||||
class RuntimeModuleData private constructor(
|
||||
val deserialization: DeserializationComponents,
|
||||
@@ -117,15 +117,19 @@ fun makeLazyJavaPackageFragmentFromClassLoaderProvider(
|
||||
singleModuleClassResolver: ModuleClassResolver,
|
||||
packagePartProvider: PackagePartProvider = PackagePartProvider.Empty
|
||||
): LazyJavaPackageFragmentProvider {
|
||||
val annotationTypeQualifierResolver = AnnotationTypeQualifierResolver(storageManager, JavaTypeEnhancementState.DISABLED_JSR_305)
|
||||
val javaTypeEnhancementState = JavaTypeEnhancementState.DISABLED_JSR_305
|
||||
val annotationTypeQualifierResolver = AnnotationTypeQualifierResolver(storageManager, JavaTypeEnhancementState.DEFAULT)
|
||||
val javaTypeEnhancementState = JavaTypeEnhancementState.DEFAULT
|
||||
val javaResolverComponents = JavaResolverComponents(
|
||||
storageManager, ReflectJavaClassFinder(classLoader), reflectKotlinClassFinder, deserializedDescriptorResolver,
|
||||
SignaturePropagator.DO_NOTHING, RuntimeErrorReporter, JavaResolverCache.EMPTY,
|
||||
JavaPropertyInitializerEvaluator.DoNothing, SamConversionResolverImpl(storageManager, emptyList()), RuntimeSourceElementFactory,
|
||||
singleModuleClassResolver, packagePartProvider, SupertypeLoopChecker.EMPTY, LookupTracker.DO_NOTHING, module,
|
||||
ReflectionTypes(module, notFoundClasses), annotationTypeQualifierResolver,
|
||||
SignatureEnhancement(annotationTypeQualifierResolver, JavaTypeEnhancementState.DISABLED_JSR_305, JavaTypeEnhancement(JavaResolverSettings.Default)),
|
||||
SignatureEnhancement(
|
||||
annotationTypeQualifierResolver,
|
||||
JavaTypeEnhancementState.DEFAULT,
|
||||
JavaTypeEnhancement(JavaResolverSettings.Default)
|
||||
),
|
||||
JavaClassesTracker.Default, JavaResolverSettings.Default, NewKotlinTypeChecker.Default, javaTypeEnhancementState,
|
||||
object : JavaModuleAnnotationsProvider {
|
||||
override fun getAnnotationsForModuleOwnerOfClass(classId: ClassId): List<JavaAnnotation>? = null
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.load.java
|
||||
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
|
||||
class JavaTypeEnhancementState(
|
||||
val jsr305: Jsr305Settings = Jsr305Settings.DEFAULT,
|
||||
val getReportLevelForAnnotation: (FqName) -> ReportLevel = ::getDefaultReportLevelForAnnotation
|
||||
) {
|
||||
val disabledDefaultAnnotations = jsr305.isDisabled || getReportLevelForAnnotation(JSPECIFY_ANNOTATIONS_PACKAGE) == ReportLevel.IGNORE
|
||||
|
||||
companion object {
|
||||
val DEFAULT = JavaTypeEnhancementState()
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.utils
|
||||
|
||||
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 == ReportLevel.WARN
|
||||
val isIgnore: Boolean get() = this == ReportLevel.IGNORE
|
||||
}
|
||||
|
||||
class JavaTypeEnhancementState(
|
||||
val globalJsr305Level: ReportLevel,
|
||||
val migrationLevelForJsr305: ReportLevel?,
|
||||
val userDefinedLevelForSpecificJsr305Annotation: Map<String, ReportLevel>,
|
||||
val enableCompatqualCheckerFrameworkAnnotations: Boolean = COMPATQUAL_CHECKER_FRAMEWORK_ANNOTATIONS_SUPPORT_DEFAULT_VALUE,
|
||||
val jspecifyReportLevel: ReportLevel = DEFAULT_REPORT_LEVEL_FOR_JSPECIFY,
|
||||
val nullabilityAnnotationsReportLevel: Map<String, ReportLevel>
|
||||
) {
|
||||
val description: Array<String> by lazy {
|
||||
val result = mutableListOf<String>()
|
||||
result.add(globalJsr305Level.description)
|
||||
|
||||
migrationLevelForJsr305?.let { result.add("under-migration:${it.description}") }
|
||||
|
||||
userDefinedLevelForSpecificJsr305Annotation.forEach {
|
||||
result.add("@${it.key}:${it.value.description}")
|
||||
}
|
||||
|
||||
result.toTypedArray()
|
||||
}
|
||||
|
||||
val disabledJsr305: Boolean =
|
||||
globalJsr305Level == ReportLevel.IGNORE &&
|
||||
migrationLevelForJsr305 == ReportLevel.IGNORE &&
|
||||
userDefinedLevelForSpecificJsr305Annotation.isEmpty()
|
||||
|
||||
val disabledDefaultAnnotations = disabledJsr305 || jspecifyReportLevel == ReportLevel.IGNORE
|
||||
|
||||
companion object {
|
||||
const val COMPATQUAL_CHECKER_FRAMEWORK_ANNOTATIONS_SUPPORT_DEFAULT_VALUE = true
|
||||
|
||||
@JvmField
|
||||
val DEFAULT_REPORT_LEVEL_FOR_JSPECIFY = ReportLevel.WARN
|
||||
|
||||
@JvmField
|
||||
val DEFAULT: JavaTypeEnhancementState = JavaTypeEnhancementState(
|
||||
ReportLevel.WARN,
|
||||
null,
|
||||
emptyMap(),
|
||||
nullabilityAnnotationsReportLevel = emptyMap()
|
||||
)
|
||||
|
||||
@JvmField
|
||||
val DISABLED_JSR_305: JavaTypeEnhancementState = JavaTypeEnhancementState(
|
||||
ReportLevel.IGNORE,
|
||||
ReportLevel.IGNORE,
|
||||
emptyMap(),
|
||||
nullabilityAnnotationsReportLevel = emptyMap()
|
||||
)
|
||||
|
||||
@JvmField
|
||||
val STRICT: JavaTypeEnhancementState = JavaTypeEnhancementState(
|
||||
ReportLevel.STRICT,
|
||||
ReportLevel.STRICT,
|
||||
emptyMap(),
|
||||
nullabilityAnnotationsReportLevel = emptyMap()
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user