void (Project project, final PsiElementFactory elementFactory, PsiElement body, Set<String> usedLocalNames, PsiVariable[] parameters) { final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project); final Map<PsiVariable, String> names = new HashMap<>(); for (PsiVariable parameter : parameters) { String parameterName = parameter.getName(); String uniqueVariableName = UniqueNameGenerator.generateUniqueName(codeStyleManager.suggestUniqueVariableName(parameterName, parameter.getParent(), false), usedLocalNames); if (!Objects.equals(parameterName, uniqueVariableName)) { names.put(parameter, uniqueVariableName); } } if (names.isEmpty()) return; final Map<PsiElement, PsiElement> replacements = new LinkedHashMap<>(); body.accept(new JavaRecursiveElementWalkingVisitor() { @Override public void visitVariable(@NotNull PsiVariable variable) { super.visitVariable(variable); final String newName = names.get(variable); if (newName != null) { replacements.put(variable.getNameIdentifier(), elementFactory.createIdentifier(newName)); } } @Override public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) { super.visitReferenceExpression(expression); final PsiElement resolve = expression.resolve(); if (resolve instanceof PsiVariable) { final String newName = names.get(resolve); if (newName != null) { replacements.put(expression, elementFactory.createExpressionFromText(newName, expression)); } } } }); for (PsiElement psiElement : replacements.keySet()) { psiElement.replace(replacements.get(psiElement)); } }