public static PsiField createFieldAndAddAssignment(@NotNull Project project,
                                                     @NotNull PsiClass targetClass,
                                                     @NotNull PsiMethod method,
                                                     @NotNull PsiParameter parameter,
                                                     @NotNull PsiType fieldType,
                                                     @NotNull String fieldName,
                                                     boolean isStatic,
                                                     boolean isFinal) {
    PsiManager psiManager = PsiManager.getInstance(project);
    JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(psiManager.getProject());
    PsiElementFactory factory = psiFacade.getElementFactory();

    PsiField field = factory.createField(fieldName, fieldType);

    PsiModifierList modifierList = field.getModifierList();
    if (modifierList == null) return null;
    modifierList.setModifierProperty(PsiModifier.STATIC, isStatic);
    modifierList.setModifierProperty(PsiModifier.FINAL, isFinal);

    NullableNotNullManager manager = NullableNotNullManager.getInstance(project);
    if (manager.copyNullableAnnotation(parameter, field) == null && isFinal) {
      manager.copyNotNullAnnotation(parameter, field);
    }

    PsiCodeBlock methodBody = method.getBody();
    if (methodBody == null) return null;
    PsiStatement[] statements = methodBody.getStatements();

    if (statements.length == 0) {
      PsiElement element = methodBody.getFirstBodyElement();
      if (element instanceof PsiWhiteSpace whiteSpace) {
        String text = whiteSpace.getText();
        int lastLineBreak = text.lastIndexOf('\n');
        if (lastLineBreak >= 0 && text.indexOf('\n') != lastLineBreak) {
          // At least two linebreaks in the body: remove last one
          text = text.substring(0, lastLineBreak);
          methodBody.getNode().replaceChild(whiteSpace.getNode(), ASTFactory.leaf(TokenType.WHITE_SPACE, text));
        }
      }
    }

    Ref<PsiField> anchor = new Ref<>();
    AtomicBoolean isBefore = new AtomicBoolean();
    int i = findFieldAssignmentAnchor(statements, anchor, isBefore, targetClass, parameter);

    String stmtText = fieldName + " = " + parameter.getName() + ";";

    PsiVariable variable = psiFacade.getResolveHelper().resolveReferencedVariable(fieldName, methodBody);
    if (variable != null && !(variable instanceof PsiField)) {
      String prefix = isStatic ? targetClass.getName() == null ? "" : targetClass.getName() + "." : "this.";
      stmtText = prefix + stmtText;
    }

    PsiStatement assignmentStmt = factory.createStatementFromText(stmtText, methodBody);
    assignmentStmt = (PsiStatement)CodeStyleManager.getInstance(project).reformat(assignmentStmt);

    if (i == statements.length) {
      methodBody.add(assignmentStmt);
    }
    else {
      methodBody.addAfter(assignmentStmt, i > 0 ? statements[i - 1] : null);
    }

    if (!anchor.isNull()) {
      PsiVariable psiVariable = anchor.get();
      psiVariable.normalizeDeclaration();
    }

    if (targetClass.findFieldByName(fieldName, false) == null) {
      if (!anchor.isNull()) {
        PsiField inField = anchor.get();
        if (isBefore.get()) {
          return (PsiField)targetClass.addBefore(field, inField);
        }
        else {
          return (PsiField)targetClass.addAfter(field, inField);
        }
      }
      else {
        return (PsiField)targetClass.add(field);
      }
    }
    return null;
  }
public static PsiField createFieldAndAddAssignment(@NotNull Project project,
                                                     @NotNull PsiClass targetClass,
                                                     @NotNull PsiMethod method,
                                                     @NotNull PsiParameter parameter,
                                                     @NotNull PsiType fieldType,
                                                     @NotNull String fieldName,
                                                     boolean isStatic,
                                                     boolean isFinal)