backing field migration fixes

This commit is contained in:
Dmitry Jemerov
2015-09-23 17:29:42 +02:00
parent 3d65b6fec2
commit dadef12723
10 changed files with 130 additions and 1 deletions
@@ -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 {
@@ -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<JetNameReferenceExpression>(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<JetNameReferenceExpression>(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
}
}
}
@@ -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)
}
}
+6
View File
@@ -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 }
}
+6
View File
@@ -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 }
}
@@ -0,0 +1,7 @@
// "Migrate backing field syntax" "true"
class Foo {
var a: Int = 0
get() = 0
set(v) { $<caret>a = v }
}
@@ -0,0 +1,7 @@
// "Migrate backing field syntax" "true"
class Foo {
var a: Int = 0
get() = 0
set(v) { field = v }
}
@@ -0,0 +1,7 @@
// "Replace with property access" "true"
class A {
var foo: Int = 0
fun bar() = $f<caret>oo
}
@@ -0,0 +1,7 @@
// "Replace with property access" "true"
class A {
var foo: Int = 0
fun bar() = foo
}
@@ -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)