void (@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) { PsiAssignmentExpression assignment = ObjectUtils.tryCast(element, PsiAssignmentExpression.class); if (assignment == null) return; PsiField field = getAssignedField(assignment); if (field == null) return; List<PsiAssignmentExpression> assignments = new ArrayList<>(); if (!isInitializedWithSameExpression(field, assignment, assignments)) return; PsiElement prev = PsiTreeUtil.skipWhitespacesAndCommentsBackward(assignment.getParent()); String comments = prev == null ? null : CommentTracker.commentsBetween(prev, assignment); CommentTracker ct = new CommentTracker(); // Should not reach here if getRExpression is null: isInitializedWithSameExpression would return false field.setInitializer(ct.markUnchanged(assignment.getRExpression())); PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); if (comments != null) { PsiCodeBlock block = factory.createCodeBlockFromText("{" + comments + "}", assignment); for(PsiElement child : block.getChildren()) { if(child instanceof PsiComment || child instanceof PsiWhiteSpace) { field.getParent().addBefore(child, field); } } } PsiModifierListOwner owner = enclosingMethodOrClassInitializer(assignment, field); for (PsiAssignmentExpression assignmentExpression : assignments) { PsiElement parent = assignmentExpression.getParent(); if (parent instanceof PsiExpressionStatement) { PsiElement grandParent = parent.getParent(); if (grandParent instanceof PsiIfStatement || grandParent instanceof PsiLoopStatement) { ct.replace(parent, ";"); } else if (grandParent instanceof PsiSwitchLabeledRuleStatement) { ct.replace(parent, "{}"); } else { ct.delete(parent); } } else if (parent instanceof PsiLambdaExpression) { ct.replace(assignmentExpression, factory.createCodeBlock()); } ct.insertCommentsBefore(field); // if we replace/delete several assignments we want to restore comments at each place separately ct = new CommentTracker(); } // Delete empty initializer left after fix if (owner instanceof PsiClassInitializer initializer) { PsiCodeBlock body = initializer.getBody(); if(body.isEmpty() && !ContainerUtil.exists(body.getChildren(), PsiComment.class::isInstance)) { new CommentTracker().deleteAndRestoreComments(owner); } } }