From 09c6ede351aba2dbf57436200e8e0f64f74a496d Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Fri, 24 Jan 2020 13:13:05 +0700 Subject: [PATCH] Use default values when generating custom constructor for records to avoid compilation error Review ID: IDEA-CR-57600 GitOrigin-RevId: 847c2c4f925714400e7daa25360b8d66620a8328 --- .../JavaConstructorBodyWithSuperCallGenerator.java | 9 +++++++-- .../afterRecordCustomConstructor2.java | 2 +- .../afterRecordCustomConstructor3.java | 7 +++++++ .../beforeRecordCustomConstructor3.java | 5 +++++ .../java/codeInsight/GenerateConstructorTest.java | 4 ++++ 5 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/generateConstructor/afterRecordCustomConstructor3.java create mode 100644 java/java-tests/testData/codeInsight/generateConstructor/beforeRecordCustomConstructor3.java diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/JavaConstructorBodyWithSuperCallGenerator.java b/java/java-impl/src/com/intellij/codeInsight/generation/JavaConstructorBodyWithSuperCallGenerator.java index dd13bcdd2d79..8bd16b4cec5a 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/JavaConstructorBodyWithSuperCallGenerator.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/JavaConstructorBodyWithSuperCallGenerator.java @@ -20,11 +20,13 @@ import com.intellij.psi.PsiField; import com.intellij.psi.PsiParameter; import com.intellij.psi.PsiRecordComponent; import com.intellij.psi.util.JavaPsiRecordUtil; +import com.intellij.psi.util.PsiTypesUtil; import com.intellij.util.ArrayUtil; import one.util.streamex.StreamEx; import org.jetbrains.annotations.NotNull; import java.util.Collection; +import java.util.Objects; public class JavaConstructorBodyWithSuperCallGenerator implements ConstructorBodyGenerator { @Override @@ -50,8 +52,11 @@ public class JavaConstructorBodyWithSuperCallGenerator implements ConstructorBod if (components.length > fields.length) { buffer.append(StreamEx.of(components) .map(JavaPsiRecordUtil::getFieldForComponent) - .mapToInt(f -> ArrayUtil.indexOf(fields, f)) - .mapToObj(idx -> idx >= 0 ? parameters[idx].getName() : "") + .peek(Objects::requireNonNull) + .map(f -> { + int index = ArrayUtil.indexOf(fields, f); + return index >= 0 ? parameters[index].getName() : PsiTypesUtil.getDefaultValueOfType(f.getType(), true); + }) .joining(",", "this(", ")")); appendSemicolon(buffer); return true; diff --git a/java/java-tests/testData/codeInsight/generateConstructor/afterRecordCustomConstructor2.java b/java/java-tests/testData/codeInsight/generateConstructor/afterRecordCustomConstructor2.java index a63435468048..43da754c9cbf 100644 --- a/java/java-tests/testData/codeInsight/generateConstructor/afterRecordCustomConstructor2.java +++ b/java/java-tests/testData/codeInsight/generateConstructor/afterRecordCustomConstructor2.java @@ -3,6 +3,6 @@ import java.lang.annotation.Target; record Test(int x,int y,int z) { public Test(int x, int y) { - this(x, y, ); + this(x, y, 0); } } diff --git a/java/java-tests/testData/codeInsight/generateConstructor/afterRecordCustomConstructor3.java b/java/java-tests/testData/codeInsight/generateConstructor/afterRecordCustomConstructor3.java new file mode 100644 index 000000000000..94030cbf1419 --- /dev/null +++ b/java/java-tests/testData/codeInsight/generateConstructor/afterRecordCustomConstructor3.java @@ -0,0 +1,7 @@ +import java.util.Optional; + +record Test(int x,int y, boolean a, double b, Optional opt, int[] data) { + public Test(int x, int y) { + this(x, y, false, 0, Optional.empty(), new int[0]); + } +} diff --git a/java/java-tests/testData/codeInsight/generateConstructor/beforeRecordCustomConstructor3.java b/java/java-tests/testData/codeInsight/generateConstructor/beforeRecordCustomConstructor3.java new file mode 100644 index 000000000000..bc699335dfac --- /dev/null +++ b/java/java-tests/testData/codeInsight/generateConstructor/beforeRecordCustomConstructor3.java @@ -0,0 +1,5 @@ +import java.util.Optional; + +record Test(int x,int y, boolean a, double b, Optional opt, int[] data) { + +} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/GenerateConstructorTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/GenerateConstructorTest.java index 577e7f4943e9..15e63e08fc29 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/GenerateConstructorTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/GenerateConstructorTest.java @@ -125,6 +125,10 @@ public class GenerateConstructorTest extends LightJavaCodeInsightFixtureTestCase public void testRecordCustomConstructor2() { doTestRecordConstructor((aClass, m) -> Arrays.copyOf(m, 2)); } + + public void testRecordCustomConstructor3() { + doTestRecordConstructor((aClass, m) -> Arrays.copyOf(m, 2)); + } private void doTest() { doTest(false);