update backing field references when renaming Kotlin property

#KT-7905 Fixed
This commit is contained in:
Dmitry Jemerov
2015-08-24 17:55:45 +02:00
parent 96f2257b23
commit 18223dfaaa
15 changed files with 133 additions and 3 deletions
@@ -182,4 +182,9 @@ public abstract class KotlinLightField<T extends JetDeclaration, D extends PsiFi
}
return super.isEquivalentTo(another);
}
@Override
public boolean isWritable() {
return getOrigin().isWritable();
}
}
@@ -174,6 +174,34 @@ public class LightClassUtil {
return extractPropertyAccessors(property, getterWrapper, setterWrapper);
}
@Nullable
private static PsiField getLightClassBackingField(JetDeclaration declaration) {
PsiClass psiClass = getWrappingClass(declaration);
if (psiClass == null) {
return null;
}
if (psiClass instanceof KotlinLightClass) {
JetClassOrObject origin = ((KotlinLightClass) psiClass).getOrigin();
if (origin instanceof JetObjectDeclaration && ((JetObjectDeclaration) origin).isCompanion()) {
JetClass containingClass = PsiTreeUtil.getParentOfType(origin, JetClass.class);
if (containingClass != null) {
PsiClass containingLightClass = getPsiClass(containingClass);
if (containingLightClass != null) {
psiClass = containingLightClass;
}
}
}
}
for (PsiField field : psiClass.getFields()) {
if (field instanceof KotlinLightField && ((KotlinLightField) field).getOrigin() == declaration) {
return field;
}
}
return null;
}
@NotNull
public static PropertyAccessorsPsiMethods getLightClassPropertyMethods(@NotNull JetParameter parameter) {
return extractPropertyAccessors(parameter, null, null);
@@ -315,9 +343,11 @@ public class LightClassUtil {
getterWrapper = wrapper;
}
}
}
return new PropertyAccessorsPsiMethods(getterWrapper, setterWrapper);
PsiField backingField = getLightClassBackingField(jetDeclaration);
return new PropertyAccessorsPsiMethods(getterWrapper, setterWrapper, backingField);
}
@NotNull
@@ -341,9 +371,10 @@ public class LightClassUtil {
public static class PropertyAccessorsPsiMethods implements Iterable<PsiMethod> {
private final PsiMethod getter;
private final PsiMethod setter;
private final PsiField backingField;
private final Collection<PsiMethod> accessors = new ArrayList<PsiMethod>(2);
PropertyAccessorsPsiMethods(@Nullable PsiMethod getter, @Nullable PsiMethod setter) {
PropertyAccessorsPsiMethods(@Nullable PsiMethod getter, @Nullable PsiMethod setter, @Nullable PsiField backingField) {
this.getter = getter;
if (getter != null) {
accessors.add(getter);
@@ -353,6 +384,8 @@ public class LightClassUtil {
if (setter != null) {
accessors.add(setter);
}
this.backingField = backingField;
}
@Nullable
@@ -365,6 +398,11 @@ public class LightClassUtil {
return setter;
}
@Nullable
public PsiField getBackingField() {
return backingField;
}
@NotNull
@Override
public Iterator<PsiMethod> iterator() {
@@ -171,6 +171,7 @@ public class KotlinReferencesSearcher : QueryExecutorBase<PsiReference, Referenc
val propertyMethods = runReadAction { LightClassUtil.getLightClassPropertyMethods(element) }
searchNamedElement(queryParameters, propertyMethods.getGetter())
searchNamedElement(queryParameters, propertyMethods.getSetter())
searchNamedElement(queryParameters, propertyMethods.getBackingField())
}
is KotlinLightMethod -> {
val declaration = element.getOrigin()
@@ -44,7 +44,9 @@ import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.OverrideResolver
public class RenameKotlinPropertyProcessor : RenamePsiElementProcessor() {
override fun canProcessElement(element: PsiElement): Boolean = element.namedUnwrappedElement is JetProperty
override fun canProcessElement(element: PsiElement): Boolean {
return element.namedUnwrappedElement is JetProperty
}
/* Can't properly update getters and setters in Java */
override fun isInplaceRenameSupported() = false
@@ -0,0 +1,7 @@
package testing.rename
class Foo {
companion object {
val SECOND = 111
}
}
@@ -0,0 +1,9 @@
package testing;
import testing.rename.*;
class JavaClient {
public int foo() {
return Foo.SECOND;
}
}
@@ -0,0 +1,7 @@
package testing.rename
class Foo {
companion object {
val FIRST = 111
}
}
@@ -0,0 +1,9 @@
package testing;
import testing.rename.*;
class JavaClient {
public int foo() {
return Foo.FIRST;
}
}
@@ -0,0 +1,6 @@
{
"type": "KOTLIN_PROPERTY",
"classId": "testing/rename/Foo.Companion",
"oldName": "FIRST",
"newName": "SECOND"
}
@@ -0,0 +1,5 @@
package testing.rename
object Foo {
val SECOND = 111
}
@@ -0,0 +1,9 @@
package testing;
import testing.rename.*;
class JavaClient {
public int foo() {
return Foo.SECOND;
}
}
@@ -0,0 +1,5 @@
package testing.rename
object Foo {
val FIRST = 111
}
@@ -0,0 +1,9 @@
package testing;
import testing.rename.*;
class JavaClient {
public int foo() {
return Foo.FIRST;
}
}
@@ -0,0 +1,6 @@
{
"type": "KOTLIN_PROPERTY",
"classId": "testing/rename/Foo",
"oldName": "FIRST",
"newName": "SECOND"
}
@@ -335,6 +335,18 @@ public class RenameTestGenerated extends AbstractRenameTest {
doTest(fileName);
}
@TestMetadata("renameKotlinValPropertyInCompanionObject/renameKotlinValPropertyInCompanionObject.test")
public void testRenameKotlinValPropertyInCompanionObject_RenameKotlinValPropertyInCompanionObject() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/rename/renameKotlinValPropertyInCompanionObject/renameKotlinValPropertyInCompanionObject.test");
doTest(fileName);
}
@TestMetadata("renameKotlinValPropertyInObject/renameKotlinValPropertyInObject.test")
public void testRenameKotlinValPropertyInObject_RenameKotlinValPropertyInObject() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/rename/renameKotlinValPropertyInObject/renameKotlinValPropertyInObject.test");
doTest(fileName);
}
@TestMetadata("renameKotlinVarProperty/renameAsJavaGetter.test")
public void testRenameKotlinVarProperty_RenameAsJavaGetter() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/rename/renameKotlinVarProperty/renameAsJavaGetter.test");