void (@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) { final PsiElement parent = element.getParent(); final PsiElement grandParent = parent.getParent(); if (!(grandParent instanceof PsiMethodCallExpression methodCallExpression)) return; final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression(); final PsiExpression qualifier = methodExpression.getQualifierExpression(); final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); final PsiExpressionList argumentList = methodCallExpression.getArgumentList(); final PsiExpression[] arguments = argumentList.getExpressions(); if (arguments.length != 1) return; final PsiExpression argument = arguments[0]; if (qualifier == null) return; final String collectionText = qualifier.getText(); final PsiType type = argument.getType(); if (type == null) return; final PsiType componentType = type.getDeepComponentType(); final String typeText = componentType.getCanonicalText(); if (myEmptyPreferred || ExpressionUtils.isSafelyRecomputableExpression(qualifier)) { CommentTracker ct = new CommentTracker(); String sizeClause = myEmptyPreferred ? "0" : collectionText + ".size()"; @NonNls final String replacementText = "new " + typeText + '[' + sizeClause + "]"; ct.replaceAndRestoreComments(argument, replacementText); return; } // need to introduce a variable to prevent calling a method twice PsiStatement statement = PsiTreeUtil.getParentOfType(methodCallExpression, PsiStatement.class); if (statement == null) return; final PsiType qualifierType = qualifier.getType(); if (qualifierType == null) return; List<String> names = new VariableNameGenerator(statement, VariableKind.LOCAL_VARIABLE) .byExpression(qualifier).byType(qualifierType).generateAll(true); String name = names.get(0); PsiDeclarationStatement declarationStatement = factory.createVariableDeclarationStatement(name, qualifierType, qualifier); PsiElement statementParent = statement.getParent(); while (statementParent instanceof PsiLoopStatement || statementParent instanceof PsiIfStatement) { statement = (PsiStatement)statementParent; statementParent = statement.getParent(); } final String toArrayText = name + ".toArray(new " + typeText + "[" + name + ".size()])"; PsiMethodCallExpression newMethodCallExpression = (PsiMethodCallExpression)factory.createExpressionFromText(toArrayText, methodCallExpression); declarationStatement = (PsiDeclarationStatement)statementParent.addBefore(declarationStatement, statement); methodCallExpression.replace(newMethodCallExpression); final PsiVariable variable = (PsiVariable)declarationStatement.getDeclaredElements()[0]; updater.rename(variable, names); }