From 39d28f1c9288a7ba1f3fd72e0489ba897eaa8b7a Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Wed, 12 Sep 2018 17:21:20 +0300 Subject: [PATCH] AddAnnotationTargetTest: replace retention annotation if needed This fixes testExpression3 from this test group --- .../idea/quickfix/AddAnnotationTargetFix.kt | 15 +++++++++------ .../addAnnotationTarget/expression3.kt.after | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/AddAnnotationTargetFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/AddAnnotationTargetFix.kt index 4fc6abe7005..245f7d05319 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/AddAnnotationTargetFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/AddAnnotationTargetFix.kt @@ -148,13 +148,16 @@ private fun KtAnnotationEntry.getActualTargetList(): List { private fun KtClass.addAnnotationTargets(annotationTargets: List, psiFactory: KtPsiFactory) { val retentionAnnotationName = KotlinBuiltIns.FQ_NAMES.retention.shortName().asString() - if (annotationTargets.any { it == KotlinTarget.EXPRESSION } - && annotationEntries.none { it.typeReference?.text == retentionAnnotationName }) { - addAnnotationEntry( - psiFactory.createAnnotationEntry( - "@$retentionAnnotationName(${KotlinBuiltIns.FQ_NAMES.annotationRetention.shortName()}.${AnnotationRetention.SOURCE.name})" - ) + if (annotationTargets.any { it == KotlinTarget.EXPRESSION }) { + val retentionEntry = annotationEntries.firstOrNull { it.typeReference?.text == retentionAnnotationName } + val newRetentionEntry = psiFactory.createAnnotationEntry( + "@$retentionAnnotationName(${KotlinBuiltIns.FQ_NAMES.annotationRetention.shortName()}.${AnnotationRetention.SOURCE.name})" ) + if (retentionEntry == null) { + addAnnotationEntry(newRetentionEntry) + } else { + retentionEntry.replace(newRetentionEntry) + } } val targetAnnotationName = KotlinBuiltIns.FQ_NAMES.target.shortName().asString() diff --git a/idea/testData/quickfix/addAnnotationTarget/expression3.kt.after b/idea/testData/quickfix/addAnnotationTarget/expression3.kt.after index 3f709ace784..8060c88cff4 100644 --- a/idea/testData/quickfix/addAnnotationTarget/expression3.kt.after +++ b/idea/testData/quickfix/addAnnotationTarget/expression3.kt.after @@ -1,6 +1,6 @@ // "Add annotation target" "true" @Target(AnnotationTarget.EXPRESSION) -@Retention +@Retention(AnnotationRetention.SOURCE) annotation class Foo fun test() {