public static @NotNull List<PsiElement> computeReferencedCodeSafeToDelete(
    @Nullable PsiMember psiMember,
    @NotNull Predicate<? super PsiElement> additionalFilter) {
    final PsiElement body;
    if (psiMember instanceof PsiMethod method) {
      body = method.getBody();
    }
    else if (psiMember instanceof PsiField field) {
      body = field.getInitializer();
    }
    else if (psiMember instanceof PsiClass) {
      body = psiMember;
    }
    else {
      body = null;
    }
    if (body == null) {
      // Do not use List.of(), as call sites like to delete from the resulting list
      return Collections.emptyList();
    }
    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 ref) {
            PsiElement[] nonMembers = Arrays.stream(ref.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();
    PsiManager manager = containingFile.getManager();
    return elementsToCheck
      .stream()
      .filter(additionalFilter)
      .filter(manager::isInProject)
      .filter(m -> m != containingFile)
      .filter(m -> !PsiTreeUtil.isAncestor(psiMember, m, true))
      .filter(m -> !(m instanceof PsiMember member) ||
                   containingClass != null && containingClass.equals(member.getContainingClass()) && !psiMember.equals(m))
      .filter(m -> !(m instanceof PsiMethod method) ||
                   method.findDeepestSuperMethods().length == 0)
      .filter(m -> m.isPhysical())
      .filter(m -> usedOnlyIn(m, psiMember))
      .collect(Collectors.toList());
  }
public static @NotNull List<PsiElement> computeReferencedCodeSafeToDelete(
    @Nullable PsiMember psiMember,
    @NotNull Predicate<? super PsiElement> additionalFilter)