Set<String> (@NotNull PsiElement element) { if (element instanceof PsiDirectory psiDirectory) { // normalize a directory to a corresponding package PsiPackage aPackage = ReadAction.compute(() -> JavaDirectoryService.getInstance().getPackage(psiDirectory)); return aPackage == null ? Collections.emptySet() : getElementNames(aPackage); } Set<String> result = new HashSet<>(); ApplicationManager.getApplication().runReadAction(() -> { if (element instanceof PsiPackage psiPackage) { ContainerUtil.addIfNotNull(result, psiPackage.getQualifiedName()); } else if (element instanceof PsiClass psiClass) { String qname = psiClass.getQualifiedName(); if (qname != null) { result.add(qname); PsiClass topLevelClass = PsiUtil.getTopLevelClass(element); if (topLevelClass != null && !(topLevelClass instanceof PsiSyntheticClass)) { String topName = topLevelClass.getQualifiedName(); assert topName != null : "topLevelClass : " + topLevelClass + "; element: " + element + " (" + qname + ") top level file: " + InjectedLanguageManager.getInstance( element.getProject()).getTopLevelFile(element); if (qname.length() > topName.length()) { result.add(topName + qname.substring(topName.length()).replace('.', '$')); } } } } else if (element instanceof PsiMethod psiMethod) { ContainerUtil.addIfNotNull(result, psiMethod.getName()); } else if (element instanceof PsiVariable psiVariable) { ContainerUtil.addIfNotNull(result, psiVariable.getName()); } else if (element instanceof PsiMetaOwner psiMetaOwner) { PsiMetaData metaData = psiMetaOwner.getMetaData(); if (metaData != null) { ContainerUtil.addIfNotNull(result, metaData.getName()); } } else if (element instanceof PsiNamedElement psiNamedElement) { ContainerUtil.addIfNotNull(result, psiNamedElement.getName()); } else if (element instanceof XmlAttributeValue xmlAttributeValue) { ContainerUtil.addIfNotNull(result, xmlAttributeValue.getValue()); } else { LOG.error("Unknown element type: " + element); } }); return result; }