diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/JavaTypeEnhancementStateParser.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/JavaTypeEnhancementStateParser.kt index 96756352dd0..59fd75bb57c 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/JavaTypeEnhancementStateParser.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/JavaTypeEnhancementStateParser.kt @@ -18,8 +18,9 @@ package org.jetbrains.kotlin.cli.common.arguments import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity import org.jetbrains.kotlin.cli.common.messages.MessageCollector -import org.jetbrains.kotlin.utils.JavaTypeEnhancementState -import org.jetbrains.kotlin.utils.ReportLevel +import org.jetbrains.kotlin.load.java.* +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.isSubpackageOf class JavaTypeEnhancementStateParser(private val collector: MessageCollector) { fun parse( @@ -30,37 +31,36 @@ class JavaTypeEnhancementStateParser(private val collector: MessageCollector) { ): JavaTypeEnhancementState { val jsr305State = parseJsr305State(jsr305Args) - val enableCompatqualCheckerFrameworkAnnotations = when (supportCompatqualCheckerFrameworkAnnotations) { - "enable" -> true - "disable" -> false - null -> null + val compatqualCheckerFrameworkAnnotationsReportLevel = when (supportCompatqualCheckerFrameworkAnnotations) { + "enable" -> ReportLevel.STRICT + "disable" -> ReportLevel.IGNORE + null -> getDefaultReportLevelForAnnotation(CHECKER_FRAMEWORK_COMPATQUAL_ANNOTATIONS_PACKAGE) else -> { collector.report( CompilerMessageSeverity.ERROR, "Unrecognized -Xsupport-compatqual-checker-framework-annotations option: $supportCompatqualCheckerFrameworkAnnotations. Possible values are 'enable'/'disable'" ) - null + getDefaultReportLevelForAnnotation(CHECKER_FRAMEWORK_COMPATQUAL_ANNOTATIONS_PACKAGE) } } val jspecifyReportLevel = parseJspecifyReportLevel(jspecifyState) val nullabilityAnnotationReportLevels = parseNullabilityAnnotationReportLevels(nullabilityAnnotations) + val jsr305Settings = Jsr305Settings(jsr305State.global ?: ReportLevel.WARN, jsr305State.migration, jsr305State.usedDefined) - val state = JavaTypeEnhancementState( - jsr305State.global ?: ReportLevel.WARN, jsr305State.migration, jsr305State.usedDefined, - enableCompatqualCheckerFrameworkAnnotations = - enableCompatqualCheckerFrameworkAnnotations - ?: JavaTypeEnhancementState.COMPATQUAL_CHECKER_FRAMEWORK_ANNOTATIONS_SUPPORT_DEFAULT_VALUE, - jspecifyReportLevel = jspecifyReportLevel, - nullabilityAnnotationsReportLevel = nullabilityAnnotationReportLevels - ) - return if (state == JavaTypeEnhancementState.DISABLED_JSR_305) JavaTypeEnhancementState.DISABLED_JSR_305 else state + return JavaTypeEnhancementState(jsr305Settings) { + when { + it.isSubpackageOf(JSPECIFY_ANNOTATIONS_PACKAGE) -> jspecifyReportLevel + it.isSubpackageOf(CHECKER_FRAMEWORK_COMPATQUAL_ANNOTATIONS_PACKAGE) -> compatqualCheckerFrameworkAnnotationsReportLevel + else -> getReportLevelForAnnotation(it, nullabilityAnnotationReportLevels) + } + } } - private fun parseNullabilityAnnotationReportLevels(nullabilityAnnotations: Array?): Map { + private fun parseNullabilityAnnotationReportLevels(nullabilityAnnotations: Array?): Map { if (nullabilityAnnotations.isNullOrEmpty()) return emptyMap() - val annotationsWithReportLevels = mutableMapOf() + val annotationsWithReportLevels = mutableMapOf() val compilerOption = "-Xnullability-annotations" for (item in nullabilityAnnotations) { @@ -83,7 +83,9 @@ class JavaTypeEnhancementStateParser(private val collector: MessageCollector) { } private fun parseJspecifyReportLevel(jspecifyState: String?): ReportLevel { - if (jspecifyState == null) return JavaTypeEnhancementState.DEFAULT_REPORT_LEVEL_FOR_JSPECIFY + if (jspecifyState == null) + return getDefaultReportLevelForAnnotation(JSPECIFY_ANNOTATIONS_PACKAGE) + val reportLevel = ReportLevel.findByDescription(jspecifyState) if (reportLevel == null) { @@ -91,7 +93,7 @@ class JavaTypeEnhancementStateParser(private val collector: MessageCollector) { CompilerMessageSeverity.ERROR, "Unrecognized -Xjspecify-annotations option: $jspecifyState. Possible values are 'disable'/'warn'/'strict'" ) - return JavaTypeEnhancementState.DEFAULT_REPORT_LEVEL_FOR_JSPECIFY + return getDefaultReportLevelForAnnotation(JSPECIFY_ANNOTATIONS_PACKAGE) } return reportLevel @@ -100,13 +102,13 @@ class JavaTypeEnhancementStateParser(private val collector: MessageCollector) { private data class Jsr305State( val global: ReportLevel?, val migration: ReportLevel?, - val usedDefined: Map + val usedDefined: Map ) private fun parseJsr305State(args: Array?): Jsr305State { var global: ReportLevel? = null var migration: ReportLevel? = null - val userDefined = mutableMapOf() + val userDefined = mutableMapOf() val compilerOption = "-Xjsr305" fun parseJsr305UnderMigration(item: String): ReportLevel? { @@ -165,7 +167,7 @@ class JavaTypeEnhancementStateParser(private val collector: MessageCollector) { collector.report(CompilerMessageSeverity.ERROR, "Conflict duplicating $sourceCompilerOption value: $first, $second") } - private fun parseAnnotationWithReportLevel(item: String, sourceCompilerOption: String): Pair? { + private fun parseAnnotationWithReportLevel(item: String, sourceCompilerOption: String): Pair? { val (name, rawState) = item.substring(1).split(":").takeIf { it.size == 2 } ?: run { reportUnrecognizedReportLevel(item, sourceCompilerOption) return null @@ -176,6 +178,6 @@ class JavaTypeEnhancementStateParser(private val collector: MessageCollector) { return null } - return name to state + return FqName(name) to state } } diff --git a/compiler/config.jvm/src/org/jetbrains/kotlin/config/JvmAnalysisFlags.kt b/compiler/config.jvm/src/org/jetbrains/kotlin/config/JvmAnalysisFlags.kt index c984dde8662..e4278e6d7ce 100644 --- a/compiler/config.jvm/src/org/jetbrains/kotlin/config/JvmAnalysisFlags.kt +++ b/compiler/config.jvm/src/org/jetbrains/kotlin/config/JvmAnalysisFlags.kt @@ -5,7 +5,7 @@ package org.jetbrains.kotlin.config -import org.jetbrains.kotlin.utils.JavaTypeEnhancementState +import org.jetbrains.kotlin.load.java.JavaTypeEnhancementState import kotlin.reflect.KProperty object JvmAnalysisFlags { diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/EnhancementSignatureParts.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/EnhancementSignatureParts.kt index 099fba4f5c6..b2ed4dee93c 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/EnhancementSignatureParts.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/EnhancementSignatureParts.kt @@ -27,7 +27,7 @@ import org.jetbrains.kotlin.load.java.structure.JavaWildcardType import org.jetbrains.kotlin.load.java.typeEnhancement.* import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.FqNameUnsafe -import org.jetbrains.kotlin.utils.JavaTypeEnhancementState +import org.jetbrains.kotlin.load.java.JavaTypeEnhancementState import org.jetbrains.kotlin.utils.addToStdlib.safeAs internal class EnhancementSignatureParts( diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/FirAnnotationTypeQualifierResolver.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/FirAnnotationTypeQualifierResolver.kt index c0bebef3aa6..6058fffdeeb 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/FirAnnotationTypeQualifierResolver.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/FirAnnotationTypeQualifierResolver.kt @@ -14,8 +14,9 @@ import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol import org.jetbrains.kotlin.load.java.* import org.jetbrains.kotlin.load.java.JvmAnnotationNames.DEFAULT_ANNOTATION_MEMBER_NAME import org.jetbrains.kotlin.name.ClassId -import org.jetbrains.kotlin.utils.JavaTypeEnhancementState -import org.jetbrains.kotlin.utils.ReportLevel +import org.jetbrains.kotlin.load.java.JavaTypeEnhancementState +import org.jetbrains.kotlin.load.java.ReportLevel +import org.jetbrains.kotlin.name.FqName class FirAnnotationTypeQualifierResolver(private val session: FirSession, private val javaTypeEnhancementState: JavaTypeEnhancementState) { @@ -52,7 +53,7 @@ class FirAnnotationTypeQualifierResolver(private val session: FirSession, privat get() = (coneClassLikeType?.lookupTag?.toSymbol(this@FirAnnotationTypeQualifierResolver.session) as? FirRegularClassSymbol)?.fir fun resolveTypeQualifierAnnotation(annotationCall: FirAnnotationCall): FirAnnotationCall? { - if (javaTypeEnhancementState.disabledJsr305) { + if (javaTypeEnhancementState.jsr305.isDisabled) { return null } @@ -63,7 +64,7 @@ class FirAnnotationTypeQualifierResolver(private val session: FirSession, privat } fun resolveQualifierBuiltInDefaultAnnotation(annotationCall: FirAnnotationCall): JavaDefaultQualifiers? { - if (javaTypeEnhancementState.disabledJsr305) { + if (javaTypeEnhancementState.jsr305.isDisabled) { return null } @@ -77,7 +78,7 @@ class FirAnnotationTypeQualifierResolver(private val session: FirSession, privat } fun resolveTypeQualifierDefaultAnnotation(annotationCall: FirAnnotationCall): TypeQualifierWithApplicability? { - if (javaTypeEnhancementState.disabledJsr305) { + if (javaTypeEnhancementState.jsr305.isDisabled) { return null } @@ -109,11 +110,11 @@ class FirAnnotationTypeQualifierResolver(private val session: FirSession, privat fun resolveJsr305ReportLevel(annotationCall: FirAnnotationCall): ReportLevel { resolveJsr305CustomLevel(annotationCall)?.let { return it } - return javaTypeEnhancementState.globalJsr305Level + return javaTypeEnhancementState.jsr305.globalLevel } fun resolveJsr305CustomLevel(annotationCall: FirAnnotationCall): ReportLevel? { - javaTypeEnhancementState.userDefinedLevelForSpecificJsr305Annotation[annotationCall.classId?.run { packageFqName.asString() + "." + relativeClassName.asString() }]?.let { return it } + javaTypeEnhancementState.jsr305.userDefinedLevelForSpecificAnnotation[annotationCall.classId?.run { FqName(packageFqName.asString() + "." + relativeClassName.asString()) }]?.let { return it } return annotationCall.resolvedClass?.migrationAnnotationStatus() } @@ -122,7 +123,7 @@ class FirAnnotationTypeQualifierResolver(private val session: FirSession, privat it.classId == MIGRATION_ANNOTATION_ID }?.arguments?.firstOrNull()?.toResolvedCallableSymbol()?.callableId?.callableName ?: return null - javaTypeEnhancementState.migrationLevelForJsr305?.let { return it } + javaTypeEnhancementState.jsr305.migrationLevel?.let { return it } return when (enumEntryName.asString()) { "STRICT" -> ReportLevel.STRICT @@ -146,7 +147,7 @@ class FirAnnotationTypeQualifierResolver(private val session: FirSession, privat ) } - val disabled: Boolean = javaTypeEnhancementState.disabledJsr305 + val isDisabled: Boolean = javaTypeEnhancementState.jsr305.isDisabled } diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/FirJavaEnhancementContext.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/FirJavaEnhancementContext.kt index 2fffb1833b9..2b0157237cd 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/FirJavaEnhancementContext.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/FirJavaEnhancementContext.kt @@ -8,7 +8,6 @@ package org.jetbrains.kotlin.fir.java.enhancement import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall import org.jetbrains.kotlin.load.java.* -import org.jetbrains.kotlin.utils.JavaTypeEnhancementState class FirJavaEnhancementContext private constructor( val session: FirSession, @@ -51,7 +50,7 @@ fun FirJavaEnhancementContext.computeNewDefaultTypeQualifiers( javaTypeEnhancementState: JavaTypeEnhancementState, additionalAnnotations: List ): JavaTypeQualifiersByElementType? { - if (typeQualifierResolver.disabled) return defaultTypeQualifiers + if (typeQualifierResolver.isDisabled) return defaultTypeQualifiers val defaultQualifiers = additionalAnnotations.mapNotNull { annotationCall -> diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/FirJsr305StateContainer.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/FirJsr305StateContainer.kt index da176be43bd..0b35dfc367b 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/FirJsr305StateContainer.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/FirJsr305StateContainer.kt @@ -7,7 +7,7 @@ package org.jetbrains.kotlin.fir.java.enhancement import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.FirSessionComponent -import org.jetbrains.kotlin.utils.JavaTypeEnhancementState +import org.jetbrains.kotlin.load.java.JavaTypeEnhancementState class FirJsr305StateContainer(val javaTypeEnhancementState: JavaTypeEnhancementState) : FirSessionComponent { companion object { diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/SignatureEnhancement.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/SignatureEnhancement.kt index d7e73562b61..6fb4482e8eb 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/SignatureEnhancement.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/SignatureEnhancement.kt @@ -34,7 +34,7 @@ import org.jetbrains.kotlin.load.java.typeEnhancement.* import org.jetbrains.kotlin.load.kotlin.SignatureBuildingComponents import org.jetbrains.kotlin.name.CallableId import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.utils.JavaTypeEnhancementState +import org.jetbrains.kotlin.load.java.JavaTypeEnhancementState import org.jetbrains.kotlin.utils.addToStdlib.safeAs class FirSignatureEnhancement( diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/nullabilityUtils.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/nullabilityUtils.kt index 8371ea4604e..2d050d1ad5d 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/nullabilityUtils.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/nullabilityUtils.kt @@ -13,7 +13,6 @@ import org.jetbrains.kotlin.load.java.* import org.jetbrains.kotlin.load.java.typeEnhancement.NullabilityQualifier import org.jetbrains.kotlin.load.java.typeEnhancement.NullabilityQualifierWithMigrationStatus import org.jetbrains.kotlin.name.ClassId -import org.jetbrains.kotlin.utils.JavaTypeEnhancementState fun List.extractNullability( annotationTypeQualifierResolver: FirAnnotationTypeQualifierResolver, @@ -42,24 +41,21 @@ fun FirAnnotationCall.extractNullability( private fun FirAnnotationCall.extractNullabilityFromKnownAnnotations(javaTypeEnhancementState: JavaTypeEnhancementState): NullabilityQualifierWithMigrationStatus? { val annotationClassId = classId ?: return null + val reportLevel = javaTypeEnhancementState.getReportLevelForAnnotation(annotationClassId.asSingleFqName()) - return when { - annotationClassId in NULLABLE_ANNOTATION_IDS -> NullabilityQualifierWithMigrationStatus(NullabilityQualifier.NULLABLE) - annotationClassId in NOT_NULL_ANNOTATION_IDS -> NullabilityQualifierWithMigrationStatus(NullabilityQualifier.NOT_NULL) - annotationClassId == JAVAX_NONNULL_ANNOTATION_ID -> extractNullabilityTypeFromArgument() + if (reportLevel == ReportLevel.IGNORE) return null - annotationClassId == COMPATQUAL_NULLABLE_ANNOTATION_ID && javaTypeEnhancementState.enableCompatqualCheckerFrameworkAnnotations -> - NullabilityQualifierWithMigrationStatus(NullabilityQualifier.NULLABLE) - - annotationClassId == COMPATQUAL_NONNULL_ANNOTATION_ID && javaTypeEnhancementState.enableCompatqualCheckerFrameworkAnnotations -> - NullabilityQualifierWithMigrationStatus(NullabilityQualifier.NOT_NULL) - - annotationClassId == ANDROIDX_RECENTLY_NON_NULL_ANNOTATION_ID -> NullabilityQualifierWithMigrationStatus( + return when (annotationClassId) { + in NULLABLE_ANNOTATION_IDS -> NullabilityQualifierWithMigrationStatus(NullabilityQualifier.NULLABLE) + in NOT_NULL_ANNOTATION_IDS -> NullabilityQualifierWithMigrationStatus(NullabilityQualifier.NOT_NULL) + JAVAX_NONNULL_ANNOTATION_ID -> extractNullabilityTypeFromArgument() + COMPATQUAL_NULLABLE_ANNOTATION_ID -> NullabilityQualifierWithMigrationStatus(NullabilityQualifier.NULLABLE) + COMPATQUAL_NONNULL_ANNOTATION_ID -> NullabilityQualifierWithMigrationStatus(NullabilityQualifier.NOT_NULL) + ANDROIDX_RECENTLY_NON_NULL_ANNOTATION_ID -> NullabilityQualifierWithMigrationStatus( NullabilityQualifier.NOT_NULL, isForWarningOnly = true ) - - annotationClassId == ANDROIDX_RECENTLY_NULLABLE_ANNOTATION_ID -> NullabilityQualifierWithMigrationStatus( + ANDROIDX_RECENTLY_NULLABLE_ANNOTATION_ID -> NullabilityQualifierWithMigrationStatus( NullabilityQualifier.NULLABLE, isForWarningOnly = true ) diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/FirSession.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/FirSession.kt index 35cbc2ae44c..af9cb4387bc 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/FirSession.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/FirSession.kt @@ -10,7 +10,7 @@ import org.jetbrains.kotlin.fir.utils.ArrayMapAccessor import org.jetbrains.kotlin.fir.utils.ComponentArrayOwner import org.jetbrains.kotlin.fir.utils.NullableArrayMapAccessor import org.jetbrains.kotlin.fir.utils.TypeRegistry -import org.jetbrains.kotlin.utils.JavaTypeEnhancementState +import org.jetbrains.kotlin.load.java.JavaTypeEnhancementState import kotlin.reflect.KClass interface FirSessionComponent diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/annotations/jvmAnnotationUtil.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/annotations/jvmAnnotationUtil.kt index b1259fc9bc4..c0b9bdc1a7a 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/annotations/jvmAnnotationUtil.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/annotations/jvmAnnotationUtil.kt @@ -6,7 +6,6 @@ package org.jetbrains.kotlin.resolve.jvm.annotations import org.jetbrains.kotlin.config.JvmDefaultMode -import org.jetbrains.kotlin.config.LanguageVersion import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.annotations.Annotated import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor @@ -17,7 +16,6 @@ import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedClassDescriptor import org.jetbrains.kotlin.util.findImplementationFromInterface -import org.jetbrains.kotlin.utils.ReportLevel val JVM_DEFAULT_FQ_NAME = FqName("kotlin.jvm.JvmDefault") val JVM_DEFAULT_NO_COMPATIBILITY_FQ_NAME = FqName("kotlin.jvm.JvmDefaultWithoutCompatibility") @@ -97,34 +95,3 @@ fun DeclarationDescriptor.findSynchronizedAnnotation(): AnnotationDescriptor? = annotations.findAnnotation(SYNCHRONIZED_ANNOTATION_FQ_NAME) fun ClassDescriptor.isJvmRecord(): Boolean = annotations.hasAnnotation(JVM_RECORD_ANNOTATION_FQ_NAME) - -data class NullabilityAnnotationsStatus( - val reportLevelBefore: ReportLevel, - val sinceVersion: LanguageVersion = LanguageVersion.KOTLIN_1_0, - val reportLevelAfter: ReportLevel = reportLevelBefore, -) { - companion object { - val DEFAULT = NullabilityAnnotationsStatus(ReportLevel.STRICT) - } -} - -val nullabilityAnnotationSettings = mapOf( - FqName("org.jetbrains.annotations") to NullabilityAnnotationsStatus.DEFAULT, - FqName("androidx.annotation") to NullabilityAnnotationsStatus.DEFAULT, - FqName("android.support.annotation") to NullabilityAnnotationsStatus.DEFAULT, - FqName("android.annotation") to NullabilityAnnotationsStatus.DEFAULT, - FqName("com.android.annotations") to NullabilityAnnotationsStatus.DEFAULT, - FqName("org.eclipse.jdt.annotation") to NullabilityAnnotationsStatus.DEFAULT, - FqName("org.checkerframework.checker.nullness.qual") to NullabilityAnnotationsStatus.DEFAULT, - FqName("org.checkerframework.checker.nullness.compatqual") to NullabilityAnnotationsStatus.DEFAULT, - FqName("javax.annotation") to NullabilityAnnotationsStatus.DEFAULT, - FqName("javax.annotation") to NullabilityAnnotationsStatus.DEFAULT, - FqName("edu.umd.cs.findbugs.annotations") to NullabilityAnnotationsStatus.DEFAULT, - FqName("io.reactivex.annotations") to NullabilityAnnotationsStatus.DEFAULT, - FqName("lombok") to NullabilityAnnotationsStatus.DEFAULT, - FqName("org.jspecify.nullness") to NullabilityAnnotationsStatus( - reportLevelBefore = ReportLevel.WARN, - sinceVersion = LanguageVersion.KOTLIN_1_6, - reportLevelAfter = ReportLevel.STRICT - ), -) \ No newline at end of file diff --git a/compiler/test-infrastructure-utils/tests/org/jetbrains/kotlin/test/util/KtTestUtil.java b/compiler/test-infrastructure-utils/tests/org/jetbrains/kotlin/test/util/KtTestUtil.java index c257e37521c..7e4380b4c45 100644 --- a/compiler/test-infrastructure-utils/tests/org/jetbrains/kotlin/test/util/KtTestUtil.java +++ b/compiler/test-infrastructure-utils/tests/org/jetbrains/kotlin/test/util/KtTestUtil.java @@ -121,7 +121,7 @@ public class KtTestUtil { if (otherProp != null) { return getJdkHome(otherProp, null, prop); } else { - return new File("/Library/Java/JavaVirtualMachines/jdk-15.jdk/Contents/Home"); + throw new AssertionError("Environment variable " + propToReport + " is not set!"); } } return new File(jdk); diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/directives/ForeignAnnotationsDirectives.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/directives/ForeignAnnotationsDirectives.kt index 40551afe172..0b71adf5962 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/directives/ForeignAnnotationsDirectives.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/directives/ForeignAnnotationsDirectives.kt @@ -7,7 +7,7 @@ package org.jetbrains.kotlin.test.directives import org.jetbrains.kotlin.test.directives.model.SimpleDirectivesContainer import org.jetbrains.kotlin.test.services.configuration.JavaForeignAnnotationType -import org.jetbrains.kotlin.utils.ReportLevel +import org.jetbrains.kotlin.load.java.ReportLevel @Suppress("RemoveExplicitTypeArguments") object ForeignAnnotationsDirectives : SimpleDirectivesContainer() { diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/handlers/JspecifyDiagnosticComplianceHandler.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/handlers/JspecifyDiagnosticComplianceHandler.kt index e10fc01278d..746bef87819 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/handlers/JspecifyDiagnosticComplianceHandler.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/handlers/JspecifyDiagnosticComplianceHandler.kt @@ -8,20 +8,22 @@ package org.jetbrains.kotlin.test.frontend.classic.handlers import org.jetbrains.kotlin.codeMetaInfo.model.DiagnosticCodeMetaInfo import org.jetbrains.kotlin.codeMetaInfo.model.JspecifyMarkerCodeMetaInfo import org.jetbrains.kotlin.diagnostics.Errors +import org.jetbrains.kotlin.load.java.JSPECIFY_ANNOTATIONS_PACKAGE +import org.jetbrains.kotlin.load.java.ReportLevel +import org.jetbrains.kotlin.load.java.getDefaultReportLevelForAnnotation import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm import org.jetbrains.kotlin.test.directives.ForeignAnnotationsDirectives import org.jetbrains.kotlin.test.frontend.classic.ClassicFrontendOutputArtifact import org.jetbrains.kotlin.test.model.TestModule import org.jetbrains.kotlin.test.services.TestServices import org.jetbrains.kotlin.test.services.globalMetadataInfoHandler -import org.jetbrains.kotlin.utils.JavaTypeEnhancementState -import org.jetbrains.kotlin.utils.ReportLevel +import org.jetbrains.kotlin.load.java.getReportLevelForAnnotation // Not that this diagnostic handler should be included only with `ClassicDiagnosticsHandler` and go after it class JspecifyDiagnosticComplianceHandler(testServices: TestServices) : ClassicFrontendAnalysisHandler(testServices) { override fun processModule(module: TestModule, info: ClassicFrontendOutputArtifact) { val jspecifyMode = module.directives[ForeignAnnotationsDirectives.JSPECIFY_STATE].singleOrNull() - ?: JavaTypeEnhancementState.DEFAULT_REPORT_LEVEL_FOR_JSPECIFY + ?: getDefaultReportLevelForAnnotation(JSPECIFY_ANNOTATIONS_PACKAGE) for ((testFile, ktFile) in info.allKtFiles) { val reportedDiagnostics = diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/configuration/JvmForeignAnnotationsConfigurator.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/configuration/JvmForeignAnnotationsConfigurator.kt index 08011f471fb..6f331394842 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/configuration/JvmForeignAnnotationsConfigurator.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/configuration/JvmForeignAnnotationsConfigurator.kt @@ -12,6 +12,7 @@ import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime import org.jetbrains.kotlin.config.AnalysisFlag import org.jetbrains.kotlin.config.CompilerConfiguration import org.jetbrains.kotlin.config.JvmAnalysisFlags +import org.jetbrains.kotlin.load.java.* import org.jetbrains.kotlin.test.MockLibraryUtil import org.jetbrains.kotlin.test.TestJavacVersion import org.jetbrains.kotlin.test.directives.ForeignAnnotationsDirectives @@ -26,8 +27,7 @@ import org.jetbrains.kotlin.test.directives.model.singleOrZeroValue import org.jetbrains.kotlin.test.model.TestModule import org.jetbrains.kotlin.test.services.* import org.jetbrains.kotlin.test.util.KtTestUtil -import org.jetbrains.kotlin.utils.JavaTypeEnhancementState -import org.jetbrains.kotlin.utils.ReportLevel +import org.jetbrains.kotlin.name.FqName import java.io.File import kotlin.io.path.createTempDirectory @@ -51,16 +51,14 @@ open class JvmForeignAnnotationsConfigurator(testServices: TestServices) : Envir val userAnnotationsState = directives[JSR305_SPECIAL_REPORT].mapNotNull { val (name, stateDescription) = it.split(":").takeIf { it.size == 2 } ?: return@mapNotNull null val state = ReportLevel.findByDescription(stateDescription) ?: return@mapNotNull null - name to state + FqName(name) to state }.toMap() - val jSpecifyReportLevel = directives.singleOrZeroValue(JSPECIFY_STATE) ?: ReportLevel.WARN + val configuredReportLevels = + directives.singleOrZeroValue(JSPECIFY_STATE)?.let { mapOf(JSPECIFY_ANNOTATIONS_PACKAGE to it) } ?: emptyMap() return mapOf( JvmAnalysisFlags.javaTypeEnhancementState to JavaTypeEnhancementState( - globalState, - migrationState, - userAnnotationsState, - jspecifyReportLevel = jSpecifyReportLevel, - nullabilityAnnotationsReportLevel = emptyMap() + Jsr305Settings(globalState, migrationState, userAnnotationsState), + getReportLevelForAnnotation = { getReportLevelForAnnotation(it, configuredReportLevels) } ) ) } diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AllNullabilityAnnotationsAreSetUp.kt b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AllNullabilityAnnotationsAreSetUp.kt index 827ed98e65e..492da1d34dc 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AllNullabilityAnnotationsAreSetUp.kt +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AllNullabilityAnnotationsAreSetUp.kt @@ -6,8 +6,8 @@ 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.name.isChildOf -import org.jetbrains.kotlin.resolve.jvm.annotations.nullabilityAnnotationSettings import org.jetbrains.kotlin.test.testFramework.KtUsefulTestCase class AllNullabilityAnnotationsAreSetUpTest : KtUsefulTestCase() { diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/LoadJavaPackageAnnotationsTest.kt b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/LoadJavaPackageAnnotationsTest.kt index caa6d004817..ca4b4f961e5 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/LoadJavaPackageAnnotationsTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/LoadJavaPackageAnnotationsTest.kt @@ -29,12 +29,12 @@ import org.jetbrains.kotlin.resolve.lazy.JvmResolveUtil import org.jetbrains.kotlin.test.* import org.jetbrains.kotlin.test.testFramework.KtUsefulTestCase import org.jetbrains.kotlin.test.util.KtTestUtil -import org.jetbrains.kotlin.utils.JavaTypeEnhancementState +import org.jetbrains.kotlin.load.java.JavaTypeEnhancementState import java.io.File class LoadJavaPackageAnnotationsTest : KtUsefulTestCase() { companion object { - private val TEST_DATA_PATH = "compiler/testData/loadJavaPackageAnnotations/" + private const val TEST_DATA_PATH = "compiler/testData/loadJavaPackageAnnotations/" } private fun doTest(useJavac: Boolean, configurator: (CompilerConfiguration) -> Unit) { @@ -45,27 +45,26 @@ class LoadJavaPackageAnnotationsTest : KtUsefulTestCase() { put(JVMConfigurationKeys.USE_JAVAC, true) } languageVersionSettings = LanguageVersionSettingsImpl( - LanguageVersion.LATEST_STABLE, ApiVersion.LATEST_STABLE, mapOf(JvmAnalysisFlags.javaTypeEnhancementState to JavaTypeEnhancementState.STRICT) + LanguageVersion.LATEST_STABLE, + ApiVersion.LATEST_STABLE, + mapOf(JvmAnalysisFlags.javaTypeEnhancementState to JavaTypeEnhancementState.DEFAULT) ) configurator(this) } - val environment = - KotlinCoreEnvironment.createForTests( - testRootDisposable, - configuration, - EnvironmentConfigFiles.JVM_CONFIG_FILES - ).apply { - if (useJavac) { - registerJavac() - } - } + val environment = KotlinCoreEnvironment.createForTests( + testRootDisposable, + configuration, + EnvironmentConfigFiles.JVM_CONFIG_FILES + ).apply { + if (useJavac) { + registerJavac() + } + } val moduleDescriptor = JvmResolveUtil.analyze(environment).moduleDescriptor - val packageFragmentDescriptor = - moduleDescriptor.getPackage(FqName("test")).fragments - .singleOrNull { - it.getMemberScope().getContributedClassifier(Name.identifier("A"), NoLookupLocation.FROM_TEST) != null - }.let { assertInstanceOf(it, LazyJavaPackageFragment::class.java) } + val packageFragmentDescriptor = moduleDescriptor.getPackage(FqName("test")).fragments + .singleOrNull { it.getMemberScope().getContributedClassifier(Name.identifier("A"), NoLookupLocation.FROM_TEST) != null } + .let { assertInstanceOf(it, LazyJavaPackageFragment::class.java) } val annotation = packageFragmentDescriptor.annotations.findAnnotation(FqName("test.Ann")) assertNotNull(annotation) diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/TypeQualifierAnnotationResolverTest.kt b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/TypeQualifierAnnotationResolverTest.kt index 2e1afc1d3a0..fb35a59fbfd 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/TypeQualifierAnnotationResolverTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/TypeQualifierAnnotationResolverTest.kt @@ -36,39 +36,39 @@ import org.jetbrains.kotlin.resolve.lazy.JvmResolveUtil import org.jetbrains.kotlin.test.* import org.jetbrains.kotlin.test.testFramework.KtUsefulTestCase import org.jetbrains.kotlin.test.util.KtTestUtil -import org.jetbrains.kotlin.utils.JavaTypeEnhancementState +import org.jetbrains.kotlin.load.java.JavaTypeEnhancementState import java.io.File class TypeQualifierAnnotationResolverTest : KtUsefulTestCase() { companion object { - private val TEST_DATA_PATH = "compiler/testData/typeQualifierNickname/" + private const val TEST_DATA_PATH = "compiler/testData/typeQualifierNickname/" } fun testBasicJSRNullabilityAnnotations() { val (typeQualifierResolver, aClass) = buildTypeQualifierResolverAndFindClass("A") assertMethodHasUnwrappedAnnotation( - aClass, typeQualifierResolver, - "nullable", - "@javax.annotation.Nonnull(when = When.UNKNOWN)" + aClass, typeQualifierResolver, + "nullable", + "@javax.annotation.Nonnull(when = When.UNKNOWN)" ) assertMethodHasUnwrappedAnnotation( - aClass, typeQualifierResolver, - "checkForNull", - "@javax.annotation.CheckForNull()" + aClass, typeQualifierResolver, + "checkForNull", + "@javax.annotation.CheckForNull()" ) assertMethodHasUnwrappedAnnotation( - aClass, typeQualifierResolver, - "nonNull", - "@javax.annotation.Nonnull()" + aClass, typeQualifierResolver, + "nonNull", + "@javax.annotation.Nonnull()" ) assertMethodHasUnwrappedAnnotation( - aClass, typeQualifierResolver, - "nonNullExplicitArgument", - "@javax.annotation.Nonnull(when = When.ALWAYS)" + aClass, typeQualifierResolver, + "nonNullExplicitArgument", + "@javax.annotation.Nonnull(when = When.ALWAYS)" ) } @@ -76,9 +76,9 @@ class TypeQualifierAnnotationResolverTest : KtUsefulTestCase() { val (typeQualifierResolver, aClass) = buildTypeQualifierResolverAndFindClass("B") assertMethodHasUnwrappedAnnotation( - aClass, typeQualifierResolver, - "myNullable", - "@javax.annotation.CheckForNull()" + aClass, typeQualifierResolver, + "myNullable", + "@javax.annotation.CheckForNull()" ) } @@ -86,16 +86,18 @@ class TypeQualifierAnnotationResolverTest : KtUsefulTestCase() { val configuration = KotlinTestUtils.newConfiguration( ConfigurationKind.ALL, TestJdkKind.FULL_JDK, listOf( - KtTestUtil.getAnnotationsJar(), - MockLibraryUtilExt.compileJavaFilesLibraryToJar( - FOREIGN_ANNOTATIONS_SOURCES_PATH, - "foreign-annotations" - ) - ), + KtTestUtil.getAnnotationsJar(), + MockLibraryUtilExt.compileJavaFilesLibraryToJar( + FOREIGN_ANNOTATIONS_SOURCES_PATH, + "foreign-annotations" + ) + ), listOf(File(TEST_DATA_PATH)) ).apply { languageVersionSettings = LanguageVersionSettingsImpl( - LanguageVersion.LATEST_STABLE, ApiVersion.LATEST_STABLE, mapOf(JvmAnalysisFlags.javaTypeEnhancementState to JavaTypeEnhancementState.STRICT) + LanguageVersion.LATEST_STABLE, + ApiVersion.LATEST_STABLE, + mapOf(JvmAnalysisFlags.javaTypeEnhancementState to JavaTypeEnhancementState.DEFAULT) ) } @@ -109,28 +111,28 @@ class TypeQualifierAnnotationResolverTest : KtUsefulTestCase() { } private fun assertMethodHasUnwrappedAnnotation( - aClass: ClassDescriptor, - typeQualifierResolver: AnnotationTypeQualifierResolver, - methodName: String, - annotationText: String + aClass: ClassDescriptor, + typeQualifierResolver: AnnotationTypeQualifierResolver, + methodName: String, + annotationText: String ) { assertEquals( - annotationText, - DescriptorRenderer.withOptions { - annotationArgumentsRenderingPolicy = AnnotationArgumentsRenderingPolicy.ALWAYS_PARENTHESIZED - }.renderAnnotation( - aClass.findSingleTypeQualifierAnnotationOnMethod(methodName, typeQualifierResolver) - ) + annotationText, + DescriptorRenderer.withOptions { + annotationArgumentsRenderingPolicy = AnnotationArgumentsRenderingPolicy.ALWAYS_PARENTHESIZED + }.renderAnnotation( + aClass.findSingleTypeQualifierAnnotationOnMethod(methodName, typeQualifierResolver) + ) ) } private fun ClassDescriptor.findSingleTypeQualifierAnnotationOnMethod( - name: String, - typeQualifierResolver: AnnotationTypeQualifierResolver + name: String, + typeQualifierResolver: AnnotationTypeQualifierResolver ): AnnotationDescriptor = unsubstitutedMemberScope - .getContributedFunctions(Name.identifier(name), NoLookupLocation.FROM_TEST) - .single() - .annotations.single() - .let(typeQualifierResolver::resolveTypeQualifierAnnotation) - .also(::assertNotNull)!! + .getContributedFunctions(Name.identifier(name), NoLookupLocation.FROM_TEST) + .single() + .annotations.single() + .let(typeQualifierResolver::resolveTypeQualifierAnnotation) + .also(::assertNotNull)!! } diff --git a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/AnnotationTypeQualifierResolver.kt b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/AnnotationTypeQualifierResolver.kt index 4177e5fe75e..0824fe03eb8 100644 --- a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/AnnotationTypeQualifierResolver.kt +++ b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/AnnotationTypeQualifierResolver.kt @@ -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 diff --git a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/lazy/context.kt b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/lazy/context.kt index 25a7213d49b..8477fa2ca2f 100644 --- a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/lazy/context.kt +++ b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/lazy/context.kt @@ -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, diff --git a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/signatureEnhancement.kt b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/signatureEnhancement.kt index 2d2798566a5..488c8e81e05 100644 --- a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/signatureEnhancement.kt +++ b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/signatureEnhancement.kt @@ -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 enhanceSignatures(c: LazyJavaResolverContext, platformSignatures: Collection): Collection { diff --git a/core/descriptors.runtime/src/org/jetbrains/kotlin/descriptors/runtime/components/RuntimeModuleData.kt b/core/descriptors.runtime/src/org/jetbrains/kotlin/descriptors/runtime/components/RuntimeModuleData.kt index 97a6d483817..9ebd61abf9d 100644 --- a/core/descriptors.runtime/src/org/jetbrains/kotlin/descriptors/runtime/components/RuntimeModuleData.kt +++ b/core/descriptors.runtime/src/org/jetbrains/kotlin/descriptors/runtime/components/RuntimeModuleData.kt @@ -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? = null diff --git a/core/util.runtime/src/org/jetbrains/kotlin/load/java/JavaTypeEnhancementState.kt b/core/util.runtime/src/org/jetbrains/kotlin/load/java/JavaTypeEnhancementState.kt new file mode 100644 index 00000000000..bd5ba4a5ba0 --- /dev/null +++ b/core/util.runtime/src/org/jetbrains/kotlin/load/java/JavaTypeEnhancementState.kt @@ -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() + } +} diff --git a/core/util.runtime/src/org/jetbrains/kotlin/load/java/ReportLevel.kt b/core/util.runtime/src/org/jetbrains/kotlin/load/java/ReportLevel.kt new file mode 100644 index 00000000000..072581a99d6 --- /dev/null +++ b/core/util.runtime/src/org/jetbrains/kotlin/load/java/ReportLevel.kt @@ -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 +} \ No newline at end of file diff --git a/core/util.runtime/src/org/jetbrains/kotlin/utils/JavaTypeEnhancementState.kt b/core/util.runtime/src/org/jetbrains/kotlin/utils/JavaTypeEnhancementState.kt deleted file mode 100644 index 94b93613961..00000000000 --- a/core/util.runtime/src/org/jetbrains/kotlin/utils/JavaTypeEnhancementState.kt +++ /dev/null @@ -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, - val enableCompatqualCheckerFrameworkAnnotations: Boolean = COMPATQUAL_CHECKER_FRAMEWORK_ANNOTATIONS_SUPPORT_DEFAULT_VALUE, - val jspecifyReportLevel: ReportLevel = DEFAULT_REPORT_LEVEL_FOR_JSPECIFY, - val nullabilityAnnotationsReportLevel: Map -) { - val description: Array by lazy { - val result = mutableListOf() - 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() - ) - } -} diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/IDELanguageSettingsProvider.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/IDELanguageSettingsProvider.kt index d70e3bc6994..be859e4a9ab 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/IDELanguageSettingsProvider.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/IDELanguageSettingsProvider.kt @@ -40,7 +40,7 @@ import org.jetbrains.kotlin.platform.TargetPlatformVersion import org.jetbrains.kotlin.platform.jvm.JdkPlatform import org.jetbrains.kotlin.platform.subplatformsOfType import org.jetbrains.kotlin.scripting.definitions.ScriptDefinition -import org.jetbrains.kotlin.utils.JavaTypeEnhancementState +import org.jetbrains.kotlin.load.java.JavaTypeEnhancementState object IDELanguageSettingsProvider : LanguageSettingsProvider { override fun getLanguageVersionSettings( diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/project/Platform.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/project/Platform.kt index 4f433f33130..d390959a62b 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/project/Platform.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/project/Platform.kt @@ -47,7 +47,7 @@ import org.jetbrains.kotlin.platform.jvm.JvmPlatforms import org.jetbrains.kotlin.psi.KtElement import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.UserDataProperty -import org.jetbrains.kotlin.utils.JavaTypeEnhancementState +import org.jetbrains.kotlin.load.java.JavaTypeEnhancementState import java.io.File val KtElement.platform: TargetPlatform diff --git a/idea/tests/org/jetbrains/kotlin/idea/highlighter/Jsr305HighlightingTest.kt b/idea/tests/org/jetbrains/kotlin/idea/highlighter/Jsr305HighlightingTest.kt index 5e588c7626b..762de8104dc 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/highlighter/Jsr305HighlightingTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/highlighter/Jsr305HighlightingTest.kt @@ -16,7 +16,7 @@ import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase import org.jetbrains.kotlin.platform.jvm.JvmPlatforms import org.jetbrains.kotlin.test.JUnit3WithIdeaConfigurationRunner import org.jetbrains.kotlin.test.MockLibraryUtilExt -import org.jetbrains.kotlin.utils.ReportLevel +import org.jetbrains.kotlin.load.java.ReportLevel import org.junit.runner.RunWith @RunWith(JUnit3WithIdeaConfigurationRunner::class)