Implement annotation related migrational quick fixes

This commit is contained in:
Denis Zharkov
2015-09-08 19:27:07 +03:00
parent 7b3303c152
commit 372e35436a
35 changed files with 509 additions and 3 deletions
@@ -47,4 +47,13 @@ public class JetAnnotation extends JetElementImplStub<KotlinPlaceHolderStub<JetA
public JetAnnotationUseSiteTarget getUseSiteTarget() {
return getStubOrPsiChild(JetStubElementTypes.ANNOTATION_TARGET);
}
public void removeEntry(@NotNull JetAnnotationEntry entry) {
if (getEntries().size() > 1) {
entry.delete();
}
else {
delete();
}
}
}
@@ -99,7 +99,11 @@ public class KotlinCleanupInspection(): LocalInspectionTool(), CleanupLocalInspe
Errors.USELESS_CAST,
Errors.USELESS_ELVIS,
ErrorsJvm.POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION,
Errors.DEPRECATED_SYMBOL_WITH_MESSAGE
Errors.DEPRECATED_SYMBOL_WITH_MESSAGE,
Errors.DEPRECATED_ANNOTATION_THAT_BECOMES_MODIFIER,
Errors.DEPRECATED_DECAPITALIZED_ANNOTATION,
Errors.DEPRECATED_ESCAPED_MODIFIER,
Errors.DEPRECATED_UNESCAPED_ANNOTATION
)
private fun Diagnostic.isObsoleteLabel(): Boolean {
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.idea.quickfix.createFromUsage.createClass.CreateClas
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.createVariable.CreateLocalVariableActionFactory
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.createVariable.CreateParameterByNamedArgumentActionFactory
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.createVariable.CreateParameterByRefActionFactory
import org.jetbrains.kotlin.idea.quickfix.migration.*
import org.jetbrains.kotlin.idea.quickfix.replaceWith.DeprecatedSymbolUsageFix
import org.jetbrains.kotlin.idea.quickfix.replaceWith.DeprecatedSymbolUsageInWholeProjectFix
import org.jetbrains.kotlin.lexer.JetTokens.*
@@ -324,5 +325,11 @@ public class QuickFixRegistrar : QuickFixContributor {
UPPER_BOUND_VIOLATED.registerFactory(AddGenericUpperBoundFix.Factory)
TYPE_INFERENCE_UPPER_BOUND_VIOLATED.registerFactory(AddGenericUpperBoundFix.Factory)
DEPRECATED_UNESCAPED_ANNOTATION.registerFactory(UnescapedAnnotationFix.Factory)
DEPRECATED_ESCAPED_MODIFIER.registerFactory(EscapedModifierFix.Factory)
DEPRECATED_ANNOTATION_THAT_BECOMES_MODIFIER.registerFactory(ReplaceAnnotationWithModifierFix.Factory)
DEPRECATED_DECAPITALIZED_ANNOTATION.registerFactory(DecapitalizedAnnotationFix.Factory)
DEPRECATED_ANNOTATION_USE.registerFactory(RemoveAnnotationFix.Factory)
}
}
@@ -0,0 +1,56 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.quickfix.migration
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.idea.quickfix.CleanupFix
import org.jetbrains.kotlin.idea.quickfix.JetIntentionAction
import org.jetbrains.kotlin.idea.quickfix.JetSingleIntentionActionFactory
import org.jetbrains.kotlin.idea.quickfix.replaceWith.ClassUsageReplacementStrategy
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
public class DecapitalizedAnnotationFix(
element: JetSimpleNameExpression,
private val classDescriptor: ClassDescriptor,
private val replacer: () -> JetElement
) : JetIntentionAction<JetSimpleNameExpression>(element), CleanupFix {
override fun getFamilyName() = "Replace deprecated decapitalized annotations"
override fun getText() = "Replace with '${classDescriptor.fqNameSafe.asString()}'"
override fun invoke(project: Project, editor: Editor?, file: JetFile) {
replacer()
}
companion object Factory : JetSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val diagnosticWithParameters = Errors.DEPRECATED_DECAPITALIZED_ANNOTATION.cast(diagnostic)
val classDescriptor = diagnosticWithParameters.a
val element = diagnosticWithParameters.psiElement
val replacement = JetPsiFactory(element).createType(classDescriptor.fqNameSafe.asString()).typeElement as JetUserType
val replacer = ClassUsageReplacementStrategy(replacement).createReplacer(element) ?: return null
return DecapitalizedAnnotationFix(element, classDescriptor, replacer)
}
}
}
@@ -0,0 +1,42 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.quickfix.migration
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.quickfix.CleanupFix
import org.jetbrains.kotlin.idea.quickfix.JetIntentionAction
import org.jetbrains.kotlin.idea.quickfix.JetSingleIntentionActionFactory
import org.jetbrains.kotlin.lexer.JetModifierKeywordToken
import org.jetbrains.kotlin.psi.JetFile
import org.jetbrains.kotlin.psi.JetPsiFactory
public class EscapedModifierFix(element: PsiElement) : JetIntentionAction<PsiElement>(element), CleanupFix {
override fun getText() = "Remove '@'"
override fun getFamilyName() = text
override fun invoke(project: Project, editor: Editor?, file: JetFile) {
val modifier = element.node.elementType as JetModifierKeywordToken
element.replace(JetPsiFactory(project).createModifier(modifier))
}
companion object Factory : JetSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic) = EscapedModifierFix(diagnostic.psiElement)
}
}
@@ -0,0 +1,39 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.quickfix.migration
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.quickfix.JetIntentionAction
import org.jetbrains.kotlin.idea.quickfix.JetSingleIntentionActionFactory
import org.jetbrains.kotlin.idea.quickfix.quickfixUtil.createIntentionForFirstParentOfType
import org.jetbrains.kotlin.psi.JetAnnotationEntry
import org.jetbrains.kotlin.psi.JetFile
public class RemoveAnnotationFix(element: JetAnnotationEntry) : JetIntentionAction<JetAnnotationEntry>(element) {
override fun getFamilyName(): String = getText()
override fun getText(): String = "Remove annotation"
override fun invoke(project: Project, editor: Editor?, file: JetFile) {
element.delete()
}
object Factory : JetSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic) = diagnostic.createIntentionForFirstParentOfType(::RemoveAnnotationFix)
}
}
@@ -0,0 +1,70 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.quickfix.migration
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.idea.core.CommentSaver
import org.jetbrains.kotlin.idea.quickfix.CleanupFix
import org.jetbrains.kotlin.idea.quickfix.JetIntentionAction
import org.jetbrains.kotlin.idea.quickfix.JetSingleIntentionActionFactory
import org.jetbrains.kotlin.lexer.JetModifierKeywordToken
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
public class ReplaceAnnotationWithModifierFix(
element: JetAnnotationEntry,
private val replacement: JetModifierKeywordToken
) : JetIntentionAction<JetAnnotationEntry>(element), CleanupFix {
override fun getFamilyName() = "Replace with modifier"
override fun getText() = "Replace with '${replacement.value}'"
override fun invoke(project: Project, editor: Editor?, file: JetFile) {
val psiFactory = JetPsiFactory(project)
val modifier = psiFactory.createModifier(replacement)
val parent = element.parent
if (parent !is JetAnnotation) {
val commentSaver = CommentSaver(element, saveLineBreaks = true)
val result = element.replace(modifier)
commentSaver.restore(result)
}
else {
// within annotation list
val modifierListOwner = (parent.parent?.parent as? JetModifierListOwner) ?: return
// insert modifier
modifierListOwner.addModifier(replacement)
parent.removeEntry(element)
}
}
companion object Factory : JetSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val annotationEntry = diagnostic.psiElement.getNonStrictParentOfType<JetAnnotationEntry>() ?: return null
val modifierValue = Errors.DEPRECATED_ANNOTATION_THAT_BECOMES_MODIFIER.cast(diagnostic).a
return ReplaceAnnotationWithModifierFix(
annotationEntry, JetTokens.ANNOTATION_MODIFIERS_KEYWORDS_ARRAY.first() { it.value == modifierValue }
)
}
}
}
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.quickfix.migration
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.quickfix.CleanupFix
import org.jetbrains.kotlin.idea.quickfix.JetIntentionAction
import org.jetbrains.kotlin.idea.quickfix.JetSingleIntentionActionFactory
import org.jetbrains.kotlin.psi.JetAnnotationEntry
import org.jetbrains.kotlin.psi.JetFile
import org.jetbrains.kotlin.idea.quickfix.quickfixUtil.*
public class UnescapedAnnotationFix(element: JetAnnotationEntry) : JetIntentionAction<JetAnnotationEntry>(element), CleanupFix {
override fun getText() = "Add '@' before annotation"
override fun getFamilyName() = text
override fun invoke(project: Project, editor: Editor?, file: JetFile) = element.addAtSymbol()
companion object Factory : JetSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic) = diagnostic.createIntentionForFirstParentOfType(::UnescapedAnnotationFix)
}
}
@@ -0,0 +1,24 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.quickfix.migration
import org.jetbrains.kotlin.psi.JetAnnotationEntry
import org.jetbrains.kotlin.psi.JetPsiFactory
public fun JetAnnotationEntry.addAtSymbol() {
addBefore(JetPsiFactory(this).createAnnotationEntry("@ann").atSymbol!!, firstChild)
}
+19
View File
@@ -37,3 +37,22 @@ fun unnecessaryCast(x: String) = x as String
fun unnecessaryElvis(x: String) = x ?: ""
JavaAnn(1, "abc") class MyClass
annotation class Ann()
Ann class A1
Ann() class A2
kotlin.data class A3
@inline @private fun <T> baz() {
@suppress("UNCHECKED_CAST")
(1 as T)
@data class Local
}
deprecated("123", ReplaceWith("34")) class Obsolete
native fun nativeFun(): Int
+20 -1
View File
@@ -35,4 +35,23 @@ fun unnecessaryCast(x: String) = x
fun unnecessaryElvis(x: String) = x
JavaAnn(1, arg1 = "abc") class MyClass
@JavaAnn(1, arg1 = "abc") class MyClass
annotation class Ann()
@Ann class A1
@Ann() class A2
data class A3
inline private fun <T> baz() {
@Suppress("UNCHECKED_CAST")
(1 as T)
data class Local
}
@Deprecated("123", ReplaceWith("34")) class Obsolete
external fun nativeFun(): Int
@@ -3,6 +3,6 @@
// ACTION: Make private
// ACTION: Make internal
J.<caret>foo(1, "2") fun test() {
@J.<caret>foo(1, "2") fun test() {
}
@@ -0,0 +1,5 @@
// "Replace with 'data'" "true"
@data<caret>
/* abc*/(1)
class A
@@ -0,0 +1,4 @@
// "Replace with 'data'" "true"
data/* abc*/
class A
@@ -0,0 +1,5 @@
// "Replace with 'inline'" "true"
annotation class Ann(val x: Int)
kotlin.inline<caret> fun foo() {}
@@ -0,0 +1,5 @@
// "Replace with 'inline'" "true"
annotation class Ann(val x: Int)
inline fun foo() {}
@@ -0,0 +1,6 @@
// "Replace with 'data'" "true"
annotation class Ann
@[dat<caret>a Ann]
/* abc*/
class A
@@ -0,0 +1,6 @@
// "Replace with 'data'" "true"
annotation class Ann
@[Ann]
data /* abc*/
class A
@@ -0,0 +1,5 @@
// "Replace with 'data'" "true"
@[dat<caret>a]
/* abc*/
class A
@@ -0,0 +1,4 @@
// "Replace with 'data'" "true"
data /* abc*/
class A
@@ -0,0 +1,3 @@
// "Replace with 'kotlin.Deprecated'" "true"
@deprecated<caret>("")
class A
@@ -0,0 +1,3 @@
// "Replace with 'kotlin.Deprecated'" "true"
@Deprecated<caret>("")
class A
@@ -0,0 +1,3 @@
// "Replace with 'kotlin.Deprecated'" "true"
fun foo(x: kotlin.deprecated<caret>) {}
@@ -0,0 +1,3 @@
// "Replace with 'kotlin.Deprecated'" "true"
fun foo(x: Deprecated) {}
+5
View File
@@ -0,0 +1,5 @@
// "Remove '@'" "true"
@private<caret>
/* abc*/
class A
@@ -0,0 +1,5 @@
// "Remove '@'" "true"
private
/* abc*/
class A
+3
View File
@@ -0,0 +1,3 @@
// "Replace with 'crossinline'" "true"
inline fun inlineFun(@inlineOptions(InlineOption.ONLY_LOCAL_RETURN)<caret> block: () -> Int) {}
@@ -0,0 +1,3 @@
// "Replace with 'crossinline'" "true"
inline fun inlineFun(crossinline block: () -> Int) {}
@@ -0,0 +1,3 @@
// "Remove annotation" "true"
inline fun inlineFun(@inlineOptions(InlineOption.LOCAL_CONTINUE_AND_BREAK)<caret> block: () -> Int) {}
@@ -0,0 +1,3 @@
// "Remove annotation" "true"
inline fun inlineFun(block: () -> Int) {}
+8
View File
@@ -0,0 +1,8 @@
// "Replace with 'tailrec'" "true"
@tailRecursive<caret>
fun foo() {
if (1 > 2) {
foo()
}
}
+8
View File
@@ -0,0 +1,8 @@
// "Replace with 'tailrec'" "true"
tailrec
fun foo() {
if (1 > 2) {
foo()
}
}
@@ -0,0 +1,7 @@
// "Add '@' before annotation" "true"
annotation class Ann(val x: Int)
Ann<caret>
/* abc*/(1)
class A
@@ -0,0 +1,7 @@
// "Add '@' before annotation" "true"
annotation class Ann(val x: Int)
@Ann<caret>
/* abc*/(1)
class A
@@ -4019,6 +4019,60 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/migration"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("annotationModifier.kt")
public void testAnnotationModifier() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/annotationModifier.kt");
doTest(fileName);
}
@TestMetadata("annotationModifier2.kt")
public void testAnnotationModifier2() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/annotationModifier2.kt");
doTest(fileName);
}
@TestMetadata("annotationModifier3.kt")
public void testAnnotationModifier3() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/annotationModifier3.kt");
doTest(fileName);
}
@TestMetadata("annotationModifier4.kt")
public void testAnnotationModifier4() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/annotationModifier4.kt");
doTest(fileName);
}
@TestMetadata("escapedModifier.kt")
public void testEscapedModifier() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/escapedModifier.kt");
doTest(fileName);
}
@TestMetadata("inlineOptions.kt")
public void testInlineOptions() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/inlineOptions.kt");
doTest(fileName);
}
@TestMetadata("inlineOptionsWithBreak.kt")
public void testInlineOptionsWithBreak() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/inlineOptionsWithBreak.kt");
doTest(fileName);
}
@TestMetadata("tailRec.kt")
public void testTailRec() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/tailRec.kt");
doTest(fileName);
}
@TestMetadata("unescapedAnnotation.kt")
public void testUnescapedAnnotation() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/unescapedAnnotation.kt");
doTest(fileName);
}
@TestMetadata("idea/testData/quickfix/migration/conflictingExtension")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -4112,6 +4166,27 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
}
@TestMetadata("idea/testData/quickfix/migration/decapitalizedAnnotation")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class DecapitalizedAnnotation extends AbstractQuickFixTest {
public void testAllFilesPresentInDecapitalizedAnnotation() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/migration/decapitalizedAnnotation"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("annotationPosition.kt")
public void testAnnotationPosition() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/decapitalizedAnnotation/annotationPosition.kt");
doTest(fileName);
}
@TestMetadata("valueParameter.kt")
public void testValueParameter() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/decapitalizedAnnotation/valueParameter.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/quickfix/migration/lambdaSyntax")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)