From 04f54bee88878d5b7ed827011fd674f12090ea91 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Tue, 14 Jun 2016 21:01:25 +0300 Subject: [PATCH] Rename: Process property accesor usages (Java) in comments and string literals #KT-9435 Fixed (cherry picked from commit 911afee) --- ChangeLog.md | 1 + .../kotlin/asJava/FakeFileForLightClass.kt | 2 +- .../KtLightClassForExplicitDeclaration.kt | 2 ++ .../rename/RenameKotlinPropertyProcessor.kt | 14 +++++++++++ .../after/test/Usage.java | 24 +++++++++++++++++++ .../after/test/Used.kt | 5 ++++ .../before/test/Usage.java | 24 +++++++++++++++++++ .../before/test/Used.kt | 5 ++++ .../nonCodePropertyAccessorUsages.test | 6 +++++ .../rename/RenameTestGenerated.java | 6 +++++ 10 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 idea/testData/refactoring/rename/nonCodePropertyAccessorUsages/after/test/Usage.java create mode 100644 idea/testData/refactoring/rename/nonCodePropertyAccessorUsages/after/test/Used.kt create mode 100644 idea/testData/refactoring/rename/nonCodePropertyAccessorUsages/before/test/Usage.java create mode 100644 idea/testData/refactoring/rename/nonCodePropertyAccessorUsages/before/test/Used.kt create mode 100644 idea/testData/refactoring/rename/nonCodePropertyAccessorUsages/nonCodePropertyAccessorUsages.test diff --git a/ChangeLog.md b/ChangeLog.md index 7337788e038..619b82da723 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -237,6 +237,7 @@ - [`KT-9156`](https://youtrack.jetbrains.com/issue/KT-9156) Quote non-identifier names in Kotlin references - [`KT-9157`](https://youtrack.jetbrains.com/issue/KT-9157) Fixed in-place rename of Kotlin expression referring Java declaration - [`KT-9241`](https://youtrack.jetbrains.com/issue/KT-9241) Do not replace Java references to synthetic component functions when renaming constructor parameter +- [`KT-9435`](https://youtrack.jetbrains.com/issue/KT-9435) Process property accesor usages (Java) in comments and string literals - [`KT-9444`](https://youtrack.jetbrains.com/issue/KT-9444) Rename dialog: Allow typing any identifier without backquotes - [`KT-9446`](https://youtrack.jetbrains.com/issue/KT-9446) Warn about calls with default arguments if function to be renamed inherits default values from some base function which is excluded from rename - [`KT-10713`](https://youtrack.jetbrains.com/issue/KT-10713) Skip read-only declarations when renaming parameters diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/FakeFileForLightClass.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/FakeFileForLightClass.kt index 9ef0c014b51..2cd483e5f65 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/FakeFileForLightClass.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/FakeFileForLightClass.kt @@ -26,7 +26,7 @@ import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.psi.KtFile open class FakeFileForLightClass( - private val ktFile: KtFile, + protected val ktFile: KtFile, private val lightClass: () -> KtLightClass, private val stub: () -> PsiClassHolderFileStub<*>, private val packageFqName: FqName = ktFile.packageFqName diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/KtLightClassForExplicitDeclaration.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/KtLightClassForExplicitDeclaration.kt index 45b85ce5ad7..262d6644342 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/KtLightClassForExplicitDeclaration.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/KtLightClassForExplicitDeclaration.kt @@ -185,6 +185,8 @@ open class KtLightClassForExplicitDeclaration( { if (classOrObject.isTopLevel()) this else create(getOutermostClassOrObject(classOrObject))!! }, { getJavaFileStub() } ) { + override fun findReferenceAt(offset: Int) = ktFile.findReferenceAt(offset) + override fun processDeclarations( processor: PsiScopeProcessor, state: ResolveState, 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 8d20822c2a4..59863b672b8 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPropertyProcessor.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPropertyProcessor.kt @@ -245,7 +245,21 @@ class RenameKotlinPropertyProcessor : RenameKotlinPsiProcessor() { val newPropertyName = if (newName != null && element is KtLightMethod) propertyNameByAccessor(newName, element) else newName + val (getterJvmName, setterJvmName) = getJvmNames(namedUnwrappedElement) + for (propertyMethod in propertyMethods) { + if (element is KtDeclaration && newPropertyName != null) { + val wrapper = object : PsiNamedElement by propertyMethod { + override fun setName(name: String) = this + override fun copy() = this + } + when { + JvmAbi.isGetterName(propertyMethod.name) && getterJvmName == null -> + allRenames[wrapper] = JvmAbi.getterName(newPropertyName) + JvmAbi.isSetterName(propertyMethod.name) && setterJvmName == null -> + allRenames[wrapper] = JvmAbi.setterName(newPropertyName) + } + } addRenameElements(propertyMethod, (element as PsiNamedElement).name, newPropertyName, allRenames, scope) } } diff --git a/idea/testData/refactoring/rename/nonCodePropertyAccessorUsages/after/test/Usage.java b/idea/testData/refactoring/rename/nonCodePropertyAccessorUsages/after/test/Usage.java new file mode 100644 index 00000000000..0ce921e13ea --- /dev/null +++ b/idea/testData/refactoring/rename/nonCodePropertyAccessorUsages/after/test/Usage.java @@ -0,0 +1,24 @@ +package test; + +public class CommentUsage { + public void context() { + Used.usedVar2 = 0; + Used.setUsedVar2(0); + Used.getUsedVar2(); + Used.INSTANCE.usedVar2 = 0; + Used.INSTANCE.setUsedVar2(0); + Used.INSTANCE.getUsedVar2(); + // Used.usedVar2 = 0; + // Used.setUsedVar2(0); + // Used.getUsedVar2(); + // Used.INSTANCE.usedVar2 = 0; + // Used.INSTANCE.setUsedVar2(0); + // Used.INSTANCE.getUsedVar2(); + String v = "Used.usedVar2" + + "Used.setUsedVar2(0)" + + "Used.getUsedVar2()" + + "Used.INSTANCE.usedVar2" + + "Used.INSTANCE.setUsedVar2(0)" + + "Used.INSTANCE.getUsedVar2()"; + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/nonCodePropertyAccessorUsages/after/test/Used.kt b/idea/testData/refactoring/rename/nonCodePropertyAccessorUsages/after/test/Used.kt new file mode 100644 index 00000000000..10701b207ec --- /dev/null +++ b/idea/testData/refactoring/rename/nonCodePropertyAccessorUsages/after/test/Used.kt @@ -0,0 +1,5 @@ +package test + +object Used { + @JvmStatic var usedVar2 = 0 +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/nonCodePropertyAccessorUsages/before/test/Usage.java b/idea/testData/refactoring/rename/nonCodePropertyAccessorUsages/before/test/Usage.java new file mode 100644 index 00000000000..db889517415 --- /dev/null +++ b/idea/testData/refactoring/rename/nonCodePropertyAccessorUsages/before/test/Usage.java @@ -0,0 +1,24 @@ +package test; + +public class CommentUsage { + public void context() { + Used.usedVar = 0; + Used.setUsedVar(0); + Used.getUsedVar(); + Used.INSTANCE.usedVar = 0; + Used.INSTANCE.setUsedVar(0); + Used.INSTANCE.getUsedVar(); + // Used.usedVar = 0; + // Used.setUsedVar(0); + // Used.getUsedVar(); + // Used.INSTANCE.usedVar = 0; + // Used.INSTANCE.setUsedVar(0); + // Used.INSTANCE.getUsedVar(); + String v = "Used.usedVar" + + "Used.setUsedVar(0)" + + "Used.getUsedVar()" + + "Used.INSTANCE.usedVar" + + "Used.INSTANCE.setUsedVar(0)" + + "Used.INSTANCE.getUsedVar()"; + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/nonCodePropertyAccessorUsages/before/test/Used.kt b/idea/testData/refactoring/rename/nonCodePropertyAccessorUsages/before/test/Used.kt new file mode 100644 index 00000000000..71246a389ab --- /dev/null +++ b/idea/testData/refactoring/rename/nonCodePropertyAccessorUsages/before/test/Used.kt @@ -0,0 +1,5 @@ +package test + +object Used { + @JvmStatic var /*rename*/usedVar = 0 +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/nonCodePropertyAccessorUsages/nonCodePropertyAccessorUsages.test b/idea/testData/refactoring/rename/nonCodePropertyAccessorUsages/nonCodePropertyAccessorUsages.test new file mode 100644 index 00000000000..74fc9d28858 --- /dev/null +++ b/idea/testData/refactoring/rename/nonCodePropertyAccessorUsages/nonCodePropertyAccessorUsages.test @@ -0,0 +1,6 @@ +{ + "type": "MARKED_ELEMENT", + "mainFile": "test/Used.kt", + "newName": "usedVar2", + "withRuntime": "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 28b10241a3c..29d7703a2cc 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java @@ -203,6 +203,12 @@ public class RenameTestGenerated extends AbstractRenameTest { doTest(fileName); } + @TestMetadata("nonCodePropertyAccessorUsages/nonCodePropertyAccessorUsages.test") + public void testNonCodePropertyAccessorUsages_NonCodePropertyAccessorUsages() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/nonCodePropertyAccessorUsages/nonCodePropertyAccessorUsages.test"); + doTest(fileName); + } + @TestMetadata("parameterRedeclaration/parameterRedeclaration.test") public void testParameterRedeclaration_ParameterRedeclaration() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/parameterRedeclaration/parameterRedeclaration.test");