From dadef127230feed039ae1e4b36ee9a3ced4f2d77 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Wed, 23 Sep 2015 17:29:42 +0200 Subject: [PATCH] backing field migration fixes --- .../inspections/KotlinCleanupInspection.kt | 3 +- .../quickfix/MigrateBackingFieldSyntaxFix.kt | 64 +++++++++++++++++++ .../kotlin/idea/quickfix/QuickFixRegistrar.kt | 3 + idea/testData/inspections/cleanup/cleanup.kt | 6 ++ .../inspections/cleanup/cleanup.kt.after | 6 ++ .../migration/backingFieldSyntax/accessor.kt | 7 ++ .../backingFieldSyntax/accessor.kt.after | 7 ++ .../migration/backingFieldSyntax/usage.kt | 7 ++ .../backingFieldSyntax/usage.kt.after | 7 ++ .../idea/quickfix/QuickFixTestGenerated.java | 21 ++++++ 10 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 idea/src/org/jetbrains/kotlin/idea/quickfix/MigrateBackingFieldSyntaxFix.kt create mode 100644 idea/testData/quickfix/migration/backingFieldSyntax/accessor.kt create mode 100644 idea/testData/quickfix/migration/backingFieldSyntax/accessor.kt.after create mode 100644 idea/testData/quickfix/migration/backingFieldSyntax/usage.kt create mode 100644 idea/testData/quickfix/migration/backingFieldSyntax/usage.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/KotlinCleanupInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/KotlinCleanupInspection.kt index f2ffc8adaeb..7cf3f445b69 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/KotlinCleanupInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/KotlinCleanupInspection.kt @@ -89,7 +89,8 @@ public class KotlinCleanupInspection(): LocalInspectionTool(), CleanupLocalInspe Errors.USELESS_ELVIS, ErrorsJvm.POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION, Errors.DEPRECATED_SYMBOL_WITH_MESSAGE, - Errors.ACCESS_TO_PRIVATE_TOP_LEVEL_FROM_ANOTHER_FILE + Errors.ACCESS_TO_PRIVATE_TOP_LEVEL_FROM_ANOTHER_FILE, + Errors.BACKING_FIELD_SYNTAX_DEPRECATED ) private fun Diagnostic.isObsoleteLabel(): Boolean { diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/MigrateBackingFieldSyntaxFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/MigrateBackingFieldSyntaxFix.kt new file mode 100644 index 00000000000..94e31a6b0bc --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/MigrateBackingFieldSyntaxFix.kt @@ -0,0 +1,64 @@ +/* + * 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 + +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.psi.JetFile +import org.jetbrains.kotlin.psi.JetNameReferenceExpression +import org.jetbrains.kotlin.psi.JetPsiFactory + +public class MigrateBackingFieldSyntaxFix(expr: JetNameReferenceExpression) + : JetIntentionAction(expr), CleanupFix { + override fun invoke(project: Project, editor: Editor?, file: JetFile) { + val replacement = JetPsiFactory(project).createExpression("field") + element.replace(replacement) + } + + override fun getFamilyName(): String = "Migrate backing field syntax" + override fun getText(): String = getFamilyName() + + companion object : JetSingleIntentionActionFactory() { + override fun createAction(diagnostic: Diagnostic): IntentionAction? { + val element = diagnostic.psiElement as? JetNameReferenceExpression ?: return null + return MigrateBackingFieldSyntaxFix(element) + } + } +} + +public class MigrateBackingFieldUsageFix(expr: JetNameReferenceExpression) + : JetIntentionAction(expr) { + override fun invoke(project: Project, editor: Editor?, file: JetFile) { + val replacement = JetPsiFactory(project).createExpression(element.text.substring(1)) + element.replace(replacement) + } + + override fun getFamilyName(): String = "Replace with property access" + override fun getText(): String = getFamilyName() + + companion object : JetSingleIntentionActionFactory() { + override fun createAction(diagnostic: Diagnostic): IntentionAction? { + val element = diagnostic.psiElement as? JetNameReferenceExpression ?: return null + if (element.text.length() > 1) { + return MigrateBackingFieldUsageFix(element) + } + return null + } + } +} diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index 2c00d0a19d7..2e32f317c54 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -319,5 +319,8 @@ public class QuickFixRegistrar : QuickFixContributor { UPPER_BOUND_VIOLATED.registerFactory(AddGenericUpperBoundFix.Factory) TYPE_INFERENCE_UPPER_BOUND_VIOLATED.registerFactory(AddGenericUpperBoundFix.Factory) + + BACKING_FIELD_SYNTAX_DEPRECATED.registerFactory(MigrateBackingFieldSyntaxFix) + BACKING_FIELD_USAGE_DEPRECATED.registerFactory(MigrateBackingFieldUsageFix) } } diff --git a/idea/testData/inspections/cleanup/cleanup.kt b/idea/testData/inspections/cleanup/cleanup.kt index 0edf7836b6c..e2dd14caf2a 100644 --- a/idea/testData/inspections/cleanup/cleanup.kt +++ b/idea/testData/inspections/cleanup/cleanup.kt @@ -35,3 +35,9 @@ fun unnecessaryCast(x: String) = x as String fun unnecessaryElvis(x: String) = x ?: "" @JavaAnn(1, "abc") class MyClass + +class Foo { + var x: Int = 0 + get = $x + set(value) { $x = value } +} diff --git a/idea/testData/inspections/cleanup/cleanup.kt.after b/idea/testData/inspections/cleanup/cleanup.kt.after index 7d19e715209..11f81d0e5e2 100644 --- a/idea/testData/inspections/cleanup/cleanup.kt.after +++ b/idea/testData/inspections/cleanup/cleanup.kt.after @@ -34,3 +34,9 @@ fun unnecessaryCast(x: String) = x fun unnecessaryElvis(x: String) = x @JavaAnn(1, arg1 = "abc") class MyClass + +class Foo { + var x: Int = 0 + get = field + set(value) { field = value } +} diff --git a/idea/testData/quickfix/migration/backingFieldSyntax/accessor.kt b/idea/testData/quickfix/migration/backingFieldSyntax/accessor.kt new file mode 100644 index 00000000000..7c6a4d14da3 --- /dev/null +++ b/idea/testData/quickfix/migration/backingFieldSyntax/accessor.kt @@ -0,0 +1,7 @@ +// "Migrate backing field syntax" "true" + +class Foo { + var a: Int = 0 + get() = 0 + set(v) { $a = v } +} diff --git a/idea/testData/quickfix/migration/backingFieldSyntax/accessor.kt.after b/idea/testData/quickfix/migration/backingFieldSyntax/accessor.kt.after new file mode 100644 index 00000000000..a505a403297 --- /dev/null +++ b/idea/testData/quickfix/migration/backingFieldSyntax/accessor.kt.after @@ -0,0 +1,7 @@ +// "Migrate backing field syntax" "true" + +class Foo { + var a: Int = 0 + get() = 0 + set(v) { field = v } +} diff --git a/idea/testData/quickfix/migration/backingFieldSyntax/usage.kt b/idea/testData/quickfix/migration/backingFieldSyntax/usage.kt new file mode 100644 index 00000000000..8a7050df798 --- /dev/null +++ b/idea/testData/quickfix/migration/backingFieldSyntax/usage.kt @@ -0,0 +1,7 @@ +// "Replace with property access" "true" + +class A { + var foo: Int = 0 + + fun bar() = $foo +} diff --git a/idea/testData/quickfix/migration/backingFieldSyntax/usage.kt.after b/idea/testData/quickfix/migration/backingFieldSyntax/usage.kt.after new file mode 100644 index 00000000000..6647987db29 --- /dev/null +++ b/idea/testData/quickfix/migration/backingFieldSyntax/usage.kt.after @@ -0,0 +1,7 @@ +// "Replace with property access" "true" + +class A { + var foo: Int = 0 + + fun bar() = foo +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index 42c04278a70..c576996b70f 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -4031,6 +4031,27 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/migration"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); } + @TestMetadata("idea/testData/quickfix/migration/backingFieldSyntax") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class BackingFieldSyntax extends AbstractQuickFixTest { + @TestMetadata("accessor.kt") + public void testAccessor() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/backingFieldSyntax/accessor.kt"); + doTest(fileName); + } + + public void testAllFilesPresentInBackingFieldSyntax() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/migration/backingFieldSyntax"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); + } + + @TestMetadata("usage.kt") + public void testUsage() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/backingFieldSyntax/usage.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/quickfix/migration/conflictingExtension") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)