EA-261935 - check whether every ctor param has an associated field by name

GitOrigin-RevId: 8d225300a2752035b3f15d64ba932a8dd2308eae
This commit is contained in:
Ilyas Selimov
2021-03-22 12:24:45 +00:00
committed by intellij-monorepo-bot
parent c03a44af2a
commit 671b158a08
4 changed files with 22 additions and 2 deletions
@@ -236,11 +236,18 @@ public class ConvertToRecordFix extends InspectionGadgetsFix {
private RecordConstructorCandidate(@NotNull PsiMethod constructor, @NotNull Set<PsiField> instanceFields) {
myConstructor = constructor;
if (constructor.getTypeParameters().length > 0) {
if (myConstructor.getTypeParameters().length > 0) {
myCanonical = false;
return;
}
PsiParameter[] ctorParams = myConstructor.getParameterList().getParameters();
Set<String> instanceFieldNames = instanceFields.stream().map(PsiField::getName).collect(Collectors.toSet());
for (PsiParameter param : ctorParams) {
if (!instanceFieldNames.contains(param.getName())) {
myCanonical = false;
return;
}
}
Map<String, PsiType> ctorParamsWithType = Arrays.stream(ctorParams)
.collect(Collectors.toMap(param -> param.getName(), param -> param.getType(), (first, second) -> first));
if (ctorParams.length != ctorParamsWithType.size()) {
@@ -45,7 +45,7 @@ class RecordBuilder {
String annotationsText = generateAnnotationsText(parameterName, fieldAccessors);
recordComponentsJoiner.add(annotationsText + parameter.getType().getCanonicalText() + " " + parameterName);
}
myRecordText.append(recordComponentsJoiner.toString());
myRecordText.append(recordComponentsJoiner);
}
myRecordText.append(")");
}
@@ -0,0 +1,8 @@
// "Convert to a record" "false"
class <caret>R {
final int first;
R(int first, int second) {
this.first = first;
}
}
@@ -1,4 +1,9 @@
// "Convert to a record" "false"
class <caret>R {
int first = 1;
final int second;
R(int second) {
this.second = second;
}
}