mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Use default values when generating custom constructor for records to avoid compilation error
Review ID: IDEA-CR-57600 GitOrigin-RevId: 847c2c4f925714400e7daa25360b8d66620a8328
This commit is contained in:
committed by
intellij-monorepo-bot
parent
619bfa9963
commit
09c6ede351
+7
-2
@@ -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;
|
||||
|
||||
+1
-1
@@ -3,6 +3,6 @@ import java.lang.annotation.Target;
|
||||
|
||||
record Test(int x,int y,int z) {
|
||||
public Test(int x, int y) {<caret>
|
||||
this(x, y, );
|
||||
this(x, y, 0);
|
||||
}
|
||||
}
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
import java.util.Optional;
|
||||
|
||||
record Test(int x,int y, boolean a, double b, Optional<String> opt, int[] data) {
|
||||
public Test(int x, int y) {<caret>
|
||||
this(x, y, false, 0, Optional.empty(), new int[0]);
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import java.util.Optional;
|
||||
|
||||
record Test(int x,int y, boolean a, double b, Optional<String> opt, int[] data) {
|
||||
<caret>
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user