From 108a40261b3b9a1ef840db1124db7b9f24849ed5 Mon Sep 17 00:00:00 2001 From: Nicolay Mitropolsky Date: Mon, 28 Aug 2017 19:14:35 +0300 Subject: [PATCH] `KtLightAnnotationForSourceEntry` fix for reading unnamed java-annotation parameter --- .../asJava/elements/lightAnnotations.kt | 2 +- .../kotlin/asJava/KtLightAnnotationTest.kt | 33 +++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/lightAnnotations.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/lightAnnotations.kt index e217c028c17..812dfb9c72d 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/lightAnnotations.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/lightAnnotations.kt @@ -107,7 +107,7 @@ class KtLightAnnotationForSourceEntry( val annotationConstructor = resolvedCall.resultingDescriptor val parameterName = memberValue.getNonStrictParentOfType()?.name ?: - memberValue.getNonStrictParentOfType()?.name ?: + memberValue.getNonStrictParentOfType()?.takeIf { it.containingClass?.isAnnotationType == true }?.name ?: "value" val parameter = annotationConstructor.valueParameters.singleOrNull { it.name.asString() == parameterName } ?: return null diff --git a/idea/tests/org/jetbrains/kotlin/asJava/KtLightAnnotationTest.kt b/idea/tests/org/jetbrains/kotlin/asJava/KtLightAnnotationTest.kt index 7e99b2d1fc7..9726b924f87 100644 --- a/idea/tests/org/jetbrains/kotlin/asJava/KtLightAnnotationTest.kt +++ b/idea/tests/org/jetbrains/kotlin/asJava/KtLightAnnotationTest.kt @@ -49,10 +49,31 @@ class KtLightAnnotationTest : KotlinLightCodeInsightFixtureTestCase() { val annotations = myFixture.findClass("AnnotatedClass").fields.single() .expectAnnotations(2).single { it.qualifiedName == "Autowired" } val annotationAttributeVal = annotations.findAttributeValue("required") as PsiElement - assertTextAndRange("true", annotationAttributeVal) + assertTextRangeAndValue("true", true, annotationAttributeVal) + } - val computed = JavaPsiFacade.getInstance(project).constantEvaluationHelper.computeConstantExpression(annotationAttributeVal) - TestCase.assertEquals(true, computed) + fun testStringAnnotationWithUnnamedParameter() { + myFixture.addClass(""" + import java.lang.annotation.ElementType; + import java.lang.annotation.Target; + + @Target(ElementType.PARAMETER) + public @interface Qualifier { + String value(); + } + """.trimIndent()) + + myFixture.configureByText("AnnotatedClass.kt", """ + class AnnotatedClass { + fun bar(@Qualifier("foo") param: String){} + } + """.trimIndent()) + myFixture.testHighlighting("Qualifier.java", "AnnotatedClass.kt") + + val annotations = myFixture.findClass("AnnotatedClass").methods.first { it.name == "bar" }.parameterList.parameters.single() + .expectAnnotations(2).single { it.qualifiedName == "Qualifier" } + val annotationAttributeVal = annotations.findAttributeValue("value") as PsiElement + assertTextRangeAndValue("\"foo\"", "foo", annotationAttributeVal) } fun testAnnotationsInAnnotationsDeclarations() { @@ -347,6 +368,12 @@ class KtLightAnnotationTest : KotlinLightCodeInsightFixtureTestCase() { TestCase.assertEquals(expected, psiElement.textRange.substring(psiElement.containingFile.text)) } + private fun assertTextRangeAndValue(expected: String, value: Any?, psiElement: PsiElement) { + assertTextAndRange(expected, psiElement) + val result = JavaPsiFacade.getInstance(project).constantEvaluationHelper.computeConstantExpression(psiElement) + TestCase.assertEquals(value, result) + } + private fun PsiModifierListOwner.expectAnnotations(number: Int): Array = this.annotations.apply { TestCase.assertEquals("expected one annotation, found ${this.joinToString(", ") { it.qualifiedName ?: "unknown" }}",