public static boolean isValidLibrary(@NotNull Library library) {
    LibraryTable table = library.getTable();
    if (table != null) {
      String name = library.getName();
      return name != null && table.getLibraryByName(name) == library;
    }

    if (!(library instanceof LibraryEx)) return false;

    Module module = ((LibraryEx)library).getModule();
    if (module == null || module.isDisposed()) return false;
    for (OrderEntry entry : ModuleRootManager.getInstance(module).getOrderEntries()) {
      if (entry instanceof LibraryOrderEntry && ((LibraryOrderEntry)entry).getLibrary() == library) {
        return true;
      }
    }
    return false;
  }
public static boolean isValidLibrary(@NotNull Library library)