private static @Nullable PsiClass getUpperClass(@NotNull PsiElement source) {
    PsiElement target = source;
    PsiElement next = source;
    while (next != null) {
      target = next;
      next = PsiTreeUtil.findFirstParent(target, true,
                                         parent -> parent instanceof PsiClass &&
                                                   !(parent instanceof PsiImplicitClass) &&
                                                   !(parent instanceof PsiAnonymousClass));
    }
    return target instanceof PsiClass ? (PsiClass)target : null;
  }
private static @Nullable PsiClass getUpperClass(@NotNull PsiElement source)