From fbd6edce926bd5661b24d6d10ec44bae60689bd1 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Mon, 15 Aug 2016 20:13:01 +0300 Subject: [PATCH] Light Classes: Support property accessors with non-conventional names #KT-13032 Fixed --- ChangeLog.md | 1 + .../org/jetbrains/kotlin/asJava/LightClassUtil.kt | 4 +++- .../kotlin/asJava/elements/KtLightMethod.kt | 13 +++++++++++++ .../KtLightModifierListWithExplicitModifiers.kt | 5 ++--- .../org/jetbrains/kotlin/asJava/lightClassUtils.kt | 2 +- .../after/test/JavaClient.java | 8 ++++++++ .../after/test/test.kt | 12 ++++++++++++ .../before/test/JavaClient.java | 8 ++++++++ .../before/test/test.kt | 12 ++++++++++++ ...rtyWithCustomGetterSetterJvmNameByGetterRef.test | 7 +++++++ .../after/test/JavaClient.java | 8 ++++++++ .../after/test/test.kt | 12 ++++++++++++ .../before/test/JavaClient.java | 8 ++++++++ .../before/test/test.kt | 12 ++++++++++++ ...rtyWithCustomGetterSetterJvmNameBySetterRef.test | 7 +++++++ .../refactoring/rename/RenameTestGenerated.java | 12 ++++++++++++ 16 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/after/test/JavaClient.java create mode 100644 idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/after/test/test.kt create mode 100644 idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/before/test/JavaClient.java create mode 100644 idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/before/test/test.kt create mode 100644 idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef.test create mode 100644 idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/after/test/JavaClient.java create mode 100644 idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/after/test/test.kt create mode 100644 idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/before/test/JavaClient.java create mode 100644 idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/before/test/test.kt create mode 100644 idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef.test diff --git a/ChangeLog.md b/ChangeLog.md index a6c9f00fb95..7cd79c90c3a 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -270,6 +270,7 @@ Using 'this' as function argument in constructor of non-final class - [`KT-13253`](https://youtrack.jetbrains.com/issue/KT-13253) Rename: Report conflicts for constructor parameters - [`KT-12971`](https://youtrack.jetbrains.com/issue/KT-12971) Push Down: Do not specifiy visibility on generated overriding members - [`KT-13124`](https://youtrack.jetbrains.com/issue/KT-13124) Pull Up: Skip super members without explicit declarations +- [`KT-13032`](https://youtrack.jetbrains.com/issue/KT-13032) Rename: Support accessors with non-conventional names ##### New features diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/LightClassUtil.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/LightClassUtil.kt index e04d5b2b224..813a4a8907b 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/LightClassUtil.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/LightClassUtil.kt @@ -198,7 +198,9 @@ object LightClassUtil { val additionalAccessors = arrayListOf() for (wrapper in getPsiMethodWrappers(ktDeclaration, true)) { - if (JvmAbi.isSetterName(wrapper.name)) { + if (wrapper !is KtLightMethod) continue + + if (wrapper.isSetter) { if (setterWrapper == null || setterWrapper === specialSetter) { setterWrapper = wrapper } diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightMethod.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightMethod.kt index 41e6aaafa87..d66c740f93b 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightMethod.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightMethod.kt @@ -232,3 +232,16 @@ fun KtLightMethod.isTraitFakeOverride(): Boolean { // Method was generated from declaration in some other trait return (parentOfMethodOrigin != null && thisClassDeclaration !== parentOfMethodOrigin && KtPsiUtil.isTrait(parentOfMethodOrigin)) } + +fun KtLightMethod.isAccessor(getter: Boolean): Boolean { + val origin = kotlinOrigin as? KtCallableDeclaration ?: return false + if (origin !is KtProperty && origin !is KtParameter) return false + val expectedParametersCount = (if (getter) 0 else 1) + (if (origin.receiverTypeReference != null) 1 else 0) + return parameterList.parametersCount == expectedParametersCount +} + +val KtLightMethod.isGetter: Boolean + get() = isAccessor(true) + +val KtLightMethod.isSetter: Boolean + get() = isAccessor(false) \ No newline at end of file diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightModifierListWithExplicitModifiers.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightModifierListWithExplicitModifiers.kt index 331aaa2503f..e4b74162880 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightModifierListWithExplicitModifiers.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightModifierListWithExplicitModifiers.kt @@ -28,7 +28,6 @@ import org.jetbrains.annotations.NonNls import org.jetbrains.kotlin.asJava.LightClassGenerationSupport import org.jetbrains.kotlin.descriptors.PropertyDescriptor import org.jetbrains.kotlin.idea.KotlinLanguage -import org.jetbrains.kotlin.load.java.JvmAbi import org.jetbrains.kotlin.psi.KtAnnotationEntry import org.jetbrains.kotlin.psi.KtDeclaration import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe @@ -100,8 +99,8 @@ internal fun computeAnnotations(lightElement: PsiModifierList, val descriptor = declaration?.let { LightClassGenerationSupport.getInstance(lightElement.project).resolveToDescriptor(it) } val annotatedDescriptor = when { descriptor !is PropertyDescriptor || lightOwner !is KtLightMethod -> descriptor - JvmAbi.isGetterName(lightOwner.name) -> descriptor.getter - JvmAbi.isSetterName(lightOwner.name) -> descriptor.setter + lightOwner.isGetter -> descriptor.getter + lightOwner.isSetter -> descriptor.setter else -> descriptor } val ktAnnotations = annotatedDescriptor?.annotations?.getAllAnnotations() ?: emptyList() diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/lightClassUtils.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/lightClassUtils.kt index e2b7c2cdb02..ea92df91a9a 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/lightClassUtils.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/lightClassUtils.kt @@ -171,7 +171,7 @@ fun propertyNameByAccessor(name: String, accessor: KtLightMethod): String? { return when { JvmAbi.isGetterName(name) -> propertyNameByGetMethodName(methodName) JvmAbi.isSetterName(name) -> propertyNameBySetMethodName(methodName, propertyName.startsWith("is")) - else -> null + else -> methodName }?.asString() } diff --git a/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/after/test/JavaClient.java b/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/after/test/JavaClient.java new file mode 100644 index 00000000000..a304102be26 --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/after/test/JavaClient.java @@ -0,0 +1,8 @@ +package test; + +class Test { + { + new A().fooNew(); + new A().bar(1); + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/after/test/test.kt b/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/after/test/test.kt new file mode 100644 index 00000000000..95d5405bb11 --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/after/test/test.kt @@ -0,0 +1,12 @@ +package test + +class A { + @get:JvmName("fooNew") + @set:JvmName("bar") + var first = 1 +} + +fun test() { + A().first + A().first = 1 +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/before/test/JavaClient.java b/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/before/test/JavaClient.java new file mode 100644 index 00000000000..5038072e60f --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/before/test/JavaClient.java @@ -0,0 +1,8 @@ +package test; + +class Test { + { + new A()./*rename*/foo(); + new A().bar(1); + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/before/test/test.kt b/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/before/test/test.kt new file mode 100644 index 00000000000..3a9e4f2839c --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/before/test/test.kt @@ -0,0 +1,12 @@ +package test + +class A { + @get:JvmName("foo") + @set:JvmName("bar") + var first = 1 +} + +fun test() { + A().first + A().first = 1 +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef.test b/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef.test new file mode 100644 index 00000000000..500f1f22430 --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef.test @@ -0,0 +1,7 @@ +{ + "type": "MARKED_ELEMENT", + "mainFile": "test/JavaClient.java", + "newName": "fooNew", + "withRuntime": "true", + "byRef": "true" +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/after/test/JavaClient.java b/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/after/test/JavaClient.java new file mode 100644 index 00000000000..6a039f86777 --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/after/test/JavaClient.java @@ -0,0 +1,8 @@ +package test; + +class Test { + { + new A().foo(); + new A().barNew(1); + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/after/test/test.kt b/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/after/test/test.kt new file mode 100644 index 00000000000..902c905e5e2 --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/after/test/test.kt @@ -0,0 +1,12 @@ +package test + +class A { + @get:JvmName("foo") + @set:JvmName("barNew") + var first = 1 +} + +fun test() { + A().first + A().first = 1 +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/before/test/JavaClient.java b/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/before/test/JavaClient.java new file mode 100644 index 00000000000..653a002736a --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/before/test/JavaClient.java @@ -0,0 +1,8 @@ +package test; + +class Test { + { + new A().foo(); + new A()./*rename*/bar(1); + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/before/test/test.kt b/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/before/test/test.kt new file mode 100644 index 00000000000..3a9e4f2839c --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/before/test/test.kt @@ -0,0 +1,12 @@ +package test + +class A { + @get:JvmName("foo") + @set:JvmName("bar") + var first = 1 +} + +fun test() { + A().first + A().first = 1 +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef.test b/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef.test new file mode 100644 index 00000000000..36d3c54e490 --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef.test @@ -0,0 +1,7 @@ +{ + "type": "MARKED_ELEMENT", + "mainFile": "test/JavaClient.java", + "newName": "barNew", + "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 ddc4ed7a0d4..4540a087fd6 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java @@ -785,6 +785,18 @@ public class RenameTestGenerated extends AbstractRenameTest { doTest(fileName); } + @TestMetadata("renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef.test") + public void testRenameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef_RenameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef/renameKotlinPropertyWithCustomGetterSetterJvmNameByGetterRef.test"); + doTest(fileName); + } + + @TestMetadata("renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef.test") + public void testRenameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef_RenameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef/renameKotlinPropertyWithCustomGetterSetterJvmNameBySetterRef.test"); + doTest(fileName); + } + @TestMetadata("renameKotlinPropertyWithGetterJvmName/renameKotlinPropertyWithGetterJvmName.test") public void testRenameKotlinPropertyWithGetterJvmName_RenameKotlinPropertyWithGetterJvmName() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/renameKotlinPropertyWithGetterJvmName/renameKotlinPropertyWithGetterJvmName.test");