void (@NotNull ActionContext context, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) { if (!(element instanceof PsiReferenceExpression referenceExpression)) return; PsiLocalVariable variable = tryCast(referenceExpression.resolve(), PsiLocalVariable.class); if (variable == null) return; final String variableText = getFieldText(variable); DeclarationInfo declarationInfo = DeclarationInfo.findExistingAnonymousClass(variable); if (declarationInfo != null) { replaceReferences(variable, declarationInfo.name); declarationInfo.replace(variableText); variable.delete(); return; } Project project = context.project(); List<String> suggestions = new VariableNameGenerator(variable, VariableKind.LOCAL_VARIABLE) .byName(NAMES).generateAll(true); String boxName = suggestions.get(0); String boxDeclarationText = "var " + boxName + " = new Object(){" + variableText + "};"; PsiStatement boxDeclaration = JavaPsiFacade.getElementFactory(project).createStatementFromText(boxDeclarationText, variable); replaceReferences(variable, boxName); PsiStatement statement = PsiTreeUtil.getParentOfType(variable, PsiStatement.class); if (statement == null) return; PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement)statement.replace(boxDeclaration); PsiLocalVariable localVariable = (PsiLocalVariable)declarationStatement.getDeclaredElements()[0]; updater.rename(localVariable, suggestions); }