java: create constructor from usage: ensure parameter type is evaluated before constructor is inserted (IDEA-244902)

otherwise inference may detect overload conflict with existing no-arg constructor

GitOrigin-RevId: bade4409a64e31bd0c7c4d79ffa41222b17a7c8f
This commit is contained in:
Anna Kozlova
2020-07-01 08:27:38 +00:00
committed by intellij-monorepo-bot
parent fab2f3b722
commit 24cb042051
3 changed files with 36 additions and 1 deletions
@@ -50,12 +50,15 @@ private class JavaConstructorRenderer(
private val factory = JavaPsiFacade.getElementFactory(project)!!
fun doMagic() {
//calculate expected parameter types before constructor is inserted
//to avoid possible overload conflicts
val parameters = request.expectedParameters
var constructor = renderConstructor()
constructor = insertConstructor(constructor)
constructor = forcePsiPostprocessAndRestoreElement(constructor) ?: return
val builder = TemplateBuilderImpl(constructor)
createTemplateContext(builder).setupParameters(constructor, request.expectedParameters)
createTemplateContext(builder).setupParameters(constructor, parameters)
val superConstructor = setupSuperCall(targetClass, constructor, builder)
constructor = forcePsiPostprocessAndRestoreElement(constructor) ?: return
@@ -0,0 +1,18 @@
// "Create constructor" "true"
class MyTest {
<T> T id(T t) {
return t;
}
{
Foo f = new Foo(id("name"));
}
}
class Foo {
public Foo() {}
public Foo(String name) {
}
}
@@ -0,0 +1,14 @@
// "Create constructor" "true"
class MyTest {
<T> T id(T t) {
return t;
}
{
Foo f = new Foo(i<caret>d("name"));
}
}
class Foo {
public Foo() {}
}