Support using Java-repeatable annotations in Kotlin

#KT-12794
This commit is contained in:
Alexander Udalov
2021-07-20 16:30:04 +02:00
parent f723389565
commit 87130edfa2
42 changed files with 958 additions and 42 deletions
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.resolve.jvm.checkers
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.descriptors.annotations.KotlinRetention
@@ -19,13 +20,17 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.getAnnotationRetention
import org.jetbrains.kotlin.resolve.descriptorUtil.isRepeatableAnnotation
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm
object RepeatableAnnotationChecker : AdditionalAnnotationChecker {
class RepeatableAnnotationChecker(languageVersionSettings: LanguageVersionSettings) : AdditionalAnnotationChecker {
private val nonSourceDisallowed = !languageVersionSettings.supportsFeature(LanguageFeature.RepeatableAnnotations)
override fun checkEntries(
entries: List<KtAnnotationEntry>,
actualTargets: List<KotlinTarget>,
trace: BindingTrace,
languageVersionSettings: LanguageVersionSettings
) {
if (entries.isEmpty()) return
val entryTypesWithAnnotations = hashMapOf<FqName, MutableList<AnnotationUseSiteTarget?>>()
for (entry in entries) {
@@ -42,7 +47,9 @@ object RepeatableAnnotationChecker : AdditionalAnnotationChecker {
&& classDescriptor.isRepeatableAnnotation()
&& classDescriptor.getAnnotationRetention() != KotlinRetention.SOURCE
) {
trace.report(ErrorsJvm.NON_SOURCE_REPEATED_ANNOTATION.on(entry))
if (nonSourceDisallowed) {
trace.report(ErrorsJvm.NON_SOURCE_REPEATED_ANNOTATION.on(entry))
}
}
existingTargetsForAnnotation.add(useSiteTarget)
@@ -71,7 +71,7 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
MAP.put(POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION, "Only named arguments are available for Java annotations");
MAP.put(DEPRECATED_JAVA_ANNOTATION, "This annotation is deprecated in Kotlin. Use ''@{0}'' instead", TO_STRING);
MAP.put(NON_SOURCE_REPEATED_ANNOTATION, "Repeatable annotations with non-SOURCE retention are not yet supported");
MAP.put(NON_SOURCE_REPEATED_ANNOTATION, "Repeatable annotations with non-SOURCE retention are only supported starting from Kotlin 1.6");
MAP.put(ANNOTATION_IS_NOT_APPLICABLE_TO_MULTIFILE_CLASSES, "Annotation ''@{0}'' is not applicable to the multi-file classes", TO_STRING);
MAP.put(JVM_PACKAGE_NAME_CANNOT_BE_EMPTY, "''@JvmPackageName'' annotation value cannot be empty");
@@ -73,7 +73,6 @@ object JvmPlatformConfigurator : PlatformConfiguratorBase(
),
additionalAnnotationCheckers = listOf(
RepeatableAnnotationChecker,
FileClassAnnotationsChecker,
ExplicitMetadataChecker
),
@@ -125,5 +124,6 @@ object JvmPlatformConfigurator : PlatformConfiguratorBase(
override fun configureModuleDependentCheckers(container: StorageComponentContainer) {
super.configureModuleDependentCheckers(container)
container.useImpl<ExpectedActualDeclarationChecker>()
container.useImpl<RepeatableAnnotationChecker>()
}
}