diff --git a/ChangeLog.md b/ChangeLog.md index 44c41c87c26..42d46c0eb3c 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -227,6 +227,7 @@ - [`KT-7851`](https://youtrack.jetbrains.com/issue/KT-7851) Respect naming conventions in automatic variable rename - [`KT-8044`](https://youtrack.jetbrains.com/issue/KT-8044), [`KT-9432`](https://youtrack.jetbrains.com/issue/KT-9432) Support @JvmName annotation in rename refactoring - [`KT-8512`](https://youtrack.jetbrains.com/issue/KT-8512) Support "Rename tests" options in Rename dialog +- [`KT-12759`](https://youtrack.jetbrains.com/issue/KT-12759) Suggest renaming both property accessors with matching @JvmName when renaming one of them from Java ###### Issues fixed - [`KT-4791`](https://youtrack.jetbrains.com/issue/KT-4791) Rename overridden property and all its accessors on attempt to rename overriding accessor in Java code diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPropertyProcessor.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPropertyProcessor.kt index e43e5093b59..e148786ca66 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPropertyProcessor.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPropertyProcessor.kt @@ -263,6 +263,22 @@ class RenameKotlinPropertyProcessor : RenameKotlinPsiProcessor() { val (getterJvmName, setterJvmName) = getJvmNames(namedUnwrappedElement) + val getter = propertyMethods.getter as? KtLightMethod + val setter = propertyMethods.setter as? KtLightMethod + if (newPropertyName != null + && getter != null && setter != null + && (element == getter || element == setter) + && propertyNameByAccessor(getter.name, getter) == propertyNameByAccessor(setter.name, setter)) { + val accessorToRename = if (element == getter) setter else getter + val newAccessorName = if (element == getter) JvmAbi.setterName(newPropertyName) else JvmAbi.getterName(newPropertyName) + if (ApplicationManager.getApplication().isUnitTestMode + || Messages.showYesNoDialog("Do you want to rename ${accessorToRename.name}() as well?", + "Rename", + Messages.getQuestionIcon()) == Messages.YES) { + allRenames[accessorToRename] = newAccessorName + } + } + for (propertyMethod in propertyMethods) { if (element is KtDeclaration && newPropertyName != null) { val wrapper = object : PsiNamedElement by propertyMethod { diff --git a/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef/after/test/JavaClient.java b/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef/after/test/JavaClient.java new file mode 100644 index 00000000000..d89e97db771 --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef/after/test/JavaClient.java @@ -0,0 +1,8 @@ +package test; + +class Test { + { + new A().getFooNew(); + new A().setFooNew(1); + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef/after/test/test.kt b/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef/after/test/test.kt new file mode 100644 index 00000000000..855cedaf6f1 --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef/after/test/test.kt @@ -0,0 +1,12 @@ +package test + +class A { + @get:JvmName("getFooNew") + @set:JvmName("setFooNew") + var first = 1 +} + +fun test() { + A().first + A().first = 1 +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef/before/test/JavaClient.java b/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef/before/test/JavaClient.java new file mode 100644 index 00000000000..eb96d467205 --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef/before/test/JavaClient.java @@ -0,0 +1,8 @@ +package test; + +class Test { + { + new A()./*rename*/getFoo(); + new A().setFoo(1); + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef/before/test/test.kt b/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef/before/test/test.kt new file mode 100644 index 00000000000..90cd0a71eea --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef/before/test/test.kt @@ -0,0 +1,12 @@ +package test + +class A { + @get:JvmName("getFoo") + @set:JvmName("setFoo") + var first = 1 +} + +fun test() { + A().first + A().first = 1 +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef/renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef.test b/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef/renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef.test new file mode 100644 index 00000000000..1aa1fd8e59e --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef/renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef.test @@ -0,0 +1,7 @@ +{ + "type": "MARKED_ELEMENT", + "mainFile": "test/JavaClient.java", + "newName": "getFooNew", + "withRuntime": "true", + "byRef": "true" +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef/after/test/JavaClient.java b/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef/after/test/JavaClient.java new file mode 100644 index 00000000000..d89e97db771 --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef/after/test/JavaClient.java @@ -0,0 +1,8 @@ +package test; + +class Test { + { + new A().getFooNew(); + new A().setFooNew(1); + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef/after/test/test.kt b/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef/after/test/test.kt new file mode 100644 index 00000000000..855cedaf6f1 --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef/after/test/test.kt @@ -0,0 +1,12 @@ +package test + +class A { + @get:JvmName("getFooNew") + @set:JvmName("setFooNew") + var first = 1 +} + +fun test() { + A().first + A().first = 1 +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef/before/test/JavaClient.java b/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef/before/test/JavaClient.java new file mode 100644 index 00000000000..8fc1df29a28 --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef/before/test/JavaClient.java @@ -0,0 +1,8 @@ +package test; + +class Test { + { + new A().getFoo(); + new A()./*rename*/setFoo(1); + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef/before/test/test.kt b/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef/before/test/test.kt new file mode 100644 index 00000000000..90cd0a71eea --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef/before/test/test.kt @@ -0,0 +1,12 @@ +package test + +class A { + @get:JvmName("getFoo") + @set:JvmName("setFoo") + var first = 1 +} + +fun test() { + A().first + A().first = 1 +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef/renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef.test b/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef/renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef.test new file mode 100644 index 00000000000..9cce77589a5 --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef/renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef.test @@ -0,0 +1,7 @@ +{ + "type": "MARKED_ELEMENT", + "mainFile": "test/JavaClient.java", + "newName": "setFooNew", + "withRuntime": "true", + "byRef": "true" +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java index 5548f5cd46d..4251f21609a 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java @@ -767,6 +767,18 @@ public class RenameTestGenerated extends AbstractRenameTest { doTest(fileName); } + @TestMetadata("renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef/renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef.test") + public void testRenameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef_RenameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef/renameKotlinPropertyWithMatchingGetterSetterJvmNameByGetterRef.test"); + doTest(fileName); + } + + @TestMetadata("renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef/renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef.test") + public void testRenameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef_RenameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef/renameKotlinPropertyWithMatchingGetterSetterJvmNameBySetterRef.test"); + doTest(fileName); + } + @TestMetadata("renameKotlinPropertyWithSetterJvmName/renameKotlinPropertyWithSetterJvmName.test") public void testRenameKotlinPropertyWithSetterJvmName_RenameKotlinPropertyWithSetterJvmName() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/renameKotlinPropertyWithSetterJvmName/renameKotlinPropertyWithSetterJvmName.test");