Quick Fixes: Fix adding parameter to Java constructor

#KT-24574 In Progress
This commit is contained in:
Alexey Sedunov
2018-05-24 20:12:36 +03:00
parent 6e25db990d
commit 35517a8253
12 changed files with 72 additions and 4 deletions
@@ -264,9 +264,8 @@ open class KotlinChangeInfo(
}
buffer.append('.')
}
buffer.append(name)
}
buffer.append(name)
}
buffer.append(getNewParametersSignature(inheritedCallable))
@@ -153,16 +153,17 @@ class KotlinChangeSignature(
// Generate new Java method signature from the Kotlin point of view
val ktChangeInfo = KotlinChangeInfo(methodDescriptor = descriptor, context = defaultValueContext)
val ktSignature = ktChangeInfo.getNewSignature(descriptor.originalPrimaryCallable)
val previewClassName = if (originalMethod.isConstructor) originalMethod.name else "Dummy"
val dummyFileText = with(StringBuilder()) {
contextFile.packageDirective?.let { append(it.text).append("\n") }
append("class Dummy {\n").append(ktSignature).append("{}\n}")
append("class $previewClassName {\n").append(ktSignature).append("{}\n}")
toString()
}
val dummyFile = KtPsiFactory(project).createFileWithLightClassSupport("dummy.kt", dummyFileText, originalMethod)
val dummyDeclaration = (dummyFile.declarations.first() as KtClass).getBody()!!.declarations.first()
// Convert to PsiMethod which can be used in Change Signature dialog
val containingClass = PsiElementFactory.SERVICE.getInstance(project).createClass("Dummy")
val containingClass = PsiElementFactory.SERVICE.getInstance(project).createClass(previewClassName)
val preview = createJavaMethod(dummyDeclaration.getRepresentativeLightMethod()!!, containingClass)
// Create JavaChangeInfo based on new signature
@@ -0,0 +1,5 @@
import java.util.Set;
class Foo {
Foo(int n, String s, Set<String> of) {}
}
@@ -0,0 +1,4 @@
// "Add parameter to constructor 'Foo'" "true"
// WITH_RUNTIME
// DISABLE-ERRORS
private val foo = Foo(1, "2", <caret>setOf("3"))
@@ -0,0 +1,3 @@
class Foo {
Foo(int n, String s) {}
}
@@ -0,0 +1,4 @@
// "Add parameter to constructor 'Foo'" "true"
// WITH_RUNTIME
// DISABLE-ERRORS
private val foo = Foo(1, "2", <caret>setOf("3"))
@@ -0,0 +1,5 @@
import java.util.Set;
class Foo {
void bar(int n, String s, Set<String> of) {}
}
@@ -0,0 +1,4 @@
// "Add parameter to function 'bar'" "true"
// WITH_RUNTIME
// DISABLE-ERRORS
private val foo = Foo().bar(1, "2", <caret>setOf("3"))
@@ -0,0 +1,3 @@
class Foo {
void bar(int n, String s) {}
}
@@ -0,0 +1,4 @@
// "Add parameter to function 'bar'" "true"
// WITH_RUNTIME
// DISABLE-ERRORS
private val foo = Foo().bar(1, "2", <caret>setOf("3"))
@@ -1100,6 +1100,29 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
runTest("idea/testData/quickfix/changeSignature/jk/jkRemoveSecondaryConstructorParameter.before.Main.java");
}
}
@TestMetadata("idea/testData/quickfix/changeSignature/kj")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Kj extends AbstractQuickFixMultiFileTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTestWithExtraFile, TargetBackend.ANY, testDataFilePath);
}
@TestMetadata("addConstructorParameter.before.Main.kt")
public void testAddConstructorParameter() throws Exception {
runTest("idea/testData/quickfix/changeSignature/kj/addConstructorParameter.before.Main.kt");
}
@TestMetadata("addMethodParameter.before.Main.kt")
public void testAddMethodParameter() throws Exception {
runTest("idea/testData/quickfix/changeSignature/kj/addMethodParameter.before.Main.kt");
}
public void testAllFilesPresentInKj() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/changeSignature/kj"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), TargetBackend.ANY, true);
}
}
}
@TestMetadata("idea/testData/quickfix/changeToLabeledReturn")
@@ -1695,6 +1695,19 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/changeSignature/jk"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
}
@TestMetadata("idea/testData/quickfix/changeSignature/kj")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Kj extends AbstractQuickFixTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInKj() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/changeSignature/kj"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
}
}
@TestMetadata("idea/testData/quickfix/changeToLabeledReturn")