private static boolean isValidPackage(@NotNull PsiDirectory directory) {
    Project project = directory.getProject();
    PsiDirectoryFactory factory = project.isDisposed() ? null : PsiDirectoryFactory.getInstance(project);
    return factory != null && factory.isPackage(directory) && factory.isValidPackageName(factory.getQualifiedName(directory, false));
  }
private static boolean isValidPackage(@NotNull PsiDirectory directory)