From 9702b97d8d3aee95f42d08796d5d1078e662ac5b Mon Sep 17 00:00:00 2001 From: Roman Golyshev Date: Fri, 9 Dec 2022 16:22:59 +0100 Subject: [PATCH] [kotlin] KTIJ-23832 Select proper argument for Kotlin annotations in `KtDefaultAnnotationArgumentReference` Kotlin annotations can be called without named parameters even if their names are different from `value`. So there's no need to search for the property named "value", since there might be none --- .../KtDefaultAnnotationArgumentReference.kt | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/analysis/kt-references/src/org/jetbrains/kotlin/idea/references/KtDefaultAnnotationArgumentReference.kt b/analysis/kt-references/src/org/jetbrains/kotlin/idea/references/KtDefaultAnnotationArgumentReference.kt index cf67518ba66..3c426d97415 100644 --- a/analysis/kt-references/src/org/jetbrains/kotlin/idea/references/KtDefaultAnnotationArgumentReference.kt +++ b/analysis/kt-references/src/org/jetbrains/kotlin/idea/references/KtDefaultAnnotationArgumentReference.kt @@ -15,10 +15,22 @@ import org.jetbrains.kotlin.psi.KtAnnotationEntry import org.jetbrains.kotlin.psi.KtParameter import org.jetbrains.kotlin.psi.KtPrimaryConstructor import org.jetbrains.kotlin.psi.KtValueArgument -import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject -import org.jetbrains.kotlin.psi.psiUtil.findPropertyByName import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType +/** + * A reference pointing to a single argument in annotation's constructor call. + * It is created only when the following conditions hold: + * + * - Annotation's constructor call has a **single** argument + * - This argument is passed as is, without an explicit name + * + * Examples: + * ``` + * @Foo("bar") // reference is created + * @Foo("bar", "baz") // no reference, there are two arguments + * @Foo(name = "bar") // no reference, named argument is used + * ``` + */ class KtDefaultAnnotationArgumentReference(element: KtValueArgument) : AbstractKtReference(element) { override val resolver: ResolveCache.PolyVariantResolver get() = Resolver @@ -41,9 +53,9 @@ class KtDefaultAnnotationArgumentReference(element: KtValueArgument) : AbstractK override fun resolve(t: KtReference, incompleteCode: Boolean): Array { require(t is KtDefaultAnnotationArgumentReference) val annotationPsi = t.resolveAnnotationCallee() ?: return emptyArray() - val name = PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME return when (annotationPsi) { is PsiClass -> { + val name = PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME val signature = MethodSignatureUtil.createMethodSignature( name, PsiType.EMPTY_ARRAY, PsiTypeParameter.EMPTY_ARRAY, PsiSubstitutor.EMPTY ) @@ -51,7 +63,9 @@ class KtDefaultAnnotationArgumentReference(element: KtValueArgument) : AbstractK arrayOf(PsiElementResolveResult(method)) } is KtPrimaryConstructor -> { - val property = annotationPsi.containingClassOrObject?.findPropertyByName(name) as? KtParameter ?: return emptyArray() + // parameters in primary constructor on Kotlin annotation can have any names, + // so we just take the first parameter + val property = annotationPsi.valueParameters.firstOrNull() ?: return emptyArray() arrayOf(PsiElementResolveResult(property)) } else -> emptyArray()