diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/KotlinAwareJavaGetterRenameProcessor.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/KotlinAwareJavaGetterRenameProcessor.kt index 90c41594f96..b20f4e0a511 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/KotlinAwareJavaGetterRenameProcessor.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/KotlinAwareJavaGetterRenameProcessor.kt @@ -9,6 +9,7 @@ import com.intellij.psi.PsiElement import com.intellij.psi.PsiMethod import com.intellij.psi.PsiReference import com.intellij.psi.PsiType +import com.intellij.psi.search.SearchScope import com.intellij.refactoring.rename.RenameJavaMethodProcessor import org.jetbrains.kotlin.asJava.elements.KtLightMethod import org.jetbrains.kotlin.idea.references.SyntheticPropertyAccessorReference @@ -20,19 +21,28 @@ import org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor import org.jetbrains.kotlin.utils.ifEmpty class KotlinAwareJavaGetterRenameProcessor : RenameJavaMethodProcessor() { - override fun canProcessElement(element: PsiElement) = element is PsiMethod && element !is KtLightMethod && JvmAbi.isGetterName(element.name) + override fun canProcessElement(element: PsiElement) = + element is PsiMethod && element !is KtLightMethod && JvmAbi.isGetterName(element.name) - override fun findReferences(element: PsiElement): MutableCollection { - val getterReferences = super.findReferences(element) + override fun findReferences( + element: PsiElement, + searchScope: SearchScope, + searchInCommentsAndStrings: Boolean + ): Collection { + val getterReferences = super.findReferences(element, searchScope, searchInCommentsAndStrings) val getter = element as? PsiMethod ?: return getterReferences - val propertyName = SyntheticJavaPropertyDescriptor.propertyNameByGetMethodName(Name.identifier(getter.name)) ?: return getterReferences + val propertyName = SyntheticJavaPropertyDescriptor.propertyNameByGetMethodName(Name.identifier(getter.name)) + ?: return getterReferences val setterName = JvmAbi.setterName(propertyName.asString()) val containingClass = getter.containingClass ?: return getterReferences KotlinFUSLogger.log(FUSEventGroups.Refactoring, this::class.java.name) val setterReferences = containingClass .findMethodsByName(setterName, true) .filter { it.parameters.size == 1 && it.returnType == PsiType.VOID } - .flatMap { super.findReferences(it).filterIsInstance() } + .flatMap { + super.findReferences(it, searchScope, searchInCommentsAndStrings) + .filterIsInstance() + } .ifEmpty { return getterReferences } return ArrayList(getterReferences.size + setterReferences.size).apply { addAll(getterReferences) diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/KotlinAwareJavaGetterRenameProcessor.kt.183 b/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/KotlinAwareJavaGetterRenameProcessor.kt.183 new file mode 100644 index 00000000000..90c41594f96 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/KotlinAwareJavaGetterRenameProcessor.kt.183 @@ -0,0 +1,42 @@ +/* + * Copyright 2000-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.idea.refactoring.rename + +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiMethod +import com.intellij.psi.PsiReference +import com.intellij.psi.PsiType +import com.intellij.refactoring.rename.RenameJavaMethodProcessor +import org.jetbrains.kotlin.asJava.elements.KtLightMethod +import org.jetbrains.kotlin.idea.references.SyntheticPropertyAccessorReference +import org.jetbrains.kotlin.idea.statistics.FUSEventGroups +import org.jetbrains.kotlin.idea.statistics.KotlinFUSLogger +import org.jetbrains.kotlin.load.java.JvmAbi +import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor +import org.jetbrains.kotlin.utils.ifEmpty + +class KotlinAwareJavaGetterRenameProcessor : RenameJavaMethodProcessor() { + override fun canProcessElement(element: PsiElement) = element is PsiMethod && element !is KtLightMethod && JvmAbi.isGetterName(element.name) + + override fun findReferences(element: PsiElement): MutableCollection { + val getterReferences = super.findReferences(element) + val getter = element as? PsiMethod ?: return getterReferences + val propertyName = SyntheticJavaPropertyDescriptor.propertyNameByGetMethodName(Name.identifier(getter.name)) ?: return getterReferences + val setterName = JvmAbi.setterName(propertyName.asString()) + val containingClass = getter.containingClass ?: return getterReferences + KotlinFUSLogger.log(FUSEventGroups.Refactoring, this::class.java.name) + val setterReferences = containingClass + .findMethodsByName(setterName, true) + .filter { it.parameters.size == 1 && it.returnType == PsiType.VOID } + .flatMap { super.findReferences(it).filterIsInstance() } + .ifEmpty { return getterReferences } + return ArrayList(getterReferences.size + setterReferences.size).apply { + addAll(getterReferences) + setterReferences.mapTo(this) { SyntheticPropertyAccessorReference.Getter(it.expression) } + } + } +} \ No newline at end of file