From e10b50a648e234866686fa7a5135e4167554f73f Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Wed, 11 Jan 2017 13:24:05 +0300 Subject: [PATCH] Extract Interface/Pull Up: Drop 'final' modifier when moving to an interface #KT-15640 Fixed --- ChangeLog.md | 1 + .../kotlin/idea/refactoring/pullUp/pullUpUtils.kt | 5 ++++- .../refactoring/extractInterface/dropFinal.kt | 6 ++++++ .../refactoring/extractInterface/dropFinal.kt.after | 11 +++++++++++ idea/testData/refactoring/pullUp/k2k/dropFinal.kt | 6 ++++++ .../refactoring/pullUp/k2k/dropFinal.kt.after | 9 +++++++++ .../introduce/ExtractionTestGenerated.java | 6 ++++++ .../idea/refactoring/pullUp/PullUpTestGenerated.java | 6 ++++++ 8 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 idea/testData/refactoring/extractInterface/dropFinal.kt create mode 100644 idea/testData/refactoring/extractInterface/dropFinal.kt.after create mode 100644 idea/testData/refactoring/pullUp/k2k/dropFinal.kt create mode 100644 idea/testData/refactoring/pullUp/k2k/dropFinal.kt.after diff --git a/ChangeLog.md b/ChangeLog.md index 547fa0cc0dc..8a1660ce124 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -479,6 +479,7 @@ These artifacts include extensions for the types available in the latter JDKs, s - [`KT-15353`](https://youtrack.jetbrains.com/issue/KT-15353) Extract Superclass/Interface: Allow extracting class with special name (and quotes) - [`KT-15643`](https://youtrack.jetbrains.com/issue/KT-15643) Extract Interface/Pull Up: Disable "Make abstract" and assume it to be true for primary constructor parameter when moving to an interface - [`KT-15607`](https://youtrack.jetbrains.com/issue/KT-15607) Extract Interface/Pull Up: Disable internal/protected members when moving to an interface +- [`KT-15640`](https://youtrack.jetbrains.com/issue/KT-15640) Extract Interface/Pull Up: Drop 'final' modifier when moving to an interface #### Intention actions, inspections and quickfixes diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/pullUp/pullUpUtils.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/pullUp/pullUpUtils.kt index 112a0dfa2ea..d55656c4a25 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/pullUp/pullUpUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/pullUp/pullUpUtils.kt @@ -18,7 +18,6 @@ package org.jetbrains.kotlin.idea.refactoring.pullUp import com.intellij.psi.PsiClass import com.intellij.psi.PsiMethod -import com.intellij.psi.PsiNamedElement import org.jetbrains.kotlin.asJava.toLightClass import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor import org.jetbrains.kotlin.descriptors.ClassDescriptor @@ -59,6 +58,10 @@ fun KtNamedDeclaration.canMoveMemberToJavaClass(targetClass: PsiClass): Boolean } fun addMemberToTarget(targetMember: KtNamedDeclaration, targetClass: KtClassOrObject): KtNamedDeclaration { + if (targetClass is KtClass && targetClass.isInterface()) { + targetMember.removeModifier(KtTokens.FINAL_KEYWORD) + } + if (targetMember is KtParameter) { val parameterList = (targetClass as KtClass).createPrimaryConstructorIfAbsent().valueParameterList!! val anchor = parameterList.parameters.firstOrNull { it.isVarArg || it.hasDefaultValue() } diff --git a/idea/testData/refactoring/extractInterface/dropFinal.kt b/idea/testData/refactoring/extractInterface/dropFinal.kt new file mode 100644 index 00000000000..a3efd038007 --- /dev/null +++ b/idea/testData/refactoring/extractInterface/dropFinal.kt @@ -0,0 +1,6 @@ +// NAME: I +// SIBLING: +class A { + // INFO: {checked: "true"} + final val one = 1 +} \ No newline at end of file diff --git a/idea/testData/refactoring/extractInterface/dropFinal.kt.after b/idea/testData/refactoring/extractInterface/dropFinal.kt.after new file mode 100644 index 00000000000..8f31e339c82 --- /dev/null +++ b/idea/testData/refactoring/extractInterface/dropFinal.kt.after @@ -0,0 +1,11 @@ +interface I { + // INFO: {checked: "true"} + val one: Int +} + +// NAME: I +// SIBLING: +class A : I { + // INFO: {checked: "true"} + final override val one = 1 +} \ No newline at end of file diff --git a/idea/testData/refactoring/pullUp/k2k/dropFinal.kt b/idea/testData/refactoring/pullUp/k2k/dropFinal.kt new file mode 100644 index 00000000000..b8d8a37567a --- /dev/null +++ b/idea/testData/refactoring/pullUp/k2k/dropFinal.kt @@ -0,0 +1,6 @@ +interface I + +class A : I { + // INFO: {checked: "true"} + final val one = 1 +} \ No newline at end of file diff --git a/idea/testData/refactoring/pullUp/k2k/dropFinal.kt.after b/idea/testData/refactoring/pullUp/k2k/dropFinal.kt.after new file mode 100644 index 00000000000..babf5666b7b --- /dev/null +++ b/idea/testData/refactoring/pullUp/k2k/dropFinal.kt.after @@ -0,0 +1,9 @@ +interface I { + // INFO: {checked: "true"} + val one: Int +} + +class A : I { + // INFO: {checked: "true"} + final override val one = 1 +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java index 0e2ffd7c1ac..c950363d069 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java @@ -4358,6 +4358,12 @@ public class ExtractionTestGenerated extends AbstractExtractionTest { doExtractInterfaceTest(fileName); } + @TestMetadata("dropFinal.kt") + public void testDropFinal() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/extractInterface/dropFinal.kt"); + doExtractInterfaceTest(fileName); + } + @TestMetadata("specialName.kt") public void testSpecialName() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/extractInterface/specialName.kt"); diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/pullUp/PullUpTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/refactoring/pullUp/PullUpTestGenerated.java index d73dbc2cdca..724eec1e245 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/pullUp/PullUpTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/pullUp/PullUpTestGenerated.java @@ -110,6 +110,12 @@ public class PullUpTestGenerated extends AbstractPullUpTest { doKotlinTest(fileName); } + @TestMetadata("dropFinal.kt") + public void testDropFinal() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/pullUp/k2k/dropFinal.kt"); + doKotlinTest(fileName); + } + @TestMetadata("dropModifierWhenMovingSideOverride.kt") public void testDropModifierWhenMovingSideOverride() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/pullUp/k2k/dropModifierWhenMovingSideOverride.kt");