private static PsiClass @Nullable [] getTopLevelClasses(PsiElement element) {
    while (true) {
      if (element == null || element instanceof PsiFile) break;
      if (element instanceof PsiClass &&
          element.getParent() != null &&
          ((PsiClass)element).getContainingClass() == null &&
          !(element instanceof PsiAnonymousClass) &&
          !(element instanceof PsiImplicitClass)) {
        break;
      }
      element = element.getParent();
    }
    if (element instanceof PsiClassOwner) {
      PsiClass[] classes = ((PsiClassOwner)element).getClasses();
      ArrayList<PsiClass> buffer = new ArrayList<>();
      for (final PsiClass aClass : classes) {
        if (isSynthetic(aClass)) {
          return null;
        }
        if (aClass instanceof PsiImplicitClass) {
          return null;
        }
        buffer.add(aClass);
      }
      return buffer.toArray(PsiClass.EMPTY_ARRAY);
    }
    return element instanceof PsiClass ? new PsiClass[]{(PsiClass)element} : null;
  }
private static PsiClass @Nullable [] getTopLevelClasses(PsiElement element)