List<PsiElement> (final PsiMember psiMember) { final PsiElement body; if (psiMember instanceof PsiMethod) { body = ((PsiMethod)psiMember).getBody(); } else if (psiMember instanceof PsiField) { body = ((PsiField)psiMember).getInitializer(); } else if (psiMember instanceof PsiClass) { body = psiMember; } else { body = null; } if (body != null) { final PsiClass containingClass = psiMember.getContainingClass(); final Set<PsiElement> elementsToCheck = new HashSet<>(); body.accept(new JavaRecursiveElementWalkingVisitor() { @Override public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) { super.visitReferenceExpression(expression); PsiElement resolved = expression.resolve(); if (resolved instanceof PsiMethod || resolved instanceof PsiField) { ContainerUtil.addAllNotNull(elementsToCheck, resolved); } } @Override public void visitLiteralExpression(@NotNull PsiLiteralExpression expression) { super.visitLiteralExpression(expression); PsiReference @NotNull [] references = expression.getReferences(); for (PsiReference reference : references) { if (reference instanceof PsiPolyVariantReference) { PsiElement[] nonMembers = Arrays.stream(((PsiPolyVariantReference)reference).multiResolve(false)) .map(result -> result.getElement()) .filter(e -> !(e instanceof PsiMember)) .toArray(PsiElement[]::new); if (nonMembers.length < 10) { ContainerUtil.addAllNotNull(elementsToCheck, nonMembers); } } else { PsiElement resolve = reference.resolve(); if (resolve != null && !(resolve instanceof PsiMember)) { elementsToCheck.add(resolve); } } } } }); PsiFile containingFile = body.getContainingFile(); return elementsToCheck .stream() .filter(m -> m != containingFile) .filter(m -> !PsiTreeUtil.isAncestor(psiMember, m, true)) .filter(m -> !(m instanceof PsiMember) || containingClass != null && containingClass.equals(((PsiMember)m).getContainingClass()) && !psiMember.equals(m)) .filter(m -> !(m instanceof PsiMethod) || ((PsiMethod)m).findDeepestSuperMethods().length == 0) .filter(m -> m.isPhysical()) .filter(m -> usedOnlyIn(m, psiMember)) .collect(Collectors.toList()); } return null; }