void (@NotNull PsiElement element, @NotNull ActionContext context, @NotNull ModPsiUpdater updater) { final PsiField field = (PsiField)element.getParent(); final PsiExpression initializer = field.getInitializer(); if (initializer == null) { return; } final String initializerText = CommonJavaRefactoringUtil.convertInitializerToNormalExpression(initializer, field.getType()).getText(); final PsiClass containingClass = field.getContainingClass(); if (containingClass == null) { return; } final boolean fieldIsStatic = field.hasModifierProperty(PsiModifier.STATIC); final PsiClassInitializer[] classInitializers = containingClass.getInitializers(); PsiClassInitializer classInitializer = null; final int fieldOffset = field.getTextOffset(); for (PsiClassInitializer existingClassInitializer : classInitializers) { final int initializerOffset = existingClassInitializer.getTextOffset(); if (initializerOffset <= fieldOffset) { continue; } final boolean initializerIsStatic = existingClassInitializer.hasModifierProperty(PsiModifier.STATIC); if (initializerIsStatic == fieldIsStatic) { Condition<PsiReference> usedBeforeInitializer = ref -> { PsiElement refElement = ref.getElement(); TextRange textRange = refElement.getTextRange(); return textRange == null || textRange.getStartOffset() < initializerOffset; }; if (!ContainerUtil .exists(ReferencesSearch.search(field, new LocalSearchScope(containingClass)).findAll(), usedBeforeInitializer)) { classInitializer = existingClassInitializer; break; } } } final PsiManager manager = field.getManager(); final Project project = manager.getProject(); final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project); if (classInitializer == null) { if (PsiUtil.isJavaToken(PsiTreeUtil.skipWhitespacesForward(field), JavaTokenType.COMMA)) { field.normalizeDeclaration(); } classInitializer = (PsiClassInitializer)containingClass.addAfter(elementFactory.createClassInitializer(), field); // add some whitespace between the field and the class initializer final PsiElement whitespace = PsiParserFacade.getInstance(project).createWhiteSpaceFromText("\n"); containingClass.addAfter(whitespace, field); } final PsiCodeBlock body = classInitializer.getBody(); @NonNls final String initializationStatementText = field.getName() + " = " + initializerText + ';'; final PsiExpressionStatement statement = (PsiExpressionStatement)elementFactory.createStatementFromText(initializationStatementText, body); final PsiElement addedElement = body.addAfter(statement, null); if (fieldIsStatic) { final PsiModifierList modifierList = classInitializer.getModifierList(); if (modifierList != null) { modifierList.setModifierProperty(PsiModifier.STATIC, true); } } initializer.delete(); CodeStyleManager.getInstance(manager.getProject()).reformat(classInitializer); updater.highlight(addedElement); }