From e5f0f2489fe629f3c3045f9ae31abd636c806a3a Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Wed, 21 Nov 2018 23:27:52 +0900 Subject: [PATCH] Introduce backing property: fix false positive for const property #KT-28341 Fixed --- .../idea/intentions/IntroduceBackingPropertyIntention.kt | 7 +++---- idea/testData/intentions/introduceBackingProperty/const.kt | 6 ++++++ .../kotlin/idea/intentions/IntentionTestGenerated.java | 5 +++++ 3 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 idea/testData/intentions/introduceBackingProperty/const.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/IntroduceBackingPropertyIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/IntroduceBackingPropertyIntention.kt index 1db3e1273ef..4318b12cde3 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/IntroduceBackingPropertyIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/IntroduceBackingPropertyIntention.kt @@ -40,6 +40,7 @@ class IntroduceBackingPropertyIntention : SelfTargetingIntention(KtP companion object { fun canIntroduceBackingProperty(property: KtProperty): Boolean { val name = property.name ?: return false + if (property.hasModifier(KtTokens.CONST_KEYWORD)) return false if (property.hasJvmFieldAnnotation()) return false val bindingContext = property.getResolutionFacade().analyzeWithAllCompilerChecks(listOf(property)).bindingContext @@ -61,8 +62,7 @@ class IntroduceBackingPropertyIntention : SelfTargetingIntention(KtP val getter = property.getter if (getter == null) { createGetter(property) - } - else { + } else { replaceFieldReferences(getter, property.name!!) } @@ -70,8 +70,7 @@ class IntroduceBackingPropertyIntention : SelfTargetingIntention(KtP val setter = property.setter if (setter == null) { createSetter(property) - } - else { + } else { replaceFieldReferences(setter, property.name!!) } } diff --git a/idea/testData/intentions/introduceBackingProperty/const.kt b/idea/testData/intentions/introduceBackingProperty/const.kt new file mode 100644 index 00000000000..44780ac0ea8 --- /dev/null +++ b/idea/testData/intentions/introduceBackingProperty/const.kt @@ -0,0 +1,6 @@ +// IS_APPLICABLE: false +class C { + companion object { + const val CONST = 1 + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 0a5a5df298a..5716fee1f92 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -9289,6 +9289,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/introduceBackingProperty/backingFieldRef.kt"); } + @TestMetadata("const.kt") + public void testConst() throws Exception { + runTest("idea/testData/intentions/introduceBackingProperty/const.kt"); + } + @TestMetadata("inapplicableAbstractProperty.kt") public void testInapplicableAbstractProperty() throws Exception { runTest("idea/testData/intentions/introduceBackingProperty/inapplicableAbstractProperty.kt");