Merge use-site targeted annotations into corresponding Annotations

Add PropertyDescriptor.backingField/delegateField to store annotations
on the field directly in an otherwise almost empty descriptor instance,
instead of storing them with use-sites in the corresponding property
descriptor. Instead of AnnotationWithTarget, create AnnotationDescriptor
instances in AnnotationSplitter. Change DescriptorRenderer to render
annotations on "related" declarations when needed, with the explicit
use-site target if applicable.

Most changes in diagnostic test data are related to the fact that
annotations which are known to have an incompatible use-site to the
declaration they're applied at (such as `@param:`-annotation on a
function), are now not loaded at all. It's fine because the code is
erroneous, so it doesn't really matter how do we load annotations with
invalid targets (some of this logic is also changed freely in subsequent
commits). Some changes are also explained by the fact that for example
an annotation on the property which is only applicable to FIELD is now
rendered with an explicit use-site target `@field:`, regardless of
whether it did have that use-site target syntactically or not.

Basically, after this change there's no point in calling
Annotations.getUseSiteTargetedAnnotations/getAllAnnotations anymore
because it's easier and more intuitive to just use Annotations of the
corresponding descriptor -- the backing / delegate field (introduced in
this commit) or the extension receiver / setter parameter (related
behavior was fixed in previous commits). Usages of
use-site-target-related methods will be refactored out in subsequent
commits
This commit is contained in:
Alexander Udalov
2018-08-14 16:45:11 +02:00
parent 8c21e86ded
commit fc87043cb3
68 changed files with 436 additions and 386 deletions
@@ -27,13 +27,9 @@ import org.jetbrains.kotlin.asJava.classes.lazyPub
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.descriptors.annotations.AnnotationWithTarget
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.psi.psiUtil.isPropertyParameter
import org.jetbrains.kotlin.resolve.AnnotationChecker
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.jvm.annotations.JVM_DEFAULT_FQ_NAME
import org.jetbrains.kotlin.resolve.source.getPsi
@@ -146,25 +142,26 @@ private fun getAnnotationDescriptors(declaration: KtDeclaration, annotatedLightE
}
val annotatedDescriptor = when {
descriptor is ClassDescriptor && annotatedLightElement is KtLightMethod && annotatedLightElement.isConstructor -> descriptor.unsubstitutedPrimaryConstructor
descriptor !is PropertyDescriptor || annotatedLightElement !is KtLightMethod -> descriptor
descriptor is ClassDescriptor && annotatedLightElement is KtLightMethod && annotatedLightElement.isConstructor ->
descriptor.unsubstitutedPrimaryConstructor
descriptor !is PropertyDescriptor -> descriptor
annotatedLightElement is KtLightFieldImpl.KtLightFieldForDeclaration -> descriptor.backingField
annotatedLightElement !is KtLightMethod -> descriptor
annotatedLightElement.isGetter -> descriptor.getter
annotatedLightElement.isSetter -> descriptor.setter
else -> descriptor
} ?: return emptyList()
val annotations = annotatedDescriptor.annotations.getAllAnnotations()
.filter { it.matches(annotatedLightElement) }
.map { it.annotation }
val annotations = annotatedDescriptor.annotations.toMutableList()
if (descriptor is PropertyDescriptor) {
val jvmDefault = descriptor.annotations.findAnnotation(JVM_DEFAULT_FQ_NAME)
if (jvmDefault != null) {
return annotations + jvmDefault
annotations.add(jvmDefault)
}
}
return annotations
return annotations
}
private fun hasAnnotationsInSource(declaration: KtDeclaration): Boolean {
@@ -178,19 +175,3 @@ private fun hasAnnotationsInSource(declaration: KtDeclaration): Boolean {
return false
}
private fun AnnotationWithTarget.matches(annotated: KtLightElement<*, *>): Boolean {
if (annotated is KtLightFieldImpl.KtLightFieldForDeclaration) {
if (target == AnnotationUseSiteTarget.FIELD) return true
if (target != null) return false
val declarationSiteTargets = AnnotationChecker.applicableTargetSet(annotation)
return KotlinTarget.FIELD in declarationSiteTargets && KotlinTarget.PROPERTY !in declarationSiteTargets
}
else if (annotated is KtLightParameter && annotated.method.isSetter) {
return target == AnnotationUseSiteTarget.SETTER_PARAMETER
}
return true
}