PsiMethod (@NotNull PsiMethod compactConstructor) { if (!JavaPsiRecordUtil.isCompactConstructor(compactConstructor)) { throw new IllegalArgumentException("Compact constructor required"); } PsiClass recordClass = Objects.requireNonNull(compactConstructor.getContainingClass()); PsiMethod prototype = new RecordConstructorMember(recordClass, false).generateRecordConstructor(); PsiModifierList modifierList = compactConstructor.getModifierList(); prototype.getModifierList().replace(modifierList); PsiElement beforeModifier = modifierList.getPrevSibling(); if (beforeModifier != null) { prototype.addRangeBefore(compactConstructor.getFirstChild(), beforeModifier, prototype.getModifierList()); } PsiCodeBlock oldBody = Objects.requireNonNull(compactConstructor.getBody()); PsiCodeBlock body = (PsiCodeBlock)Objects.requireNonNull(prototype.getBody()).replace(oldBody); PsiElementFactory factory = JavaPsiFacade.getElementFactory(compactConstructor.getProject()); for (PsiRecordComponent component : recordClass.getRecordComponents()) { PsiField field = JavaPsiRecordUtil.getFieldForComponent(component); if (field != null && !HighlightControlFlowUtil.variableDefinitelyAssignedIn(field, body)) { body.add(factory.createStatementFromText("this." + field.getName() + "=" + field.getName() + ";", body)); } } return prototype; }