Add quick fixes for RESTRICTED_RETENTION_FOR_EXPRESSION_ANNOTATION (KT-25574)

This commit is contained in:
Toshiaki Kameyama
2018-08-01 14:18:27 +03:00
committed by Nikolay Krasko
parent fb0261bfc1
commit 58b6360388
28 changed files with 382 additions and 0 deletions
@@ -607,5 +607,8 @@ class QuickFixRegistrar : QuickFixContributor {
MUST_BE_INITIALIZED_OR_BE_ABSTRACT.registerFactory(AbstractAddAccessorsIntention)
MUST_BE_INITIALIZED.registerFactory(AbstractAddAccessorsIntention)
RESTRICTED_RETENTION_FOR_EXPRESSION_ANNOTATION.registerFactory(RestrictedRetentionForExpressionAnnotationFactory)
RESTRICTED_RETENTION_FOR_EXPRESSION_ANNOTATION_WARNING.registerFactory(RestrictedRetentionForExpressionAnnotationFactory)
}
}
@@ -0,0 +1,86 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.quickfix
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.core.ShortenReferences
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.containingClass
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
object RestrictedRetentionForExpressionAnnotationFactory : KotlinIntentionActionsFactory() {
private val sourceRetention = "${KotlinBuiltIns.FQ_NAMES.annotationRetention.asString()}.${AnnotationRetention.SOURCE.name}"
private val sourceRetentionAnnotation = "@${KotlinBuiltIns.FQ_NAMES.retention.asString()}($sourceRetention)"
override fun doCreateActions(diagnostic: Diagnostic): List<IntentionAction> {
val annotationEntry = diagnostic.psiElement as? KtAnnotationEntry ?: return emptyList()
val containingClass = annotationEntry.containingClass() ?: return emptyList()
val retentionAnnotation = containingClass.annotation(KotlinBuiltIns.FQ_NAMES.retention)
return listOf(
RemoveExpressionTargetFix(containingClass),
if (retentionAnnotation == null) AddSourceRetentionFix(containingClass) else ChangeRetentionToSourceFix(containingClass)
)
}
private fun KtClass.annotation(fqName: FqName): KtAnnotationEntry? {
return annotationEntries.firstOrNull {
it.typeReference?.text?.endsWith(fqName.shortName().asString()) == true
&& analyze()[BindingContext.TYPE, it.typeReference]?.constructor?.declarationDescriptor?.fqNameSafe == fqName
}
}
private class AddSourceRetentionFix(element: KtClass) : KotlinQuickFixAction<KtClass>(element) {
override fun getText() = "Add SOURCE retention"
override fun getFamilyName() = text
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val element = element ?: return
val added = element.addAnnotationEntry(KtPsiFactory(element).createAnnotationEntry(sourceRetentionAnnotation))
ShortenReferences.DEFAULT.process(added)
}
}
private class ChangeRetentionToSourceFix(element: KtClass) : KotlinQuickFixAction<KtClass>(element) {
override fun getText() = "Change existent retention to SOURCE"
override fun getFamilyName() = text
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val retentionAnnotation = element?.annotation(KotlinBuiltIns.FQ_NAMES.retention) ?: return
val psiFactory = KtPsiFactory(retentionAnnotation)
val added = if (retentionAnnotation.valueArgumentList == null) {
retentionAnnotation.add(psiFactory.createCallArguments("($sourceRetention)")) as KtValueArgumentList
} else {
if (retentionAnnotation.valueArguments.isNotEmpty()) {
retentionAnnotation.valueArgumentList?.removeArgument(0)
}
retentionAnnotation.valueArgumentList?.addArgument(psiFactory.createArgument(sourceRetention))
}
if (added != null) {
ShortenReferences.DEFAULT.process(added)
}
}
}
private class RemoveExpressionTargetFix(element: KtClass) : KotlinQuickFixAction<KtClass>(element) {
override fun getText() = "Remove EXPRESSION target"
override fun getFamilyName() = text
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
element?.annotation(KotlinBuiltIns.FQ_NAMES.target)?.delete()
}
}
}
@@ -0,0 +1,9 @@
// "Add SOURCE retention" "false"
// DISABLE-ERRORS
// ACTION: Change existent retention to SOURCE
// ACTION: Make internal
// ACTION: Make private
// ACTION: Remove EXPRESSION target
<caret>@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
@@ -0,0 +1,9 @@
// "Add SOURCE retention" "false"
// DISABLE-ERRORS
// ACTION: Change existent retention to SOURCE
// ACTION: Make internal
// ACTION: Make private
// ACTION: Remove EXPRESSION target
<caret>@Retention
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
@@ -0,0 +1,9 @@
// "Add SOURCE retention" "false"
// DISABLE-ERRORS
// ACTION: Change existent retention to SOURCE
// ACTION: Make internal
// ACTION: Make private
// ACTION: Remove EXPRESSION target
<caret>@Retention()
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
@@ -0,0 +1,3 @@
// "Add SOURCE retention" "true"
<caret>@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
@@ -0,0 +1,4 @@
// "Add SOURCE retention" "true"
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
@@ -0,0 +1,6 @@
// "Add SOURCE retention" "true"
annotation class Retention
@Retention
<caret>@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
@@ -0,0 +1,7 @@
// "Add SOURCE retention" "true"
annotation class Retention
@kotlin.annotation.Retention(AnnotationRetention.SOURCE)
@Retention
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
@@ -0,0 +1,4 @@
// "Change existent retention to SOURCE" "true"
<caret>@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
@@ -0,0 +1,4 @@
// "Change existent retention to SOURCE" "true"
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
@@ -0,0 +1,4 @@
// "Change existent retention to SOURCE" "true"
<caret>@Retention
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
@@ -0,0 +1,4 @@
// "Change existent retention to SOURCE" "true"
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
@@ -0,0 +1,4 @@
// "Change existent retention to SOURCE" "true"
<caret>@Retention()
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
@@ -0,0 +1,4 @@
// "Change existent retention to SOURCE" "true"
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
@@ -0,0 +1,6 @@
// "Change existent retention to SOURCE" "true"
class AnnotationRetention
<caret>@Retention()
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
@@ -0,0 +1,8 @@
import kotlin.annotation.AnnotationRetention
// "Change existent retention to SOURCE" "true"
class AnnotationRetention
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
@@ -0,0 +1,8 @@
// "Change existent retention to SOURCE" "false"
// DISABLE-ERRORS
// ACTION: Add SOURCE retention
// ACTION: Make internal
// ACTION: Make private
// ACTION: Remove EXPRESSION target
<caret>@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
@@ -0,0 +1,4 @@
// "Remove EXPRESSION target" "true"
<caret>@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
@@ -0,0 +1,3 @@
// "Remove EXPRESSION target" "true"
@Retention(AnnotationRetention.BINARY)
annotation class Ann
@@ -0,0 +1,4 @@
// "Remove EXPRESSION target" "true"
<caret>@Retention
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
@@ -0,0 +1,3 @@
// "Remove EXPRESSION target" "true"
@Retention
annotation class Ann
@@ -0,0 +1,4 @@
// "Remove EXPRESSION target" "true"
<caret>@Retention()
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
@@ -0,0 +1,3 @@
// "Remove EXPRESSION target" "true"
@Retention()
annotation class Ann
@@ -0,0 +1,3 @@
// "Remove EXPRESSION target" "true"
<caret>@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
@@ -0,0 +1,2 @@
// "Remove EXPRESSION target" "true"
annotation class Ann
@@ -3885,6 +3885,58 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
}
}
@TestMetadata("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RestrictedRetentionForExpressionAnnotation extends AbstractQuickFixMultiFileTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTestWithExtraFile, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInRestrictedRetentionForExpressionAnnotation() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), TargetBackend.ANY, true);
}
@TestMetadata("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/addSourceRetention")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class AddSourceRetention extends AbstractQuickFixMultiFileTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTestWithExtraFile, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInAddSourceRetention() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/addSourceRetention"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), TargetBackend.ANY, true);
}
}
@TestMetadata("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/changeRetentionToSource")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ChangeRetentionToSource extends AbstractQuickFixMultiFileTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTestWithExtraFile, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInChangeRetentionToSource() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/changeRetentionToSource"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), TargetBackend.ANY, true);
}
}
@TestMetadata("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/removeExpressionTarget")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RemoveExpressionTarget extends AbstractQuickFixMultiFileTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTestWithExtraFile, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInRemoveExpressionTarget() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/removeExpressionTarget"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), TargetBackend.ANY, true);
}
}
}
@TestMetadata("idea/testData/quickfix/simplifyComparison")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -10855,6 +10855,128 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
}
@TestMetadata("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RestrictedRetentionForExpressionAnnotation extends AbstractQuickFixTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInRestrictedRetentionForExpressionAnnotation() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/addSourceRetention")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class AddSourceRetention extends AbstractQuickFixTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInAddSourceRetention() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/addSourceRetention"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("binaryRetention.kt")
public void testBinaryRetention() throws Exception {
runTest("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/addSourceRetention/binaryRetention.kt");
}
@TestMetadata("emptyRetention.kt")
public void testEmptyRetention() throws Exception {
runTest("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/addSourceRetention/emptyRetention.kt");
}
@TestMetadata("emptyRetention2.kt")
public void testEmptyRetention2() throws Exception {
runTest("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/addSourceRetention/emptyRetention2.kt");
}
@TestMetadata("noRetention.kt")
public void testNoRetention() throws Exception {
runTest("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/addSourceRetention/noRetention.kt");
}
@TestMetadata("noRetention2.kt")
public void testNoRetention2() throws Exception {
runTest("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/addSourceRetention/noRetention2.kt");
}
}
@TestMetadata("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/changeRetentionToSource")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ChangeRetentionToSource extends AbstractQuickFixTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInChangeRetentionToSource() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/changeRetentionToSource"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("binaryRetention.kt")
public void testBinaryRetention() throws Exception {
runTest("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/changeRetentionToSource/binaryRetention.kt");
}
@TestMetadata("emptyRetention.kt")
public void testEmptyRetention() throws Exception {
runTest("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/changeRetentionToSource/emptyRetention.kt");
}
@TestMetadata("emptyRetention2.kt")
public void testEmptyRetention2() throws Exception {
runTest("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/changeRetentionToSource/emptyRetention2.kt");
}
@TestMetadata("emptyRetention3.kt")
public void testEmptyRetention3() throws Exception {
runTest("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/changeRetentionToSource/emptyRetention3.kt");
}
@TestMetadata("noRetention.kt")
public void testNoRetention() throws Exception {
runTest("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/changeRetentionToSource/noRetention.kt");
}
}
@TestMetadata("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/removeExpressionTarget")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RemoveExpressionTarget extends AbstractQuickFixTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInRemoveExpressionTarget() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/removeExpressionTarget"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("binaryRetention.kt")
public void testBinaryRetention() throws Exception {
runTest("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/removeExpressionTarget/binaryRetention.kt");
}
@TestMetadata("emptyRetention.kt")
public void testEmptyRetention() throws Exception {
runTest("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/removeExpressionTarget/emptyRetention.kt");
}
@TestMetadata("emptyRetention2.kt")
public void testEmptyRetention2() throws Exception {
runTest("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/removeExpressionTarget/emptyRetention2.kt");
}
@TestMetadata("noRetention.kt")
public void testNoRetention() throws Exception {
runTest("idea/testData/quickfix/restrictedRetentionForExpressionAnnotation/removeExpressionTarget/noRetention.kt");
}
}
}
@TestMetadata("idea/testData/quickfix/simplifyComparison")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)