PsiStatement (@NotNull Project project, @NotNull PsiElementFactory factory) { myField.normalizeDeclaration(); PsiClassInitializer initializer = tryCast(PsiTreeUtil.skipWhitespacesAndCommentsForward(myField), PsiClassInitializer.class); boolean isStatic = myField.hasModifierProperty(PsiModifier.STATIC); if (initializer == null || initializer.hasModifierProperty(PsiModifier.STATIC) != isStatic) { initializer = factory.createClassInitializer(); if (isStatic) { Objects.requireNonNull(initializer.getModifierList()).setModifierProperty(PsiModifier.STATIC, true); } initializer = (PsiClassInitializer)myField.getParent().addAfter(initializer, myField); } PsiCodeBlock body = initializer.getBody(); // There are at least two children: open and close brace // we will insert an initializer after the first brace and any whitespace which follow it PsiElement anchor = PsiTreeUtil.skipWhitespacesForward(body.getFirstChild()); assert anchor != null; anchor = anchor.getPrevSibling(); assert anchor != null; PsiExpressionStatement assignment = (PsiExpressionStatement)factory.createStatementFromText(myField.getName() + "=null;", initializer); assignment = (PsiExpressionStatement)body.addAfter(assignment, anchor); PsiExpression fieldInitializer = myField.getInitializer(); fieldInitializer = ExpressionUtils.convertInitializerToExpression(fieldInitializer, factory, myField.getType()); PsiExpression rExpression = ((PsiAssignmentExpression)assignment.getExpression()).getRExpression(); assert fieldInitializer != null; assert rExpression != null; rExpression.replace(fieldInitializer); Objects.requireNonNull(myField.getInitializer()).delete(); return assignment; }