void (PsiReferenceExpression referenceExpression, PsiIfStatement ifStatement, @NotNull ModPsiUpdater updater) { final PsiElement resolved = referenceExpression.resolve(); if (!(resolved instanceof PsiField field)) { return; } final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(field.getProject()); final String fieldName = field.getName(); @NonNls final String holderName = StringUtil.capitalize(codeStyleManager.variableNameToPropertyName(fieldName, VariableKind.STATIC_FINAL_FIELD)) + "Holder"; final PsiElement expressionParent = referenceExpression.getParent(); if (!(expressionParent instanceof PsiAssignmentExpression assignmentExpression)) { return; } final PsiExpression rhs = assignmentExpression.getRExpression(); if (rhs == null) { return; } @NonNls final String text = "private static final class " + holderName + " {}"; final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(field.getProject()); final PsiClass holder = elementFactory.createClassFromText(text, field).getInnerClasses()[0]; final PsiMember method = PsiTreeUtil.getParentOfType(referenceExpression, PsiMember.class); if (method == null) { return; } final PsiClass holderClass = (PsiClass)method.getParent().addBefore(holder, method); final PsiField newField = (PsiField)holderClass.add(field); final PsiModifierList modifierList = newField.getModifierList(); assert modifierList != null; modifierList.setModifierProperty(PsiModifier.FINAL, true); if (!PsiUtil.isLanguageLevel11OrHigher(holderClass)) { modifierList.setModifierProperty(PsiModifier.PACKAGE_LOCAL, true); } newField.setInitializer(rhs); CodeStyleManager.getInstance(referenceExpression.getProject()).reformat(holderClass); if (ifStatement != null) { new CommentTracker().deleteAndRestoreComments(ifStatement); } final PsiExpression holderReference = elementFactory.createExpressionFromText(holderName + "." + fieldName, field); final PsiClass containingClass = PsiUtil.getTopLevelClass(field); if (containingClass == null) return; // Search references within top-level class only. // If there are other references, the fix should not be created, see isStaticAndAssignedOnce for (PsiReferenceExpression reference : VariableAccessUtils.getVariableReferences(field, containingClass)) { reference.replace(holderReference); } field.delete(); updater.rename(holderClass, Stream.of(holderClass.getName(), suggestHolderName(field)).distinct().toList()); }