String (String baseName, PsiElement place, PsiField fieldToReplace) { for(int index = 0;;index++) { final String name = index > 0 ? baseName + index : baseName; final PsiManager manager = place.getManager(); PsiResolveHelper helper = JavaPsiFacade.getInstance(manager.getProject()).getResolveHelper(); PsiVariable refVar = helper.resolveAccessibleReferencedVariable(name, place); if (refVar != null && !manager.areElementsEquivalent(refVar, fieldToReplace)) continue; final boolean[] found = {false}; place.accept(new JavaRecursiveElementWalkingVisitor() { @Override public void visitClass(@NotNull PsiClass aClass) { } @Override public void visitVariable(@NotNull PsiVariable variable) { if (name.equals(variable.getName())) { found[0] = true; stopWalking(); } } }); if (found[0]) { continue; } return name; } }