PsiVariable (final String[] names, final PsiType psiType) { final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(myProject); return WriteAction.compute(() -> { PsiClass parentClass = getParentClass(); PsiField field = elementFactory.createFieldFromText( psiType.getCanonicalText() + " " + (chooseName(names, parentClass.getLanguage())) + " = " + myInitializerText + ";", parentClass); PsiUtil.setModifierProperty(field, PsiModifier.FINAL, true); PsiUtil.setModifierProperty(field, PsiModifier.STATIC, true); myVisibility = getSelectedVisibility(); PsiUtil.setModifierProperty(field, myVisibility, true); PsiElement finalAnchorElement = getAnchorElementIfAll(); while (finalAnchorElement != null && finalAnchorElement.getParent() != parentClass) { finalAnchorElement = finalAnchorElement.getParent(); } PsiMember anchorMember = finalAnchorElement instanceof PsiMember ? (PsiMember)finalAnchorElement : null; field = BaseExpressionToFieldHandler.ConvertToFieldRunnable .appendField(myExpr, BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION, parentClass, parentClass, field, anchorMember); updateVariable(field); return field; }); }