Set<String> (@NotNull PsiElement position) { Set<String> result = new LinkedHashSet<>(); boolean add = false; while (position != null) { if (position instanceof PsiAnonymousClass) { add = true; } else if (add && position instanceof PsiClass) { ContainerUtil.addIfNotNull(result, ((PsiClass)position).getName()); } position = position.getParent(); } return result; }