Provide more clear API around AnnotationChecker.applicableTargetSet

This commit is contained in:
Mikhail Glukhikh
2021-08-23 11:41:29 +03:00
committed by TeamCityServer
parent 19ab0ab2f8
commit 255eb87375
6 changed files with 14 additions and 10 deletions
@@ -297,16 +297,21 @@ class AnnotationChecker(
val descriptor = trace.get(BindingContext.ANNOTATION, entry) ?: return KotlinTarget.DEFAULT_TARGET_SET
// For descriptor with error type, all targets are considered as possible
if (descriptor.type.isError) return KotlinTarget.ALL_TARGET_SET
return descriptor.annotationClass?.let(this::applicableTargetSet) ?: KotlinTarget.DEFAULT_TARGET_SET
return descriptor.annotationClass?.let(this::applicableTargetSetFromTargetAnnotationOrNull) ?: KotlinTarget.DEFAULT_TARGET_SET
}
@JvmStatic
fun applicableTargetSet(descriptor: AnnotationDescriptor): Set<KotlinTarget> {
val classDescriptor = descriptor.annotationClass ?: return emptySet()
return applicableTargetSet(classDescriptor) ?: KotlinTarget.DEFAULT_TARGET_SET
return applicableTargetSet(classDescriptor)
}
fun applicableTargetSet(classDescriptor: ClassDescriptor): Set<KotlinTarget>? {
fun applicableTargetSet(classDescriptor: ClassDescriptor): Set<KotlinTarget> {
val targetEntryDescriptor = classDescriptor.annotations.findAnnotation(StandardNames.FqNames.target)
return targetEntryDescriptor?.let { loadAnnotationTargets(it) } ?: KotlinTarget.DEFAULT_TARGET_SET
}
fun applicableTargetSetFromTargetAnnotationOrNull(classDescriptor: ClassDescriptor): Set<KotlinTarget>? {
val targetEntryDescriptor = classDescriptor.annotations.findAnnotation(StandardNames.FqNames.target) ?: return null
return loadAnnotationTargets(targetEntryDescriptor)
}
@@ -7,7 +7,6 @@ package org.jetbrains.kotlin.resolve.checkers
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
@@ -27,7 +26,7 @@ class AnnotationClassTargetAndRetentionChecker : DeclarationChecker {
if (declaration !is KtClassOrObject) return
if (!DescriptorUtils.isAnnotationClass(descriptor)) return
val targets = AnnotationChecker.applicableTargetSet(descriptor) ?: return
val targets = AnnotationChecker.applicableTargetSetFromTargetAnnotationOrNull(descriptor) ?: return
val retention = descriptor.getAnnotationRetention() ?: KotlinRetention.RUNTIME
if (targets.contains(KotlinTarget.EXPRESSION) && retention != KotlinRetention.SOURCE) {
@@ -59,7 +59,7 @@ class ExperimentalMarkerDeclarationAnnotationChecker(private val module: ModuleD
}
val annotationClass = annotation.annotationClass ?: continue
if (annotationClass.annotations.any { it.fqName in OptInNames.EXPERIMENTAL_FQ_NAMES }) {
val applicableTargets = AnnotationChecker.applicableTargetSet(annotationClass) ?: KotlinTarget.DEFAULT_TARGET_SET
val applicableTargets = AnnotationChecker.applicableTargetSet(annotationClass)
val possibleTargets = applicableTargets.intersect(actualTargets)
val annotationUseSiteTarget = entry.useSiteTarget?.getAnnotationUseSiteTarget()
if (PROPERTY_GETTER in possibleTargets ||