String (@NotNull @NonNls String baseName, PsiElement place, boolean lookForward, Predicate<? super PsiVariable> canBeReused) { int index = 0; PsiElement scope = PsiTreeUtil.getNonStrictParentOfType(place, PsiStatement.class, PsiCodeBlock.class, PsiMethod.class); NextName: while (true) { String name = baseName; if (index > 0) { name += index; } index++; if (PsiUtil.isVariableNameUnique(name, place)) { if (lookForward) { final String name1 = name; PsiElement run = scope; while (run != null) { class CancelException extends RuntimeException { } try { run.accept(new JavaRecursiveElementWalkingVisitor() { @Override public void visitAnonymousClass(final @NotNull PsiAnonymousClass aClass) { } @Override public void visitVariable(@NotNull PsiVariable variable) { if (name1.equals(variable.getName()) && !canBeReused.test(variable)) { throw new CancelException(); } } }); } catch (CancelException e) { continue NextName; } run = run.getNextSibling(); if (scope instanceof PsiMethod) {//do not check next member for param name conflict break; } } } return name; } } }