void (@NotNull Project project, @NotNull PsiElement startElement, @NotNull ModPsiUpdater updater) { final PsiElement element = startElement.getParent(); if (!(element instanceof PsiAnonymousClass aClass)) { return; } final PsiElement parent = aClass.getParent(); if (!(parent instanceof PsiNewExpression newExpression)) { return; } final PsiElement ancestor = PsiTreeUtil.skipParentsOfType(newExpression, PsiParenthesizedExpression.class); final String qualifierText; if (ancestor instanceof PsiVariable) { qualifierText = ((PsiVariable)ancestor).getName(); } else if (ancestor instanceof PsiAssignmentExpression assignmentExpression) { final PsiExpression lhs = PsiUtil.skipParenthesizedExprDown(assignmentExpression.getLExpression()); if (!(lhs instanceof PsiReferenceExpression referenceExpression)) { return; } final PsiElement target = referenceExpression.resolve(); if (!(target instanceof PsiVariable)) { return; } qualifierText = referenceExpression.getText(); } else { return; } final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); final PsiJavaCodeReferenceElement baseClassReference = aClass.getBaseClassReference(); final PsiElement baseClassTarget = baseClassReference.resolve(); if (!(baseClassTarget instanceof PsiClass)) { return; } final PsiExpressionList argumentList = aClass.getArgumentList(); if (argumentList == null) { return; } qualifyReferences(aClass, (PsiClass) baseClassTarget, qualifierText); final PsiClassInitializer initializer = aClass.getInitializers()[0]; final PsiCodeBlock body = initializer.getBody(); PsiElement child = body.getLastBodyElement(); final PsiElement stop = body.getFirstBodyElement(); final PsiElement anchor = PsiTreeUtil.getParentOfType(aClass, PsiMember.class, PsiStatement.class); if (anchor == null) { return; } if (anchor instanceof PsiMember member) { final PsiClassInitializer newInitializer = factory.createClassInitializer(); if (member.hasModifierProperty(PsiModifier.STATIC)) { final PsiModifierList modifierList = newInitializer.getModifierList(); if (modifierList != null) { modifierList.setModifierProperty(PsiModifier.STATIC, true); } } final PsiCodeBlock initializerBody = newInitializer.getBody(); while (child != null && !child.equals(stop)) { initializerBody.add(child); child = child.getPrevSibling(); } member.getParent().addAfter(newInitializer, member); } else { final PsiElement container = anchor.getParent(); while (child != null && !child.equals(stop)) { container.addAfter(child, anchor); child = child.getPrevSibling(); } } final PsiExpression newNewExpression = factory.createExpressionFromText("new " + baseClassReference.getText() + argumentList.getText(), aClass); newExpression.replace(newNewExpression); }