@NotNull
 public static String getLibraryName(@NotNull Library library) {
   final String result = library.getName();
   if (result != null) {
     return result;
   }
   String[] endingsToStrip = {"/", "!", ".jar"};
   StringBuilder buffer = new StringBuilder();
   for (OrderRootType type : OrderRootType.getAllTypes()) {
     for (String url : library.getUrls(type)) {
       buffer.setLength(0);
       buffer.append(url);
       for (String ending : endingsToStrip) {
         if (buffer.lastIndexOf(ending) == buffer.length() - ending.length()) {
           buffer.setLength(buffer.length() - ending.length());
         }
       }
       final int i = buffer.lastIndexOf(PATH_SEPARATOR);
       if (i < 0 || i >= buffer.length() - 1) {
         continue;
       }
       String candidate = buffer.substring(i + 1);
       if (!StringUtil.isEmpty(candidate)) {
         return candidate;
       }
     }
   }
   assert false;
   return "unknown-lib";
 }
 private static boolean hasUserPaths(OrderRootType rootType, Library library, String pathToJar) {
   String[] sources = library.getUrls(rootType);
   for (String each : sources) {
     if (!FileUtil.startsWith(each, pathToJar)) return true;
   }
   return false;
 }
  @NotNull
  public static Library updatePackagesLibraryRoots(
      @NotNull final Project project, @NotNull final DartLibInfo libInfo) {
    final LibraryTable projectLibraryTable = ProjectLibraryTable.getInstance(project);
    final Library existingLibrary =
        projectLibraryTable.getLibraryByName(DartPackagesLibraryType.DART_PACKAGES_LIBRARY_NAME);
    final Library library =
        existingLibrary != null
            ? existingLibrary
            : ApplicationManager.getApplication()
                .runWriteAction(
                    new Computable<Library>() {
                      @Override
                      public Library compute() {
                        final LibraryTableBase.ModifiableModel libTableModel =
                            ProjectLibraryTable.getInstance(project).getModifiableModel();
                        final Library lib =
                            libTableModel.createLibrary(
                                DartPackagesLibraryType.DART_PACKAGES_LIBRARY_NAME,
                                DartPackagesLibraryType.LIBRARY_KIND);
                        libTableModel.commit();
                        return lib;
                      }
                    });

    final String[] existingUrls = library.getUrls(OrderRootType.CLASSES);

    final Collection<String> libRootUrls = libInfo.getLibRootUrls();

    if ((!libInfo.isProjectWithoutPubspec()
            && isBrokenPackageMap(((LibraryEx) library).getProperties()))
        || existingUrls.length != libRootUrls.size()
        || !libRootUrls.containsAll(Arrays.asList(existingUrls))) {
      ApplicationManager.getApplication()
          .runWriteAction(
              new Runnable() {
                @Override
                public void run() {
                  final LibraryEx.ModifiableModelEx model =
                      (LibraryEx.ModifiableModelEx) library.getModifiableModel();
                  for (String url : existingUrls) {
                    model.removeRoot(url, OrderRootType.CLASSES);
                  }

                  for (String url : libRootUrls) {
                    model.addRoot(url, OrderRootType.CLASSES);
                  }

                  final DartPackagesLibraryProperties libraryProperties =
                      new DartPackagesLibraryProperties();
                  libraryProperties.setPackageNameToDirsMap(libInfo.getPackagesMap());
                  model.setProperties(libraryProperties);

                  model.commit();
                }
              });
    }

    return library;
  }
 @NotNull
 public static String getLibraryName(@NotNull Library library) {
   final String result = library.getName();
   if (result != null) {
     return result;
   }
   for (OrderRootType type : OrderRootType.getAllTypes()) {
     for (String url : library.getUrls(type)) {
       String candidate = extractNameFromPath(url);
       if (!StringUtil.isEmpty(candidate)) {
         return candidate;
       }
     }
   }
   assert false;
   return "unknown-lib";
 }
  public static boolean isChangedByUser(Library library) {
    String[] classRoots = library.getUrls(OrderRootType.CLASSES);
    if (classRoots.length != 1) return true;

    String classes = classRoots[0];

    if (!classes.endsWith("!/")) return true;

    int dotPos = classes.lastIndexOf("/", classes.length() - 2 /* trim ending !/ */);
    if (dotPos == -1) return true;
    String pathToJar = classes.substring(0, dotPos);

    if (hasUserPaths(OrderRootType.SOURCES, library, pathToJar)) return true;
    if (hasUserPaths(JavadocOrderRootType.getInstance(), library, pathToJar)) return true;

    return false;
  }
  private void configSurefirePlugin() {
    List<String> urls = new ArrayList<String>();

    AccessToken accessToken = ReadAction.start();
    try {
      MavenDomProjectModel domModel = null;

      Element config =
          myMavenProject.getPluginConfiguration(
              "org.apache.maven.plugins", "maven-surefire-plugin");
      for (String each :
          MavenJDOMUtil.findChildrenValuesByPath(
              config, "additionalClasspathElements", "additionalClasspathElement")) {
        String url = VfsUtil.pathToUrl(each);

        if (domModel == null) {
          domModel =
              MavenDomUtil.getMavenDomProjectModel(myModule.getProject(), myMavenProject.getFile());
        }

        if (domModel != null) {
          url = MavenPropertyResolver.resolve(url, domModel);
        }

        urls.add(url);
      }
    } finally {
      accessToken.finish();
    }

    LibraryTable moduleLibraryTable = myRootModelAdapter.getRootModel().getModuleLibraryTable();

    Library library = moduleLibraryTable.getLibraryByName(SUREFIRE_PLUGIN_LIBRARY_NAME);
    if (library == null) {
      if (urls.isEmpty()) {
        return;
      }

      library = moduleLibraryTable.createLibrary(SUREFIRE_PLUGIN_LIBRARY_NAME);
      LibraryOrderEntry orderEntry =
          myRootModelAdapter.getRootModel().findLibraryOrderEntry(library);
      orderEntry.setScope(DependencyScope.TEST);
    } else {
      if (urls.isEmpty()) {
        moduleLibraryTable.removeLibrary(library);
        return;
      }
    }

    String[] oldUrls = library.getUrls(OrderRootType.CLASSES);
    if (!urls.equals(Arrays.asList(oldUrls))) {
      Library.ModifiableModel modifiableModel = library.getModifiableModel();

      for (String url : oldUrls) {
        modifiableModel.removeRoot(url, OrderRootType.CLASSES);
      }

      for (String url : urls) {
        modifiableModel.addRoot(url, OrderRootType.CLASSES);
      }

      modifiableModel.commit();
    }
  }
 @NotNull
 @Override
 public String[] getLibraryUrls(@NotNull Library library, @NotNull OrderRootType type) {
   return library.getUrls(type);
 }