Pull Up: Drop default parameter values in function which becomes overriding. Disable function with default values if target class is the Java one

#KT-9833 Fixed
This commit is contained in:
Alexey Sedunov
2015-12-02 17:14:58 +03:00
parent 80051b8303
commit 8f8acf7a83
11 changed files with 90 additions and 15 deletions
@@ -191,4 +191,10 @@ fun KtSecondaryConstructor.getOrCreateBody(): KtBlockExpression {
val anchor = if (delegationCall.isImplicit) valueParameterList else delegationCall
val newBody = KtPsiFactory(this).createEmptyBody()
return addAfter(newBody, anchor) as KtBlockExpression
}
fun KtParameter.dropDefaultValue() {
val from = equalsToken ?: return
val to = defaultValue ?: from
deleteChildRange(from, to)
}
@@ -33,6 +33,7 @@ import org.jetbrains.kotlin.asJava.unwrapped
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.idea.codeInsight.shorten.addToShorteningWaitSet
import org.jetbrains.kotlin.idea.core.dropDefaultValue
import org.jetbrains.kotlin.idea.core.refactoring.createJavaField
import org.jetbrains.kotlin.idea.core.refactoring.createJavaMethod
import org.jetbrains.kotlin.idea.core.refactoring.createPrimaryConstructorIfAbsent
@@ -360,6 +361,7 @@ class KotlinPullUpHelper(
}
else {
member.addModifierWithSpace(KtTokens.OVERRIDE_KEYWORD)
(member as? KtNamedFunction)?.valueParameters?.forEach { it.dropDefaultValue() }
}
}
@@ -17,7 +17,6 @@
package org.jetbrains.kotlin.idea.refactoring.pullUp
import com.intellij.psi.PsiClass
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.idea.codeInsight.shorten.addToShorteningWaitSet
@@ -46,7 +45,7 @@ fun KtNamedDeclaration.canMoveMemberToJavaClass(targetClass: PsiClass): Boolean
if (accessors.isNotEmpty() || delegateExpression != null) return false
true
}
is KtNamedFunction -> true
is KtNamedFunction -> valueParameters.all { it.defaultValue == null }
else -> false
}
}
@@ -0,0 +1,3 @@
class A {
}
@@ -0,0 +1,3 @@
class A {
}
@@ -0,0 +1,11 @@
class <caret>B : A() {
// INFO: {"checked": "true", "toAbstract": "true"}
fun foo(n: Int = 1) {
}
}
fun test() {
B().foo()
B().foo(2)
}
@@ -0,0 +1,11 @@
class B : A() {
// INFO: {"checked": "true", "toAbstract": "true"}
fun foo(n: Int = 1) {
}
}
fun test() {
B().foo()
B().foo(2)
}
@@ -0,0 +1,15 @@
open class A {
}
class <caret>B : A() {
// INFO: {"checked": "true", "toAbstract": "true"}
fun foo(n: Int = 1) {
}
}
fun test() {
B().foo()
B().foo(2)
}
@@ -0,0 +1,17 @@
abstract class A {
// INFO: {"checked": "true", "toAbstract": "true"}
abstract fun foo(n: Int = 1)
}
class B : A() {
// INFO: {"checked": "true", "toAbstract": "true"}
override fun foo(n: Int) {
}
}
fun test() {
B().foo()
B().foo(2)
}
@@ -49,6 +49,12 @@ public class PullUpTestGenerated extends AbstractPullUpTest {
doKotlinTest(fileName);
}
@TestMetadata("defaultValuesInOverride.kt")
public void testDefaultValuesInOverride() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/pullUp/k2k/defaultValuesInOverride.kt");
doKotlinTest(fileName);
}
@TestMetadata("fromClassToClass.kt")
public void testFromClassToClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/pullUp/k2k/fromClassToClass.kt");
@@ -232,6 +238,12 @@ public class PullUpTestGenerated extends AbstractPullUpTest {
KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File("idea/testData/refactoring/pullUp/k2j"), Pattern.compile("^(.+)\\.kt$"));
}
@TestMetadata("defaultValuesInOverride.kt")
public void testDefaultValuesInOverride() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/pullUp/k2j/defaultValuesInOverride.kt");
doKotlinTest(fileName);
}
@TestMetadata("fromClassToClass.kt")
public void testFromClassToClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/pullUp/k2j/fromClassToClass.kt");